1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
// 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;
}
|