Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace mlpack {
- namespace optimization {
- template<>
- double SGD<mlpack::svd::RegularizedSVDFunction>::Optimize(arma::mat& parameters)
- {
- // Find the number of functions to use.
- const size_t numFunctions = function.NumFunctions();
- // To keep track of where we are and how things are going.
- size_t currentFunction = 0;
- double overallObjective = 0;
- double lastObjective = DBL_MAX;
- // Calculate the first objective function.
- for(size_t i = 0; i < numFunctions; i++)
- overallObjective += function.Evaluate(parameters, i);
- const arma::mat data = function.Dataset();
- // Now iterate!
- for(size_t i = 1; i != maxIterations; i++, currentFunction++)
- {
- // Is this iteration the start of a sequence?
- if((currentFunction % numFunctions) == 0)
- {
- // Reset the counter variables.
- lastObjective = overallObjective;
- overallObjective = 0;
- currentFunction = 0;
- }
- const size_t numUsers = function.NumUsers();
- // Indices for accessing the the correct parameter columns.
- const size_t user = data(0, currentFunction);
- const size_t item = data(1, currentFunction) + numUsers;
- // Prediction error for the example.
- const double rating = data(2, i);
- double ratingError = rating - arma::dot(parameters.col(user),
- parameters.col(item));
- // Gradient is non-zero only for the parameter columns corresponding to the
- // example.
- iterate.col(user) -= stepSize * (lambda * parameters.col(user) -
- ratingError * parameters.col(item));
- iterate.col(item) -= stepSize * (lambda * parameters.col(item) -
- ratingError * parameters.col(user));
- // Now add that to the overall objective function.
- overallObjective += function.Evaluate(parameters, currentFunction);
- }
- return overallObjective;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement