Guest User

Untitled

a guest
Jun 9th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.17 KB | None | 0 0
  1. 1) This part has almost same timing as that of total impostors()
  2. Timer::Start("sgd-knn");
  3. // Perform KNN search with differently labeled points as reference
  4. // set and same class points as query set.
  5. knn.Train(dataset.cols(indexDiff[i]));
  6. knn.Search(subDataset.cols(subIndexSame), k, neighbors, distances);
  7. Timer::Stop("sgd-knn");
  8.  
  9.  
  10.  
  11. 2) Similarly, the part below is the one which is most inefficient, it takes a lot more time than impostors()
  12.  
  13.   Timer::Start("sgd-total_computation_part");
  14.   for (size_t i = begin; i < begin + batchSize; i++)
  15.   {
  16.     for (size_t j = 0; j < k ; j++)
  17.     {
  18.       // Calculate cost due to distance between target neighbors & data point.
  19.       double eval = metric.Evaluate(transformedDataset.col(i),
  20.                         transformedDataset.col(targetNeighbors(j, i)));
  21.       cost += (1 - regularization) * eval;
  22.  
  23.       // Calculate gradient due to target neighbors.
  24.       arma::vec diff = dataset.col(i) - dataset.col(targetNeighbors(j, i));
  25.       cij += diff * arma::trans(diff);
  26.     }
  27.  
  28.     for (int j = k - 1; j >= 0; j--)
  29.     {
  30.       // Bound constraints to avoid uneccesary computation.
  31.       for (size_t l = 0, bp = k; l < bp ; l++)
  32.       {
  33.         // Calculate cost due to {data point, target neighbors, impostors}
  34.         // triplets.
  35.         double eval = metric.Evaluate(transformedDataset.col(i),
  36.                           transformedDataset.col(targetNeighbors(j, i))) -
  37.                       metric.Evaluate(transformedDataset.col(i),
  38.                           transformedDataset.col(impostors(l, i)));
  39.  
  40.         // Check bounding condition.
  41.         if (eval < -1)
  42.         {
  43.           // update bound.
  44.           bp = l;
  45.           break;
  46.         }
  47.  
  48.         cost += regularization * 1 + eval;
  49.  
  50.         // Caculate gradient due to impostors.
  51.         arma::vec diff = dataset.col(i) - dataset.col(targetNeighbors(j, i));
  52.         cil += diff * arma::trans(diff);
  53.  
  54.         diff = dataset.col(i) - dataset.col(impostors(l, i));
  55.         cil -= diff * arma::trans(diff);
  56.       }
  57.     }
  58.   }
  59.  
  60.   gradient = 2 * coordinates * ((1 - regularization) * cij +
  61.       regularization * cil);
  62.   Timer::Stop("sgd-total_computation_part");
Add Comment
Please, Sign In to add comment