Skip to content

Commit e6fd577

Browse files
authored
Upgrade ensmallen 2 10 0 (#14)
* Upgrade ensmallen to 2.10.0 * Update version and armadillo dependency * Update package description * Add NEWS.md update * Add changelog update
1 parent 0bafbaa commit e6fd577

File tree

209 files changed

+10282
-4673
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

209 files changed

+10282
-4673
lines changed

ChangeLog

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
2019-09-07 James Balamuta <[email protected]>
2+
3+
* DESCRIPTION (Version, Date): Release 2.10.0 and armadillo dependency
4+
5+
* NEWS.md: Update for Ensmallen release 2.10.0
6+
7+
* inst/include/ensmallen_bits: Upgraded to Ensmallen 2.10.0
8+
* inst/include/ensmallen.hpp: ditto
9+
110
2019-08-09 James Balamuta <[email protected]>
211

312
* DESCRIPTION (Version, Date): Release 1.16.0

DESCRIPTION

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: RcppEnsmallen
22
Title: Header-Only C++ Mathematical Optimization Library for 'Armadillo'
3-
Version: 0.1.16.0.1
3+
Version: 0.2.10.0.1
44
Authors@R: c(
55
person("James Joseph", "Balamuta", email = "[email protected]",
66
role = c("aut", "cre", "cph"),
@@ -22,14 +22,14 @@ Description: 'Ensmallen' is a templated C++ mathematical optimization library
2222
'RcppArmadillo' (the 'Rcpp' bindings/bridge to 'Armadillo') is licensed under
2323
the GNU GPL version 2 or later. Thus, 'RcppEnsmallen' is also licensed under
2424
similar terms. Note that 'Ensmallen' requires a compiler that supports
25-
'C++11' and 'Armadillo' 6.500 or later.
25+
'C++11' and 'Armadillo' 8.400 or later.
2626
Depends: R (>= 3.3.0)
2727
License: GPL (>= 2)
2828
URL: https://github.com/coatless/rcppensmallen, https://github.com/mlpack/ensmallen, http://ensmallen.org/
2929
BugReports: https://github.com/coatless/rcppensmallen/issues
3030
Encoding: UTF-8
3131
LazyData: true
32-
LinkingTo: Rcpp, RcppArmadillo
32+
LinkingTo: Rcpp, RcppArmadillo (>= 0.8.400.0.0)
3333
Imports: Rcpp
3434
RoxygenNote: 6.1.1
3535
Roxygen: list(markdown = TRUE)

NEWS.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# RcppEnsmallen 0.2.10.0.1
2+
3+
- Upgraded to ensmallen 2.10.0: "Fried Chicken" (2019-09-07)
4+
- All `Optimize()` functions now take any matrix type; so, e.g., `arma::fmat`
5+
or `arma::sp_mat` can be used for optimization. See the documentation for
6+
more details ([#113](https://github.com/mlpack/ensmallen/pull/113),
7+
[#119](https://github.com/mlpack/ensmallen/pull/119)).
8+
- Introduce callback support. Callbacks can be appended as the last arguments
9+
of an `Optimize()` call, and can perform custom behavior at different points
10+
during the optimization. See the documentation for more details
11+
([#119](https://github.com/mlpack/ensmallen/pull/119)).
12+
- Slight speedups for `FrankWolfe` optimizer
13+
([#127](https://github.com/mlpack/ensmallen/pull/127)).
14+
115
# RcppEnsmallen 0.1.16.0.1
216

317
- Upgraded to ensmallen release 1.16.0 "Loud Alarm Clock" (2019-08-09)

inst/include/ensmallen.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@
5959
#include "ensmallen_bits/ens_version.hpp"
6060
#include "ensmallen_bits/log.hpp" // TODO: should move to another place
6161

62+
#include "ensmallen_bits/utility/any.hpp"
63+
#include "ensmallen_bits/utility/arma_traits.hpp"
64+
65+
// Callbacks.
66+
#include "ensmallen_bits/callbacks/callbacks.hpp"
67+
#include "ensmallen_bits/callbacks/early_stop_at_min_loss.hpp"
68+
#include "ensmallen_bits/callbacks/print_loss.hpp"
69+
#include "ensmallen_bits/callbacks/progress_bar.hpp"
70+
#include "ensmallen_bits/callbacks/store_best_coordinates.hpp"
71+
#include "ensmallen_bits/callbacks/timer_stop.hpp"
72+
6273
#include "ensmallen_bits/problems/problems.hpp" // TODO: should move to another place
6374

6475
#include "ensmallen_bits/ada_delta/ada_delta.hpp"

inst/include/ensmallen_bits/ada_delta/ada_delta.hpp

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,40 @@ class AdaDelta
8585
* objective value is returned. The DecomposableFunctionType is checked for
8686
* API consistency at compile time.
8787
*
88-
* @tparam DecomposableFunctionType Type of the function to optimize.
88+
* @tparam DecomposableFunctionType Type of the function to be optimized.
89+
* @tparam MatType Type of matrix to optimize with.
90+
* @tparam GradType Type of matrix to use to represent function gradients.
91+
* @tparam CallbackTypes Types of callback functions.
8992
* @param function Function to optimize.
9093
* @param iterate Starting point (will be modified).
94+
* @param callbacks Callback functions.
9195
* @return Objective value of the final point.
9296
*/
93-
template<typename DecomposableFunctionType>
94-
double Optimize(DecomposableFunctionType& function, arma::mat& iterate)
97+
template<typename DecomposableFunctionType,
98+
typename MatType,
99+
typename GradType,
100+
typename... CallbackTypes>
101+
typename std::enable_if<IsArmaType<GradType>::value,
102+
typename MatType::elem_type>::type
103+
Optimize(DecomposableFunctionType& function,
104+
MatType& iterate,
105+
CallbackTypes&&... callbacks)
95106
{
96-
return optimizer.Optimize(function, iterate);
107+
return optimizer.Optimize<DecomposableFunctionType, MatType, GradType,
108+
CallbackTypes...>(function, iterate, callbacks...);
109+
}
110+
111+
//! Forward the MatType as GradType.
112+
template<typename DecomposableFunctionType,
113+
typename MatType,
114+
typename... CallbackTypes>
115+
typename MatType::elem_type Optimize(DecomposableFunctionType& function,
116+
MatType& iterate,
117+
CallbackTypes&&... callbacks)
118+
{
119+
return Optimize<DecomposableFunctionType, MatType, MatType,
120+
CallbackTypes...>(function, iterate,
121+
std::forward<CallbackTypes>(callbacks)...);
97122
}
98123

99124
//! Get the step size.

inst/include/ensmallen_bits/ada_delta/ada_delta_update.hpp

Lines changed: 65 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -51,49 +51,6 @@ class AdaDeltaUpdate
5151
// Nothing to do.
5252
}
5353

54-
/**
55-
* The Initialize method is called by SGD Optimizer method before the start of
56-
* the iteration update process. In AdaDelta update policy, the mean squared
57-
* and the delta mean squared gradient matrices are initialized to the zeros
58-
* matrix with the same size as gradient matrix (see ens::SGD<>).
59-
*
60-
* @param rows Number of rows in the gradient matrix.
61-
* @param cols Number of columns in the gradient matrix.
62-
*/
63-
void Initialize(const size_t rows, const size_t cols)
64-
{
65-
// Initialize empty matrices for mean sum of squares of parameter gradient.
66-
meanSquaredGradient = arma::zeros<arma::mat>(rows, cols);
67-
meanSquaredGradientDx = arma::zeros<arma::mat>(rows, cols);
68-
}
69-
70-
/**
71-
* Update step for SGD. The AdaDelta update dynamically adapts over time using
72-
* only first order information. Additionally, AdaDelta requires no manual
73-
* tuning of a learning rate.
74-
*
75-
* @param iterate Parameters that minimize the function.
76-
* @param stepSize Step size to be used for the given iteration.
77-
* @param gradient The gradient matrix.
78-
*/
79-
void Update(arma::mat& iterate,
80-
const double stepSize,
81-
const arma::mat& gradient)
82-
{
83-
// Accumulate gradient.
84-
meanSquaredGradient *= rho;
85-
meanSquaredGradient += (1 - rho) * (gradient % gradient);
86-
arma::mat dx = arma::sqrt((meanSquaredGradientDx + epsilon) /
87-
(meanSquaredGradient + epsilon)) % gradient;
88-
89-
// Accumulate updates.
90-
meanSquaredGradientDx *= rho;
91-
meanSquaredGradientDx += (1 - rho) * (dx % dx);
92-
93-
// Apply update.
94-
iterate -= (stepSize * dx);
95-
}
96-
9754
//! Get the smoothing parameter.
9855
double Rho() const { return rho; }
9956
//! Modify the smoothing parameter.
@@ -104,18 +61,77 @@ class AdaDeltaUpdate
10461
//! Modify the value used to initialise the mean squared gradient parameter.
10562
double& Epsilon() { return epsilon; }
10663

64+
/**
65+
* The UpdatePolicyType policy classes must contain an internal 'Policy'
66+
* template class with two template arguments: MatType and GradType. This is
67+
* instantiated at the start of the optimization, and holds parameters
68+
* specific to an individual optimization.
69+
*/
70+
template<typename MatType, typename GradType>
71+
class Policy
72+
{
73+
public:
74+
/**
75+
* This constructor is called by the SGD optimizer method before the start
76+
* of the iteration update process. In AdaDelta update policy, the mean
77+
* squared and the delta mean squared gradient matrices are initialized to
78+
* the zeros matrix with the same size as gradient matrix (see ens::SGD<>).
79+
*
80+
* @param parent AdaDeltaUpdate object.
81+
* @param rows Number of rows in the gradient matrix.
82+
* @param cols Number of columns in the gradient matrix.
83+
*/
84+
Policy(AdaDeltaUpdate& parent, const size_t rows, const size_t cols) :
85+
parent(parent)
86+
{
87+
meanSquaredGradient.zeros(rows, cols);
88+
meanSquaredGradientDx.zeros(rows, cols);
89+
}
90+
91+
/**
92+
* Update step for SGD. The AdaDelta update dynamically adapts over time
93+
* using only first order information. Additionally, AdaDelta requires no
94+
* manual tuning of a learning rate.
95+
*
96+
* @param iterate Parameters that minimize the function.
97+
* @param stepSize Step size to be used for the given iteration.
98+
* @param gradient The gradient matrix.
99+
*/
100+
void Update(MatType& iterate,
101+
const double stepSize,
102+
const GradType& gradient)
103+
{
104+
// Accumulate gradient.
105+
meanSquaredGradient *= parent.rho;
106+
meanSquaredGradient += (1 - parent.rho) * (gradient % gradient);
107+
GradType dx = arma::sqrt((meanSquaredGradientDx + parent.epsilon) /
108+
(meanSquaredGradient + parent.epsilon)) % gradient;
109+
110+
// Accumulate updates.
111+
meanSquaredGradientDx *= parent.rho;
112+
meanSquaredGradientDx += (1 - parent.rho) * (dx % dx);
113+
114+
// Apply update.
115+
iterate -= (stepSize * dx);
116+
}
117+
118+
private:
119+
// The instantiated parent class.
120+
AdaDeltaUpdate& parent;
121+
122+
// The mean squared gradient matrix.
123+
GradType meanSquaredGradient;
124+
125+
// The delta mean squared gradient matrix.
126+
GradType meanSquaredGradientDx;
127+
};
128+
107129
private:
108130
// The smoothing parameter.
109131
double rho;
110132

111133
// The epsilon value used to initialise the mean squared gradient parameter.
112134
double epsilon;
113-
114-
// The mean squared gradient matrix.
115-
arma::mat meanSquaredGradient;
116-
117-
// The delta mean squared gradient matrix.
118-
arma::mat meanSquaredGradientDx;
119135
};
120136

121137
} // namespace ens

inst/include/ensmallen_bits/ada_grad/ada_grad.hpp

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,40 @@ class AdaGrad
8181
* be modified to store the finishing point of the algorithm, and the final
8282
* objective value is returned.
8383
*
84-
* @tparam DecomposableFunctionType Type of the function to optimize.
84+
* @tparam DecomposableFunctionType Type of the function to be optimized.
85+
* @tparam MatType Type of matrix to optimize with.
86+
* @tparam GradType Type of matrix to use to represent function gradients.
87+
* @tparam CallbackTypes Types of callback functions.
8588
* @param function Function to optimize.
8689
* @param iterate Starting point (will be modified).
90+
* @param callbacks Callback functions.
8791
* @return Objective value of the final point.
8892
*/
89-
template<typename DecomposableFunctionType>
90-
double Optimize(DecomposableFunctionType& function, arma::mat& iterate)
93+
template<typename DecomposableFunctionType,
94+
typename MatType,
95+
typename GradType,
96+
typename... CallbackTypes>
97+
typename std::enable_if<IsArmaType<GradType>::value,
98+
typename MatType::elem_type>::type
99+
Optimize(DecomposableFunctionType& function,
100+
MatType& iterate,
101+
CallbackTypes&&... callbacks)
91102
{
92-
return optimizer.Optimize(function, iterate);
103+
return optimizer.Optimize<DecomposableFunctionType, MatType, GradType,
104+
CallbackTypes...>(function, iterate, callbacks...);
105+
}
106+
107+
//! Forward the MatType as GradType.
108+
template<typename DecomposableFunctionType,
109+
typename MatType,
110+
typename... CallbackTypes>
111+
typename MatType::elem_type Optimize(DecomposableFunctionType& function,
112+
MatType& iterate,
113+
CallbackTypes&&... callbacks)
114+
{
115+
return Optimize<DecomposableFunctionType, MatType, MatType,
116+
CallbackTypes...>(function, iterate,
117+
std::forward<CallbackTypes>(callbacks)...);
93118
}
94119

95120
//! Get the step size.

inst/include/ensmallen_bits/ada_grad/ada_grad_update.hpp

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -49,49 +49,67 @@ class AdaGradUpdate
4949
// Nothing to do.
5050
}
5151

52-
/**
53-
* The Initialize method is called by SGD Optimizer method before the start of
54-
* the iteration update process. In AdaGrad update policy, squared
55-
* gradient matrix is initialized to the zeros matrix with the same size as
56-
* gradient matrix (see ens::SGD<>).
57-
*
58-
* @param rows Number of rows in the gradient matrix.
59-
* @param cols Number of columns in the gradient matrix.
60-
*/
61-
void Initialize(const size_t rows, const size_t cols)
62-
{
63-
// Initialize an empty matrix for sum of squares of parameter gradient.
64-
squaredGradient = arma::zeros<arma::mat>(rows, cols);
65-
}
52+
//! Get the value used to initialise the squared gradient parameter.
53+
double Epsilon() const { return epsilon; }
54+
//! Modify the value used to initialise the squared gradient parameter.
55+
double& Epsilon() { return epsilon; }
6656

6757
/**
68-
* Update step for SGD. The AdaGrad update adapts the learning rate by
69-
* performing larger updates for more sparse parameters and smaller updates
70-
* for less sparse parameters .
71-
*
72-
* @param iterate Parameters that minimize the function.
73-
* @param stepSize Step size to be used for the given iteration.
74-
* @param gradient The gradient matrix.
58+
* The UpdatePolicyType policy classes must contain an internal 'Policy'
59+
* template class with two template arguments: MatType and GradType. This is
60+
* instantiated at the start of the optimization, and holds parameters
61+
* specific to an individual optimization.
7562
*/
76-
void Update(arma::mat& iterate,
77-
const double stepSize,
78-
const arma::mat& gradient)
63+
template<typename MatType, typename GradType>
64+
class Policy
7965
{
80-
squaredGradient += (gradient % gradient);
81-
iterate -= (stepSize * gradient) / (arma::sqrt(squaredGradient) + epsilon);
82-
}
66+
public:
67+
/**
68+
* This constructor is called by the SGD optimizer before the start of the
69+
* iteration update process. In AdaGrad update policy, squared gradient
70+
* matrix is initialized to the zeros matrix with the same size as gradient
71+
* matrix (see ens::SGD<>).
72+
*
73+
* @param parent Instantiated parent class.
74+
* @param rows Number of rows in the gradient matrix.
75+
* @param cols Number of columns in the gradient matrix.
76+
*/
77+
Policy(AdaGradUpdate& parent, const size_t rows, const size_t cols) :
78+
parent(parent),
79+
squaredGradient(rows, cols)
80+
{
81+
// Initialize an empty matrix for sum of squares of parameter gradient.
82+
squaredGradient.zeros();
83+
}
8384

84-
//! Get the value used to initialise the squared gradient parameter.
85-
double Epsilon() const { return epsilon; }
86-
//! Modify the value used to initialise the squared gradient parameter.
87-
double& Epsilon() { return epsilon; }
85+
/**
86+
* Update step for SGD. The AdaGrad update adapts the learning rate by
87+
* performing larger updates for more sparse parameters and smaller updates
88+
* for less sparse parameters.
89+
*
90+
* @param iterate Parameters that minimize the function.
91+
* @param stepSize Step size to be used for the given iteration.
92+
* @param gradient The gradient matrix.
93+
*/
94+
void Update(MatType& iterate,
95+
const double stepSize,
96+
const GradType& gradient)
97+
{
98+
squaredGradient += (gradient % gradient);
99+
iterate -= (stepSize * gradient) / (arma::sqrt(squaredGradient) +
100+
parent.epsilon);
101+
}
102+
103+
private:
104+
// Instantiated parent class.
105+
AdaGradUpdate& parent;
106+
// The squared gradient matrix.
107+
GradType squaredGradient;
108+
};
88109

89110
private:
90111
// The epsilon value used to initialise the squared gradient parameter.
91112
double epsilon;
92-
93-
// The squared gradient matrix.
94-
arma::mat squaredGradient;
95113
};
96114

97115
} // namespace ens

0 commit comments

Comments
 (0)