Beispiel #1
0
void train(lbfgsfloatval_t* fx, lbfgsfloatval_t* parameter,
		   int featsize, int poslabel,
		   boost::shared_ptr<Eigen::SparseMatrix<double, Eigen::RowMajor> >& samples,
		   boost::shared_ptr<Eigen::VectorXi>& labels,
		   boost::shared_ptr<lbfgs_parameter_t>& opt_params,
		   boost::shared_ptr<derivative_parameter_t>& prog_params){

			   boost::shared_ptr<Eigen::VectorXi> instlabels( new Eigen::VectorXi(labels->rows()) );
			   instlabels->setOnes();
			   Eigen::Map<Eigen::VectorXd> map_param(parameter, featsize, 1);
			   for(int i=0; i < instlabels->rows(); ++i){
				   if( labels->coeff(i) != poslabel ){
					   instlabels->coeffRef(i) = -1;
				   }
			   }
			   map_param.setZero();
			   int ret=-1;
			   optimization_instance_t optInst(poslabel, samples, instlabels, prog_params);
			   do {
				   ret = lbfgs(featsize, parameter, fx,
					   evaluate, progress, (void*)&optInst, 
					   opt_params.get());

				   if( 0 == ret || LBFGSERR_MAXIMUMLINESEARCH == ret){
					   std::cout << "L-BFGS optimize successful" << std::endl;
				   }
				   std::cout << "L-BFGS optimization terminated with status code = " << ret << std::endl;
				   std::cout << "objective function value = " << *fx << std::endl;

				   std::cout << "Training Accuracy " << test(parameter, featsize, samples, instlabels) * 100
					   << std::endl;

			   } while( 0 );
}
Beispiel #2
0
/** @pred optimizer_run(-F,-Status)
Runs the optimization, _F is the best (minimal) function value and
Status (int) is the status code returned by libLBFGS. Anything except
0 indicates an error, see the documentation of libLBFGS for the
meaning.
*/
static int optimizer_run(void) {
  int ret = 0;
  YAP_Term t1 = YAP_ARG1;
  YAP_Term t2 = YAP_ARG2;
  YAP_Int s1, s2;
  lbfgsfloatval_t fx;
  lbfgsfloatval_t * tmp_x=x;

 if (optimizer_status == OPTIMIZER_STATUS_NONE) {
    printf("ERROR: Memory for parameter vector not initialized, please call optimizer_initialize/1 first.\n");
    return FALSE;
  }
  
  if (optimizer_status != OPTIMIZER_STATUS_INITIALIZED) {
    printf("ERROR: Optimizer is running right now. Please wait till it is finished.\n");
    return FALSE;
  }

  
  // both arguments have to be variables
  if (! YAP_IsVarTerm(t1) || ! YAP_IsVarTerm(t2)) {
    return FALSE;
  }
  s1 = YAP_InitSlot(t1);
  s2 = YAP_InitSlot(t2);
  optimizer_status = OPTIMIZER_STATUS_RUNNING;
  ret = lbfgs(n, x, &fx, evaluate, progress, NULL, &param);
  x=tmp_x;
  optimizer_status = OPTIMIZER_STATUS_INITIALIZED;

  YAP_Unify(YAP_GetFromSlot(s1),YAP_MkFloatTerm(fx));
  YAP_Unify(YAP_GetFromSlot(s2),YAP_MkIntTerm(ret));

  return TRUE;
}
TEST(Services, do_bfgs_optimize__lbfgs) {
  std::vector<double> cont_vector(2);
  cont_vector[0] = -1; cont_vector[1] = 1;
  std::vector<int> disc_vector;

  static const std::string DATA("");
  std::stringstream data_stream(DATA);
  stan::io::dump dummy_context(data_stream);
  Model model(dummy_context);

  typedef stan::optimization::BFGSLineSearch<Model,stan::optimization::LBFGSUpdate<> > Optimizer_LBFGS;
  Optimizer_LBFGS lbfgs(model, cont_vector, disc_vector, &std::cout);


  double lp = 0;
  bool save_iterations = true;
  int refresh = 0;
  int return_code;
  unsigned int random_seed = 0;
  rng_t base_rng(random_seed);

  std::fstream* output_stream = 0;
  mock_callback callback;

  return_code = stan::services::optimization::do_bfgs_optimize(model, lbfgs, base_rng,
                                                               lp, cont_vector, disc_vector,
                                                               output_stream, &std::cout,
                                                               save_iterations, refresh,
                                                               callback);
  EXPECT_FLOAT_EQ(return_code, 0);
  EXPECT_EQ(35, callback.n);
}
float64_t CKLDualInferenceMethod::lbfgs_optimization()
{
	lbfgs_parameter_t lbfgs_param;
	lbfgs_param.m = m_m;
	lbfgs_param.max_linesearch = m_max_linesearch;
	lbfgs_param.linesearch = m_linesearch;
	lbfgs_param.max_iterations = m_max_iterations;
	lbfgs_param.delta = m_delta;
	lbfgs_param.past = m_past;
	lbfgs_param.epsilon = m_epsilon;
	lbfgs_param.min_step = m_min_step;
	lbfgs_param.max_step = m_max_step;
	lbfgs_param.ftol = m_ftol;
	lbfgs_param.wolfe = m_wolfe;
	lbfgs_param.gtol = m_gtol;
	lbfgs_param.xtol = m_xtol;
	lbfgs_param.orthantwise_c = m_orthantwise_c;
	lbfgs_param.orthantwise_start = m_orthantwise_start;
	lbfgs_param.orthantwise_end = m_orthantwise_end;

	float64_t nlml_opt=0;
	void * obj_prt = static_cast<void *>(this);

	Map<VectorXd> eigen_W(m_W.vector, m_W.vlen);
	lbfgs(m_W.vlen, m_W.vector, &nlml_opt,
		CKLDualInferenceMethod::evaluate,
		NULL, obj_prt, &lbfgs_param, CKLDualInferenceMethod::adjust_step);
	return nlml_opt;
}
Beispiel #5
0
/* Try to compute a trajectory in the given region.
 *  l       Vector of length {params.N} that will receieve the left thruster
 *          values for each time step.
 *  r       Vector of length {params.N} that will receive the right thruster
 *          values for each time step.
 *  params  A structure containing the problem to solve.
 */
region_result_t compute_trajectory(double *l, double *r, region_params_t *params)
{
    lbfgsfloatval_t fx;
    lbfgs_parameter_t param;
    int i;
    int n = params->N;
    lbfgsfloatval_t *x = lbfgs_malloc(n*2);
    int ret = 0;

    if (!x) {
        printf("ERROR: Failed to allocate a memory block for variables.\n");
        return 1;
    }

    for (i = 0; i < n*2; i++) {
        x[i] = 0;
    }

    /* Initialize the parameters for the L-BFGS optimization. */
    lbfgs_parameter_init(&param);
    param.linesearch = LBFGS_LINESEARCH_BACKTRACKING_STRONG_WOLFE;
    param.past = 100;
    param.delta = 1e-4;
    param.max_linesearch = 1000;
    param.m = 5;

    ret = lbfgs(n*2, x, &fx, evaluate, progress, params, &param);
    memcpy(l, x, sizeof(l[0])*n);
    memcpy(r, x+n, sizeof(r[0])*n);

    printf("\nL-BFGS optimization terminated with status code = %d\n", ret);
    printf("fx = %f\n", fx);
    printf("\n");

    printf("MATLAB variables:\n");
    printf("l = [ ");
    for (i = 0; i < n; i++) { printf("%f ", l[i]); }
    printf(" ];\nr = [ ");
    for (i = 0; i < n; i++) { printf("%f ", r[i]); }
    printf(" ];\n");

    printf("rx = [ ");
    for (i = 0; i < params->polycount; i++) { printf("%f ", params->poly[i].x); }
    printf(" ];\nry = [ ");
    for (i = 0; i < params->polycount; i++) { printf("%f ", params->poly[i].y); }
    printf(" ];\n");
    printf("parm = [ %f %f %f %f %f %f %f %f %f ];\n",
        params->p_0.x, params->p_0.y, params->v_0.x, params->v_0.y,
        params->d_0.x, params->d_0.y, params->omega_0, params->mass,
        params->radius);
    printf("dt = %f\n", params->dt);

    lbfgs_free(x);
    
    if (ret == 0 || ret == 1 || ret == 2) {
        return REGION_SOLVED;
    } else {
        return REGION_STUCK;
    }
}
float64_t CKLDualInferenceMethodMinimizer::minimize()
{
	lbfgs_parameter_t lbfgs_param;
	lbfgs_param.m = m_m;
	lbfgs_param.max_linesearch = m_max_linesearch;
	lbfgs_param.linesearch = LBFGSLineSearchHelper::get_lbfgs_linear_search(m_linesearch_id);
	lbfgs_param.max_iterations = m_max_iterations;
	lbfgs_param.delta = m_delta;
	lbfgs_param.past = m_past;
	lbfgs_param.epsilon = m_epsilon;
	lbfgs_param.min_step = m_min_step;
	lbfgs_param.max_step = m_max_step;
	lbfgs_param.ftol = m_ftol;
	lbfgs_param.wolfe = m_wolfe;
	lbfgs_param.gtol = m_gtol;
	lbfgs_param.xtol = m_xtol;
	lbfgs_param.orthantwise_c = m_orthantwise_c;
	lbfgs_param.orthantwise_start = m_orthantwise_start;
	lbfgs_param.orthantwise_end = m_orthantwise_end;

	init_minimization();

	float64_t cost=0.0;
	int error_code=lbfgs(m_target_variable.vlen, m_target_variable.vector,
		&cost, CKLDualInferenceMethodMinimizer::evaluate,
		NULL, this, &lbfgs_param, CKLDualInferenceMethodMinimizer::adjust_step);

	if(error_code!=0 && error_code!=LBFGS_ALREADY_MINIMIZED)
	{
	  SG_SWARNING("Error(s) happened during L-BFGS optimization (error code:%d)\n",
		  error_code);
	}
	return cost;
}
Beispiel #7
0
 int run(Array &x)
 {
     double fx;
     int ret = lbfgs(x.size(), x.data(), &fx, _evaluate, _progress, this, &_params);
     _error_code = ret;
     return ret;
 }
Beispiel #8
0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    /* Variable Declarations */
    double *s, *y, *g, *H, *d, *ro, *alpha, *beta, *q, *r;
    int nVars,nSteps,lhs_dims[2];
    double temp;
    int i,j;
    
    /* Get Input Pointers */
	g = mxGetPr(prhs[0]);
    s = mxGetPr(prhs[1]);
    y = mxGetPr(prhs[2]);
    H = mxGetPr(prhs[3]);
    
    /* Compute number of variables (p), rank of update (d) */    
    nVars = mxGetDimensions(prhs[1])[0];
    nSteps = mxGetDimensions(prhs[1])[1];

    /* Set-up Output Vector */    
    lhs_dims[0] = nVars;
    lhs_dims[1] = 1;
    
    plhs[0] = mxCreateNumericArray(2,lhs_dims,mxDOUBLE_CLASS,mxREAL);
    d = mxGetPr(plhs[0]);
    
    lbfgs(g, s, y, H, nVars, nSteps, d);    
}
Beispiel #9
0
Datei: pr.c Projekt: nionjo/dm
int  learn_regression(Regression * regression){
    PR * pr = (PR*)regression;
    if (pr->p.method == 2){
        lbfgs(pr, pr_eval, pr_grad, pr_repo, 5, pr->c, pr->p.niters, pr->x);
    }
    else if (pr->p.method == 1){
        owlqn(pr, pr_eval, pr_grad, pr_repo, 5, pr->c, pr->p.niters, pr->p.lambda, pr->x);
    }
    return 0;

}
Beispiel #10
0
double Inference::get_bright_logprob(const int spot_index)
{
    active_spot_index = spot_index;
    // set E = 1 before optimize this spot
    frame.E[active_spot_index] = 1;
    
    int N = 3;
    double logp;
    double * x = new double[N];
    if(x==NULL)
    {
	std::cout<<"Allocating storage FAILED!"<< "\n";
	return -1;
    }

    for (int i=0; i<N; i++)
    {
	x[i] = 1.0;
    }
    lbfgs_parameter_t param;
    lbfgs_parameter_init(&param);
    param.m = 10;
    //param.epsilon = 1e-5;
    param.max_iterations = 20000;
    param.linesearch = LBFGS_LINESEARCH_BACKTRACKING_WOLFE;
    int status = lbfgs(N,x,&logp,inner_evaluate,inner_progress,this,&param);
    if (status == 0)
    {
	printf("Inner L-BFGS optimization terminated with status code = %d, logp=%f\n",status, logp);
	//getchar();
    }
    else
    {
	printf("Inner L-BFGS optimization terminated with status code = %d, logp=%f\n",status, logp);
	getchar();
    }

    // make sure to set E = 0 before exit to ensure that 
    // we don't the bright spot set. However, it will change
    // the parameters A, B, and phi.
    frame.E[active_spot_index] = 0;

    // double logp_piece;
    // double logp = 0.0;
    // for(std::vector<Evidence>::iterator iter = evidence_list.begin(); 
    // 	iter != evidence_list.end(); iter++)
    // {
    // 	logp_piece = 
    // 	    iter->s[active_spot_index] * log(frame.mu[active_spot_index])
    // 	    - frame.mu[active_spot_index];
    // 	logp = logp + logp_piece;
    // }
    return logp;
}
Beispiel #11
0
int LbfgsWrap::run()
{
	lbfgsfloatval_t fx;
	if (m_x)
	{
		lbfgs_free(m_x);
	}
	m_x = lbfgs_malloc(m_unknow_x.rows() * m_unknow_x.cols());
	
	if (!m_x)
	{
		EAGLEEYE_ERROR("failed to allocate a memory block for variables. \n");
		return 1;
	}

	int rows = m_unknow_x.rows();
	int cols = m_unknow_x.cols();
	for (int i = 0; i < rows; ++i)
	{
		float* tr_data = m_unknow_x.row(i);
		for (int j = 0; j < cols; ++j)
		{
			m_x[i * cols + j] = tr_data[j];
		}
	}

	/*
        Start the L-BFGS optimization; this will invoke the callback functions
        evaluate() and progress() when necessary.
     */
	int ret = lbfgs(rows * cols,m_x,&fx,_evaluate,_progress,this,&m_param);

	for (int i = 0; i < rows; ++i)
	{
		float* tr_data = m_unknow_x.row(i);
		for (int j = 0; j < cols; ++j)
		{
			tr_data[j] = float(m_x[i * cols + j]);
		}
	}

	//report the result
	EAGLEEYE_INFO("L-BFGS optimization terminated with status code = %d\n", ret);
	EAGLEEYE_INFO("loss = %f",fx);

	return ret;
}
Beispiel #12
0
 int run(int N) {
   lbfgsfloatval_t fx;
   lbfgsfloatval_t *m_x = lbfgs_malloc(N);
   if (m_x == NULL) {
       printf("ERROR: Failed to allocate a memory block for variables.\n");
       return 1;
   };
   /* Initialize the variables. */
   for (int i = 0;i < N; ++i) {
       m_x[i] = -1.2;
   };
   /*
       Start the L-BFGS optimization; this will invoke the callback functions
       evaluate() and progress() when necessary.
    */
   int ret = lbfgs(N, m_x, &fx, _evaluate, _progress, this, NULL);
   /* Report the result. */
   printf("L-BFGS optimization terminated with status code = %d\n", ret);
   printf("  fx = %f, x[0] = %f, x[1] = %f\n", fx, m_x[0], m_x[1]);
   return ret;
 };
Beispiel #13
0
int main(int argc, char* argv[]) {
  int i, ret = 0;
  double fx;
  double* x = vecalloc(N);
  lbfgs_parameter_t param;

  /* Initialize the variables. */
  for (i = 0; i < N; i += 2) {
    x[i] = -1.2;
    x[i + 1] = 1.0;
  }

  lbfgs_default_parameter(&param);
  param.orthantwise_c = 1.0;
  param.linesearch = LBFGS_LINESEARCH_BACKTRACKING_WOLFE;
  ret = lbfgs(N, x, &fx, evaluate, 0, 0, &param);
  vecfree(x);

  printf("L-BFGS optimization terminated with status code = %d\n", ret);
  return ret;
}
Beispiel #14
0
int
main(int argc, char* argv[])
{
    int i, n, status;
    nlp_float *x;
    function_object func_obj;
    linesearch_parameter ls_parameter;
    lbfgs_parameter parameter;

    n = 10;
    x = (nlp_float *)malloc_vec(n * sizeof(nlp_float));
    for (i = 0; i < n; ++i)
        x[i] = 1.;

    func_obj.func = func;
    func_obj.grad = grad;

    default_lbfgs_parameter(&parameter);
    default_linesearch_parameter(&ls_parameter);

    status = lbfgs(
        x,
        n,
        &func_obj,
        &ls_parameter,
        &parameter
    );

    if (LBFGS_SATISFIED == status)
    {
        printf("\n=== Optimal x =========================================\n");
        for (i = 0; i < n; ++i)
            printf(" x_%-8d = %12.6e\n", i + 1 , x[i]);
        printf("=======================================================\n");
    }

    free_vec(x);
    return 0;
}
Beispiel #15
0
Matrix *runLbfgsOptim(Matrix *y, doubleVector *target, int L, int jobs, double epsilon) {
    int M = y->nrow;
    Matrix *w = createZeroMatrix(M, L);
    
    lbfgsfloatval_t fx;
    lbfgsfloatval_t *x = lbfgs_malloc(w->nrow*w->ncol);
    lbfgs_parameter_t param;

    if (x == NULL) {
        printf("ERROR: Failed to allocate a memory block for variables.\n");
        exit(1);
    }

    /* Initialize the variables. */
    for (size_t i = 0; i < w->nrow; i++) {
        for (size_t j = 0; j < w->ncol;j++) {
            x[j*w->nrow + i] = getMatrixElement(w, i, j);
        }    
    }
    lbfgs_parameter_init(&param);
    param.epsilon = epsilon;
    param.m = 20;

    LbfgsInput inp;
    inp.target = target;
    inp.y = y;
    inp.jobs = jobs;
    inp.L = w->ncol;
    int ret = lbfgs(w->nrow*w->ncol, x, &fx, evaluate, progress, (void*)&inp, &param);

    printf("L-BFGS optimization terminated with status code = %d\n", ret);
    printf("  fx = %f\n", fx);
    Matrix *w_opt = formMatrixFromFloatVal(x, w->nrow, w->ncol);
    
    lbfgs_free(x);

    return(w_opt);
}
Beispiel #16
0
void testSoftICADriver()
{
  const int numPatches = 200000; // 200000 10000
  const int patchWidth = 8;
  MNISTSamplePatchesDataFunction mnistdf(numPatches, patchWidth);
  Config config;
  config.setValue("addBiasTerm", false);
  config.setValue("meanStddNormalize", false);
  config.setValue("configurePolicyTesting", false);
  config.setValue("trainingMeanAndStdd", false);
  updateMNISTConfig(config);

  const int numFeatures = 50;
  const double lambda = 0.0005f;
  const double epsilon = 1e-2;

  SoftICACostFunction sfc(numFeatures, lambda, epsilon);

  LIBLBFGSOptimizer lbfgs(1000);
  Driver drv(&config, &mnistdf, &sfc, &lbfgs);
  drv.drive();

}
Beispiel #17
0
int main(int argc, char *argv[])
{
    int i, ret = 0;
    int n = 2;
    lbfgsfloatval_t fx;
    lbfgsfloatval_t *x = lbfgs_malloc(n);
    lbfgs_parameter_t param;

    if (!x) {
        printf("ERROR: Failed to allocate a memory block for variables.\n");
        return 1;
    }

    for (i = 0; i < n; i++) {
        x[i] = 10;
    }

    /* Initialize the parameters for the L-BFGS optimization. */
    lbfgs_parameter_init(&param);
    //param.linesearch = LBFGS_LINESEARCH_BACKTRACKING_STRONG_WOLFE;

    ret = lbfgs(n, x, &fx, evaluate, progress, NULL, &param);

    printf("L-BFGS optimization terminated with status code = %d\n", ret);
    printf("  fx = %f; ", fx);
    for (i = 0; i < n; i ++){
        printf("x[%d] = %f; ", i, x[i]);
    }
    printf("\n");

    /* Answer according to Wolfram Alpha:
     *  1.00607 x^2 - 0.363643 x + 0.554
     */

    lbfgs_free(x);
    return 0;
}
Beispiel #18
0
Datei: RAE.cpp Projekt: zerkh/RAE
void RAE::training()
{
	x = lbfgs_malloc(getRAEWeightSize());
	Map<MatrixLBFGS>(x, getRAEWeightSize(), 1).setRandom();
	lbfgs_parameter_t param;
	iterTimes = atoi(para->getPara("IterationTime").c_str());

	loadTrainingData();
	lbfgs_parameter_init(&param);
	param.max_iterations = iterTimes;

	lbfgsfloatval_t fx = 0;
	int ret;

	ret = lbfgs(getRAEWeightSize(), x, &fx, RAELBFGS::evaluate, RAELBFGS::progress, this, &param);

	cout << "L-BFGS optimization terminated with status code = " << ret << endl;
	cout << " fx = " << fx << endl;

	updateWeights(x);
	logWeights(para);
	trainingData.clear();
	lbfgs_free(x);
}
Eigen::VectorXf minimizeLBFGS( EnergyFunction & efun, int restart, bool verbose ) {
	Eigen::VectorXf x0 = efun.initialValue();
	const int n = x0.rows();
	
	lbfgsfloatval_t *x = lbfgs_malloc(n);
	if (x == NULL) {
		printf("ERROR: Failed to allocate a memory block for variables.\n");
		return x0;
	}
	std::copy( x0.data(), x0.data()+n, x );
	
	lbfgs_parameter_t param;
	lbfgs_parameter_init(&param);
	// You might want to adjust the parameters to your problem
	param.epsilon = 1e-6;
	param.max_iterations = 50;
	
	double last_f = 1e100;
	int ret;
	for( int i=0; i<=restart; i++ ) {
		lbfgsfloatval_t fx;
		ret = lbfgs(n, x, &fx, evaluate, verbose?progress:NULL, &efun, &param);
		if( last_f > fx )
			last_f = fx;
		else
			break;
	}
	
	if ( verbose ) {
		printf("L-BFGS optimization terminated with status code = %d\n", ret);
	}
	
	std::copy( x, x+n, x0.data() );
	lbfgs_free(x);
	return x0;
}
Beispiel #20
0
void testStlDriver()
{
  const int numPatches = 200000; // 200000
  const int patchWidth = 9;

  const int numFeatures = 50;
  const double lambda = 0.0005f;
  const double epsilon = 1e-2;
  Config config;
  config.setValue("addBiasTerm", false);
  config.setValue("meanStddNormalize", false);
  config.setValue("configurePolicyTesting", false);
  config.setValue("trainingMeanAndStdd", false);
  updateMNISTConfig(config);

  if (false)
  {
    MNISTSamplePatchesUnlabeledDataFunction mnistUnlabeled(numPatches, patchWidth);
    SoftICACostFunction sfc(numFeatures, lambda, epsilon);

    LIBLBFGSOptimizer lbfgs(200); // 1000
    Driver drv1(&config, &mnistUnlabeled, &sfc, &lbfgs);
    const Vector_t optThetaRica = drv1.drive();

    Matrix_t Wrica(
        Eigen::Map<const Matrix_t>(optThetaRica.data(), numFeatures, pow(patchWidth, 2)));

    std::ofstream ofs_wrica("../W2.txt");
    ofs_wrica << Wrica << std::endl;
  }

  Matrix_t Wrica;
  // debug: read off the values
  std::ifstream in("/home/sam/School/online/stanford_dl_ex/W2.txt");
  if (in.is_open())
  {
    std::string str;
    int nbRows = 0;
    while (std::getline(in, str))
    {
      if (str.size() == 0)
        continue;
      std::istringstream iss(str);
      std::vector<double> tokens //
      { std::istream_iterator<double> { iss }, std::istream_iterator<double> { } };
      Wrica.conservativeResize(nbRows + 1, tokens.size());
      for (size_t i = 0; i < tokens.size(); ++i)
        Wrica(nbRows, i) = tokens[i];
      ++nbRows;
    }
  }
  else
  {
    std::cerr << "file W.txt failed" << std::endl;
    exit(EXIT_FAILURE);
  }

  const int imageDim = 28;
  Eigen::Vector2i imageConfig;
  imageConfig << imageDim, imageDim;

  const int numFilters = numFeatures;
  const int poolDim = 5;
  const int filterDim = patchWidth;
  const int convDim = (imageDim - filterDim + 1);
  assert(convDim % poolDim == 0);
  const int outputDim = (convDim / poolDim);

  StlFilterFunction stlFilterFunction(filterDim, Wrica);
  SigmoidFunction sigmoidFunction;
  ConvolutionFunction convolutionFunction(&stlFilterFunction, &sigmoidFunction);
  MeanPoolFunction meanPoolFunction(numFilters, outputDim);

  MNISTSamplePatchesLabeledDataFunction mnistLabeled(&convolutionFunction, &meanPoolFunction,
      imageConfig, numFilters, poolDim, outputDim);

  SoftmaxCostFunction mnistcf(0.01f);
  LIBLBFGSOptimizer lbfgs2(300);
  config.setValue("configurePolicyTesting", false);
  config.setValue("trainingMeanAndStdd", true);
  config.setValue("meanStddNormalize", true);
  config.setValue("addBiasTerm", true);

  config.setValue("numGrd", true);
  config.setValue("training_accuracy", true);
  config.setValue("testing_accuracy", true);
  //config.setValue("addBiasTerm", false);
  Driver drv2(&config, &mnistLabeled, &mnistcf, &lbfgs2);
  drv2.drive();

}
Beispiel #21
0
void EstimatePairModelMAP(numeric_t *x, numeric_t *lambdas, alignment_t *ali,
    options_t *options) {
    /* Computes Maximum a posteriori (MAP) estimates for the parameters of 
       and undirected graphical model by L-BFGS */

    /* Start timer */
    gettimeofday(&ali->start, NULL);

    /* Initialize L-BFGS */
    lbfgs_parameter_t param;
    lbfgs_parameter_init(&param);
    param.epsilon = 1E-3;
    param.max_iterations = options->maxIter; /* 0 is unbounded */

    /* Array of void pointers provides relevant data structures */
    void *d[3] = {(void *)ali, (void *)options, (void *)lambdas};

    /* Estimate parameters by optimization */
    static lbfgs_evaluate_t algo;
    switch(options->estimatorMAP) {
        case INFER_MAP_PLM:
            algo = PLMNegLogPosterior;
            break;
        case INFER_MAP_PLM_GAPREDUCE:
            algo = PLMNegLogPosteriorGapReduce;
            break;
        case INFER_MAP_PLM_BLOCK:
            algo = PLMNegLogPosteriorBlock;
            break;
        case INFER_MAP_PLM_DROPOUT:
            algo = PLMNegLogPosteriorDO;
            break;
        default:
            algo = PLMNegLogPosterior;
    }

    if (options->zeroAPC == 1) fprintf(stderr,
            "Estimating coupling hyperparameters le = 1/2 inverse variance\n");

    int ret = 0;
    lbfgsfloatval_t fx;
    ret = lbfgs(ali->nParams, x, &fx, algo, ReportProgresslBFGS,
        (void*)d, &param);
    fprintf(stderr, "Gradient optimization: %s\n", LBFGSErrorString(ret));

    /* Optionally re-estimate parameters with adjusted hyperparameters */
    if (options->zeroAPC == 1) {
        /* Form new priors on the variances */
        ZeroAPCPriors(ali, options, lambdas, x);

        /* Reinitialize coupling parameters */
        for (int i = 0; i < ali->nSites - 1; i++)
            for (int j = i + 1; j < ali->nSites; j++)
                for (int ai = 0; ai < ali->nCodes; ai++)
                    for (int aj = 0; aj < ali->nCodes; aj++)
                        xEij(i, j, ai, aj) = 0.0;

        /* Iterate estimation with new hyperparameter estimates */
        options->zeroAPC = 2;
        ret = lbfgs(ali->nParams, x, &fx, algo,
            ReportProgresslBFGS, (void*)d, &param);
        fprintf(stderr, "Gradient optimization: %s\n", LBFGSErrorString(ret));
    }
}
Beispiel #22
0
/**
 * @brief Use the open source implement of OWL-QN to optimize the feature weights.
 *
 * @param weights  feature weights
 */
void MStep_Trainer::OptimizeFeatureWeights_jp(vector<double>& weights)
{

    //if expect_count==0 L1 norm LBFGS error!!
    for(int i=0;i<this->m_scfg.phrase_rules_count;i++)
    {
        if(this->m_scfg.phrase_rules[i]->expect_count==0)
        {
            this->m_scfg.phrase_rules[i]->expect_count=1e-20;
            this->m_scfg.N_cnt[3]+=1e-20;
        }
    }

    cout<<"begin optimize the lambda!"<<endl;
    int N = FeatureManager::GetSingleton().GetFeatureCount();
    cout<<"the number of features is: "<<N<<endl;
    int  ret = 0;
    lbfgsfloatval_t fx=0;
    lbfgsfloatval_t *x = lbfgs_malloc(N);
    lbfgs_parameter_t param;

    if (x == NULL) {
        cout<<"ERROR: Failed to allocate a memory block for variables"<<endl;
    }

    /* Initialize the variables. */
    if(weights.size()>0)
    {
        for(int i=0;i<N;i++)
        {
            x[i]= weights[i];
        }
    }

    /* Initialize the parameters for the L-BFGS optimization. */
    lbfgs_parameter_init(&param);
    /*param.linesearch = LBFGS_LINESEARCH_BACKTRACKING;*/

    /*
       Start the L-BFGS optimization; this will invoke the callback functions
       evaluate() and progress() when necessary.
       */

    //we pre compute some value that used in gradient function 
    LBFGS_Param* lbfgs_param=new LBFGS_Param();
    lbfgs_param->scfg=&this->m_scfg;
    //int thread_count = this->config->configfile->read<int>("thread_count");
    double L2_coefficient=this->m_pconfig->configfile->read<double>("L2_coefficient");

    lbfgs_param->L2_coefficient= L2_coefficient;
    PreComputeLBFGS_Param(lbfgs_param,N);

    int max_iteration = this->m_pconfig->configfile->read<int>("max_iteration");
    int past=this->m_pconfig->configfile->read<int>("past");
    double delta=this->m_pconfig->configfile->read<double>("delta");
    int linesearch =this->m_pconfig->configfile->read<int>("linesearch");
    double gtol =this->m_pconfig->configfile->read<double>("gtol");
    double epsilon=this->m_pconfig->configfile->read<double>("epsilon");
    double L1_coefficient = this->m_pconfig->configfile->read<double>("L1_coefficient");
    int orthantwise_start = this->m_pconfig->configfile->read<int>("orthantwise_start");

    param.orthantwise_start = orthantwise_start;
    param.orthantwise_end = N-1;
    param.orthantwise_c=L1_coefficient;
    param.linesearch = linesearch;
    param.max_linesearch = 20;
    param.max_iterations =max_iteration;
    param.past=past;
    param.delta=delta;
    param.epsilon=epsilon;
    param.gtol=gtol;

    ret = lbfgs(N, x, &fx, evaluate_main, progress, static_cast<void*>(lbfgs_param), &param);

    Loger::LogTime();
    Loger::mylogfile<<"L-BFGS optimization terminated with status code = "<<ret<<endl;
    Loger::mylogfile<<"L-BFGS optimization log value fx = "<<fx<<endl;
    cout<<"L-BFGS optimization terminated with status code = "<<ret<<endl;
    cout<<"L-BFGS optimization log value fx = "<<fx<<endl;


    weights.clear();
    weights.resize(N,0);

    for(int i=0;i<N;i++)
    {
        weights[i]=x[i];
    }
    lbfgs_free(x);
}