Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1) This part has almost same timing as that of total impostors()
- Timer::Start("sgd-knn");
- // Perform KNN search with differently labeled points as reference
- // set and same class points as query set.
- knn.Train(dataset.cols(indexDiff[i]));
- knn.Search(subDataset.cols(subIndexSame), k, neighbors, distances);
- Timer::Stop("sgd-knn");
- 2) Similarly, the part below is the one which is most inefficient, it takes a lot more time than impostors()
- Timer::Start("sgd-total_computation_part");
- for (size_t i = begin; i < begin + batchSize; i++)
- {
- for (size_t j = 0; j < k ; j++)
- {
- // Calculate cost due to distance between target neighbors & data point.
- double eval = metric.Evaluate(transformedDataset.col(i),
- transformedDataset.col(targetNeighbors(j, i)));
- cost += (1 - regularization) * eval;
- // Calculate gradient due to target neighbors.
- arma::vec diff = dataset.col(i) - dataset.col(targetNeighbors(j, i));
- cij += diff * arma::trans(diff);
- }
- for (int j = k - 1; j >= 0; j--)
- {
- // Bound constraints to avoid uneccesary computation.
- for (size_t l = 0, bp = k; l < bp ; l++)
- {
- // Calculate cost due to {data point, target neighbors, impostors}
- // triplets.
- double eval = metric.Evaluate(transformedDataset.col(i),
- transformedDataset.col(targetNeighbors(j, i))) -
- metric.Evaluate(transformedDataset.col(i),
- transformedDataset.col(impostors(l, i)));
- // Check bounding condition.
- if (eval < -1)
- {
- // update bound.
- bp = l;
- break;
- }
- cost += regularization * 1 + eval;
- // Caculate gradient due to impostors.
- arma::vec diff = dataset.col(i) - dataset.col(targetNeighbors(j, i));
- cil += diff * arma::trans(diff);
- diff = dataset.col(i) - dataset.col(impostors(l, i));
- cil -= diff * arma::trans(diff);
- }
- }
- }
- gradient = 2 * coordinates * ((1 - regularization) * cij +
- regularization * cil);
- Timer::Stop("sgd-total_computation_part");
Add Comment
Please, Sign In to add comment