[mlpack-git] master, mlpack-1.0.x: * Added momentum to SVD batch learning * AMF now calls Initialize on update rule before starting the optimization * Every update rule should now implement Initialize accepting data matrix and rank (7660602)
gitdub at big.cc.gt.atl.ga.us
gitdub at big.cc.gt.atl.ga.us
Thu Mar 5 21:48:48 EST 2015
Repository : https://github.com/mlpack/mlpack
On branches: master,mlpack-1.0.x
Link : https://github.com/mlpack/mlpack/compare/904762495c039e345beba14c1142fd719b3bd50e...f94823c800ad6f7266995c700b1b630d5ffdcf40
>---------------------------------------------------------------
commit 7660602835c6c19d316f4f1d480f6b5185b81517
Author: sumedhghaisas <sumedhghaisas at gmail.com>
Date: Wed Jun 11 15:17:04 2014 +0000
* Added momentum to SVD batch learning
* AMF now calls Initialize on update rule before starting the optimization
* Every update rule should now implement Initialize accepting data matrix
and rank
>---------------------------------------------------------------
7660602835c6c19d316f4f1d480f6b5185b81517
src/mlpack/methods/amf/amf.hpp | 2 +-
src/mlpack/methods/amf/amf_impl.hpp | 6 ++--
src/mlpack/methods/amf/update_rules/nmf_als.hpp | 7 +++++
.../methods/amf/update_rules/nmf_mult_dist.hpp | 7 +++++
.../methods/amf/update_rules/nmf_mult_div.hpp | 7 +++++
.../methods/amf/update_rules/svd_batchlearning.hpp | 32 ++++++++++++++++++----
6 files changed, 51 insertions(+), 10 deletions(-)
diff --git a/src/mlpack/methods/amf/amf.hpp b/src/mlpack/methods/amf/amf.hpp
index 64ef615..d505c88 100644
--- a/src/mlpack/methods/amf/amf.hpp
+++ b/src/mlpack/methods/amf/amf.hpp
@@ -83,7 +83,7 @@ class AMF
double Apply(const MatType& V,
const size_t r,
arma::mat& W,
- arma::mat& H) const;
+ arma::mat& H);
private:
//! The maximum number of iterations allowed before giving up.
diff --git a/src/mlpack/methods/amf/amf_impl.hpp b/src/mlpack/methods/amf/amf_impl.hpp
index b601596..2bd0ee9 100644
--- a/src/mlpack/methods/amf/amf_impl.hpp
+++ b/src/mlpack/methods/amf/amf_impl.hpp
@@ -43,7 +43,7 @@ double AMF<InitializationRule, UpdateRule>::Apply(
const MatType& V,
const size_t r,
arma::mat& W,
- arma::mat& H) const
+ arma::mat& H)
{
const size_t n = V.n_rows;
const size_t m = V.n_cols;
@@ -61,7 +61,7 @@ double AMF<InitializationRule, UpdateRule>::Apply(
double norm = 0;
arma::mat WH;
- std::cout << tolerance << std::endl;
+ update.Initialize(V, r);
while (((oldResidue - residue) / oldResidue >= tolerance || iteration < 4) && iteration != maxIterations)
{
@@ -84,8 +84,6 @@ double AMF<InitializationRule, UpdateRule>::Apply(
normOld = norm;
iteration++;
-
- std::cout << residue << std::endl;
}
Log::Info << "AMF converged to residue of " << sqrt(residue) << " in "
diff --git a/src/mlpack/methods/amf/update_rules/nmf_als.hpp b/src/mlpack/methods/amf/update_rules/nmf_als.hpp
index 2ad3606..a3269b4 100644
--- a/src/mlpack/methods/amf/update_rules/nmf_als.hpp
+++ b/src/mlpack/methods/amf/update_rules/nmf_als.hpp
@@ -27,6 +27,13 @@ class NMFALSUpdate
// Empty constructor required for the UpdateRule template.
NMFALSUpdate() { }
+ template<typename MatType>
+ void Initialize(const MatType& dataset, const size_t rank)
+ {
+ (void)dataset;
+ (void)rank;
+ }
+
/**
* The update rule for the basis matrix W. The formula used is
* \f[
diff --git a/src/mlpack/methods/amf/update_rules/nmf_mult_dist.hpp b/src/mlpack/methods/amf/update_rules/nmf_mult_dist.hpp
index 90c0744..c0db131 100644
--- a/src/mlpack/methods/amf/update_rules/nmf_mult_dist.hpp
+++ b/src/mlpack/methods/amf/update_rules/nmf_mult_dist.hpp
@@ -26,6 +26,13 @@ class NMFMultiplicativeDistanceUpdate
// Empty constructor required for the UpdateRule template.
NMFMultiplicativeDistanceUpdate() { }
+ template<typename MatType>
+ void Initialize(const MatType& dataset, const size_t rank)
+ {
+ (void)dataset;
+ (void)rank;
+ }
+
/**
* The update rule for the basis matrix W. The formula used is
* \f[
diff --git a/src/mlpack/methods/amf/update_rules/nmf_mult_div.hpp b/src/mlpack/methods/amf/update_rules/nmf_mult_div.hpp
index 4890643..b4d7037 100644
--- a/src/mlpack/methods/amf/update_rules/nmf_mult_div.hpp
+++ b/src/mlpack/methods/amf/update_rules/nmf_mult_div.hpp
@@ -25,6 +25,13 @@ class NMFMultiplicativeDivergenceUpdate
// Empty constructor required for the WUpdateRule template.
NMFMultiplicativeDivergenceUpdate() { }
+ template<typename MatType>
+ void Initialize(const MatType& dataset, const size_t rank)
+ {
+ (void)dataset;
+ (void)rank;
+ }
+
/**
* The update rule for the basis matrix W. The formula used is
* \f[
diff --git a/src/mlpack/methods/amf/update_rules/svd_batchlearning.hpp b/src/mlpack/methods/amf/update_rules/svd_batchlearning.hpp
index 3cd6055..c452895 100644
--- a/src/mlpack/methods/amf/update_rules/svd_batchlearning.hpp
+++ b/src/mlpack/methods/amf/update_rules/svd_batchlearning.hpp
@@ -13,9 +13,21 @@ public:
SVDBatchLearning(double u = 0.000001,
double kw = 0,
double kh = 0,
+ double momentum = 0.2,
double min = -DBL_MIN,
double max = DBL_MAX)
- : u(u), kw(kw), kh(kh), min(min), max(max) {}
+ : u(u), kw(kw), kh(kh), min(min), max(max), momentum(momentum)
+ {}
+
+ template<typename MatType>
+ void Initialize(const MatType& dataset, const size_t rank)
+ {
+ const size_t n = dataset.n_rows;
+ const size_t m = dataset.n_cols;
+
+ mW.zeros(n, rank);
+ mH.zeros(rank, m);
+ }
/**
* The update rule for the basis matrix W.
@@ -29,13 +41,15 @@ public:
template<typename MatType>
inline void WUpdate(const MatType& V,
arma::mat& W,
- const arma::mat& H) const
+ const arma::mat& H)
{
size_t n = V.n_rows;
size_t m = V.n_cols;
size_t r = W.n_cols;
+ mW = momentum * mW;
+
arma::mat deltaW(n, r);
deltaW.zeros();
@@ -46,7 +60,8 @@ public:
deltaW.row(i) -= kw * W.row(i);
}
- W += u * deltaW;
+ mW += u * deltaW;
+ W += mW;
}
/**
@@ -61,13 +76,15 @@ public:
template<typename MatType>
inline void HUpdate(const MatType& V,
const arma::mat& W,
- arma::mat& H) const
+ arma::mat& H)
{
size_t n = V.n_rows;
size_t m = V.n_cols;
size_t r = W.n_cols;
+ mH = momentum * mH;
+
arma::mat deltaH(r, m);
deltaH.zeros();
@@ -78,7 +95,8 @@ public:
deltaH.col(j) -= kh * H.col(j);
}
- H += u*deltaH;
+ mH += u*deltaH;
+ H += mH;
}
private:
@@ -94,6 +112,10 @@ private:
double kh;
double min;
double max;
+ double momentum;
+
+ arma::mat mW;
+ arma::mat mH;
};
} // namespace amf
} // namespace mlpack
More information about the mlpack-git
mailing list