-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from arvigj/adam
Add ADAM optimizer
- Loading branch information
Showing
5 changed files
with
211 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// ADAM from "ADAM: A METHOD FOR STOCHASTIC OPTIMIZATION" | ||
|
||
#include "ADAM.hpp" | ||
|
||
namespace polysolve::nonlinear | ||
{ | ||
|
||
ADAM::ADAM(const json &solver_params, | ||
const bool is_stochastic, | ||
const double characteristic_length, | ||
spdlog::logger &logger) | ||
: Superclass(solver_params, characteristic_length, logger), is_stochastic_(is_stochastic) | ||
{ | ||
std::string param_name = is_stochastic ? "StochasticADAM" : "ADAM"; | ||
alpha_ = solver_params[param_name]["alpha"]; | ||
beta_1_ = solver_params[param_name]["beta_1"]; | ||
beta_2_ = solver_params[param_name]["beta_2"]; | ||
epsilon_ = solver_params[param_name]["epsilon"]; | ||
if (is_stochastic) | ||
erase_component_probability_ = solver_params["StochasticADAM"]["erase_component_probability"]; | ||
} | ||
|
||
void ADAM::reset(const int ndof) | ||
{ | ||
Superclass::reset(ndof); | ||
m_prev_ = Eigen::VectorXd::Zero(ndof); | ||
v_prev_ = Eigen::VectorXd::Zero(ndof); | ||
t_ = 0; | ||
} | ||
|
||
bool ADAM::compute_update_direction( | ||
Problem &objFunc, | ||
const TVector &x, | ||
const TVector &grad, | ||
TVector &direction) | ||
{ | ||
if (m_prev_.size() == 0) | ||
m_prev_ = Eigen::VectorXd::Zero(x.size()); | ||
if (v_prev_.size() == 0) | ||
v_prev_ = Eigen::VectorXd::Zero(x.size()); | ||
|
||
TVector grad_modified = grad; | ||
|
||
if (is_stochastic_) | ||
{ | ||
Eigen::VectorXd mask = (Eigen::VectorXd::Random(direction.size()).array() + 1.) / 2.; | ||
for (int i = 0; i < direction.size(); ++i) | ||
grad_modified(i) *= (mask(i) < erase_component_probability_) ? 0. : 1.; | ||
} | ||
|
||
TVector m = (beta_1_ * m_prev_) + ((1 - beta_1_) * grad_modified); | ||
TVector v = beta_2_ * v_prev_; | ||
for (int i = 0; i < v.size(); ++i) | ||
v(i) += (1 - beta_2_) * grad_modified(i) * grad_modified(i); | ||
|
||
m = m.array() / (1 - pow(beta_1_, t_)); | ||
v = v.array() / (1 - pow(beta_2_, t_)); | ||
|
||
direction = -alpha_ * m; | ||
for (int i = 0; i < v.size(); ++i) | ||
direction(i) /= sqrt(v(i) + epsilon_); | ||
|
||
++t_; | ||
|
||
return true; | ||
} | ||
} // namespace polysolve::nonlinear |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#pragma once | ||
|
||
#include "DescentStrategy.hpp" | ||
#include <polysolve/Utils.hpp> | ||
|
||
#include <polysolve/linear/Solver.hpp> | ||
|
||
namespace polysolve::nonlinear | ||
{ | ||
class ADAM : public DescentStrategy | ||
{ | ||
public: | ||
using Superclass = DescentStrategy; | ||
|
||
ADAM(const json &solver_params, | ||
const bool is_stochastic, | ||
const double characteristic_length, | ||
spdlog::logger &logger); | ||
|
||
std::string name() const override { return is_stochastic_ ? "StochasticADAM" : "ADAM"; } | ||
|
||
void reset(const int ndof) override; | ||
|
||
virtual bool compute_update_direction( | ||
Problem &objFunc, | ||
const TVector &x, | ||
const TVector &grad, | ||
TVector &direction) override; | ||
|
||
bool is_direction_descent() override { return false; } | ||
|
||
private: | ||
TVector m_prev_; | ||
TVector v_prev_; | ||
|
||
double beta_1_, beta_2_; | ||
double alpha_; | ||
|
||
int t_ = 0; | ||
double epsilon_; | ||
|
||
bool is_stochastic_; | ||
double erase_component_probability_ = 0; | ||
}; | ||
} // namespace polysolve::nonlinear |
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