void GPOptimizerRprop::maximize(GPRegression* gp, int const& n, bool const& verbose) { int const& n_dim_Y = gp->yDimension(); for (int d = 0; d < n_dim_Y; ++d) { KernelFunction* Kf = gp->kernelFunction(d); int const& n_dim_param = Kf->parameterDimension(); VecN delta = VecN::Ones(n_dim_param) * _p->_delta0; VecN grad_old = VecN::Zero(n_dim_param); VecN params = Kf->logParameters(); VecN best_params = params; Scalar best = log(0); for (int i = 0; i < n; ++i) { VecN grad = -gp->logLikelihoodGrad(d); grad_old = grad_old.cwiseProduct(grad); for (int j = 0; j < grad_old.size(); ++j) { if (grad_old(j) > 0) { delta(j) = std::min(delta(j) * _p->_eta_plus, _p->_delta_max); } else if (grad_old(j) < 0) { delta(j) = std::max(delta(j) * _p->_eta_minus, _p->_delta_min); grad(j) = 0; } params(j) += -_p->sign(grad(j)) * delta(j); } grad_old = grad; if (grad_old.norm() < _p->_eps_stop) break; Kf->initLogParameters(params); Scalar lik = gp->logLikelihood(d); if (verbose) std::cout << i << " " << -lik << std::endl; if (lik > best) { best = lik; best_params = params; } } Kf->initLogParameters(best_params); } }
void RpropSolver::internalSolve(Vector & x0, const function_t & FunctionValue, const gradient_t & FunctionGradient, const hessian_t & FunctionHessian) { //helper function auto sign = [](double x) -> double { if (x>0) return 1.0; if (x<0) return -1.0; return 0.0; }; const size_t DIM = x0.rows(); Vector Delta = Eigen::VectorXd::Ones ( DIM ) * Delta0; //step size Vector Delta_old = Eigen::VectorXd::Ones ( DIM ) * Delta0; //step size Vector grad = Eigen::VectorXd::Zero ( DIM ); Vector grad_old = Eigen::VectorXd::Zero ( DIM ); Vector params = x0; // hyperparameter //Vector params_old = x0; Vector best_params = params; double best = std::numeric_limits<double>::infinity(); size_t iter = 0; do { FunctionGradient(params, grad); // calc gradient auto FunctionValue_this = FunctionValue(params); grad_old = grad_old.cwiseProduct ( grad ); auto params_new = params; for ( int j = 0; j < grad_old.size(); ++j ) { if ( grad_old ( j ) > 0 ) // no sign change { Delta ( j ) = std::min ( Delta ( j ) * etaplus, Deltamax ); } else if ( grad_old ( j ) < 0 ) // sign change { Delta ( j ) = std::max ( Delta ( j ) * etaminus, Deltamin ); } params_new ( j ) += -sign(grad (j)) * Delta (j); } params = std::move(params_new); //std::cout << best << std::endl; //std::cout << iter << " "<< FunctionValue_this << std::endl; FunctionValueHistory.push_back(FunctionValue_this); x0History.push_back(x0); if ( FunctionValue_this < best ) { best = FunctionValue_this; best_params = params; } grad_old = grad; //params_old = params; Delta_old = Delta; ++iter; } //while((grad_old.lpNorm<Eigen::Infinity>() > settings.gradTol) && (iter < settings.maxIter)); // if ( grad_old.norm() < eps_stop ) break; while((iter < settings.maxIter)); // if ( grad_old.norm() < eps_stop ) break; // save best hyperparameters double temp = FunctionValue(best_params); }