Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Sample test for sparse matrix completion
#include <mlpack/core.hpp>
#include <mlpack/core/optimizers/parallel_sgd/parallel_sgd.hpp>
#include <mlpack/core/optimizers/parallel_sgd/decay_policies/constant_step.hpp>
#include <mlpack/core/optimizers/parallel_sgd/sparse_mc_function.hpp>
using namespace std;
using namespace mlpack;
using namespace mlpack::optimization;
int main()
{
arma::mat X, values;
arma::umat indices;
mlpack::data::Load("./completion_X.csv", X, true, false);
mlpack::data::Load("./completion_indices.csv", indices, true, false);
values.set_size(indices.n_cols);
for(size_t i = 0; i < indices.n_cols; ++i)
{
values(i) = X(indices(0, i), indices(1, i));
}
size_t rank = 6;
SparseMCLossFunction f(arma::trans(indices.row(0)),
arma::trans(indices.row(1)), values, 0.001, rank);
ConstantStep decayPolicy(0.4);
ParallelSGD<ConstantStep> s(0, f.NumFunctions() / 4, 1e-10, decayPolicy);
arma::mat iterate(rank, X.n_cols + X.n_rows, arma::fill::randu);
auto final = s.Optimize(f, iterate);
std::cout << "Final : " << final << endl;
arma::mat recovered = f.Recover(iterate);
std::cout << "Err : "<< arma::norm(X - recovered, "fro") / arma::norm(X, "fro") << std::endl;
}
Final : 0.0197419
Err : 0.00129987
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment