Advertisement
Guest User

Untitled

a guest
Jul 9th, 2014
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. namespace mlpack {
  2. namespace optimization {
  3.  
  4. template<>
  5. double SGD<mlpack::svd::RegularizedSVDFunction>::Optimize(arma::mat& parameters)
  6. {
  7. // Find the number of functions to use.
  8. const size_t numFunctions = function.NumFunctions();
  9.  
  10. // To keep track of where we are and how things are going.
  11. size_t currentFunction = 0;
  12. double overallObjective = 0;
  13. double lastObjective = DBL_MAX;
  14.  
  15. // Calculate the first objective function.
  16. for(size_t i = 0; i < numFunctions; i++)
  17. overallObjective += function.Evaluate(parameters, i);
  18.  
  19. const arma::mat data = function.Dataset();
  20.  
  21. // Now iterate!
  22. for(size_t i = 1; i != maxIterations; i++, currentFunction++)
  23. {
  24. // Is this iteration the start of a sequence?
  25. if((currentFunction % numFunctions) == 0)
  26. {
  27. // Reset the counter variables.
  28. lastObjective = overallObjective;
  29. overallObjective = 0;
  30. currentFunction = 0;
  31. }
  32.  
  33. const size_t numUsers = function.NumUsers();
  34.  
  35. // Indices for accessing the the correct parameter columns.
  36. const size_t user = data(0, currentFunction);
  37. const size_t item = data(1, currentFunction) + numUsers;
  38.  
  39. // Prediction error for the example.
  40. const double rating = data(2, i);
  41. double ratingError = rating - arma::dot(parameters.col(user),
  42. parameters.col(item));
  43.  
  44. // Gradient is non-zero only for the parameter columns corresponding to the
  45. // example.
  46. iterate.col(user) -= stepSize * (lambda * parameters.col(user) -
  47. ratingError * parameters.col(item));
  48. iterate.col(item) -= stepSize * (lambda * parameters.col(item) -
  49. ratingError * parameters.col(user));
  50.  
  51. // Now add that to the overall objective function.
  52. overallObjective += function.Evaluate(parameters, currentFunction);
  53. }
  54.  
  55. return overallObjective;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement