Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
ensmallen/example.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
70 lines (51 sloc)
1.46 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example implementation of an objective function class for linear regression | |
// and usage of the L-BFGS optimizer. | |
// | |
// Compilation: | |
// g++ example.cpp -o example -O3 -larmadillo | |
#include <iostream> | |
#include <armadillo> | |
#include <ensmallen.hpp> | |
class LinearRegressionFunction | |
{ | |
public: | |
LinearRegressionFunction(arma::mat& X, arma::vec& y) : X(X), y(y) { } | |
double EvaluateWithGradient(const arma::mat& theta, arma::mat& gradient) | |
{ | |
const arma::vec tmp = X.t() * theta - y; | |
gradient = 2 * X * tmp; | |
return arma::dot(tmp,tmp); | |
} | |
private: | |
const arma::mat& X; | |
const arma::vec& y; | |
}; | |
int main(int argc, char** argv) | |
{ | |
if (argc < 3) | |
{ | |
std::cout << "usage: " << argv[0] << " n_dims n_points" << std::endl; | |
return -1; | |
} | |
int n_dims = atoi(argv[1]); | |
int n_points = atoi(argv[2]); | |
// generate noisy dataset with a slight linear pattern | |
arma::mat X(n_dims, n_points, arma::fill::randu); | |
arma::vec y( n_points, arma::fill::randu); | |
for (size_t i = 0; i < n_points; ++i) | |
{ | |
double a = arma::randu(); | |
X(1, i) += a; | |
y(i) += a; | |
} | |
LinearRegressionFunction lrf(X, y); | |
// create a Limited-memory BFGS optimizer object with default parameters | |
ens::L_BFGS opt; | |
opt.MaxIterations() = 10; | |
// initial point (uniform random) | |
arma::vec theta(n_dims, arma::fill::randu); | |
opt.Optimize(lrf, theta); | |
// theta now contains the optimized parameters | |
theta.print("theta:"); | |
return 0; | |
} |