[mlpack-svn] r10365 - mlpack/trunk/src/mlpack/methods/infomax_ica
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Wed Nov 23 18:36:43 EST 2011
Author: rcurtin
Date: 2011-11-23 18:36:43 -0500 (Wed, 23 Nov 2011)
New Revision: 10365
Added:
mlpack/trunk/src/mlpack/methods/infomax_ica/infomax_ica.cpp
mlpack/trunk/src/mlpack/methods/infomax_ica/infomax_ica.hpp
mlpack/trunk/src/mlpack/methods/infomax_ica/main.cpp
Removed:
mlpack/trunk/src/mlpack/methods/infomax_ica/infomax_ica.cc
mlpack/trunk/src/mlpack/methods/infomax_ica/infomax_ica.h
mlpack/trunk/src/mlpack/methods/infomax_ica/main.cc
Modified:
mlpack/trunk/src/mlpack/methods/infomax_ica/CMakeLists.txt
Log:
Change filenames as per #152 and fix formatting as per #153.
Modified: mlpack/trunk/src/mlpack/methods/infomax_ica/CMakeLists.txt
===================================================================
--- mlpack/trunk/src/mlpack/methods/infomax_ica/CMakeLists.txt 2011-11-23 23:30:20 UTC (rev 10364)
+++ mlpack/trunk/src/mlpack/methods/infomax_ica/CMakeLists.txt 2011-11-23 23:36:43 UTC (rev 10365)
@@ -3,8 +3,8 @@
# Define the files we need to compile
# Anything not in this list will not be compiled into MLPACK.
set(SOURCES
- infomax_ica.h
- infomax_ica.cc
+ infomax_ica.hpp
+ infomax_ica.cpp
)
# Add directory name to sources.
@@ -17,7 +17,7 @@
set(MLPACK_SRCS ${MLPACK_SRCS} ${DIR_SRCS} PARENT_SCOPE)
add_executable(infomax_ica
- main.cc
+ main.cpp
)
target_link_libraries(infomax_ica
mlpack
Deleted: mlpack/trunk/src/mlpack/methods/infomax_ica/infomax_ica.cc
===================================================================
--- mlpack/trunk/src/mlpack/methods/infomax_ica/infomax_ica.cc 2011-11-23 23:30:20 UTC (rev 10364)
+++ mlpack/trunk/src/mlpack/methods/infomax_ica/infomax_ica.cc 2011-11-23 23:36:43 UTC (rev 10365)
@@ -1,218 +0,0 @@
-/**
- * @file infomax_ica.cc
- * @author Chip Mappus
- *
- * Methods for InfomaxICA.
- *
- * @see infomax_ica.h
- */
-#include <mlpack/core.h>
-
-#include "infomax_ica.h"
-
-namespace mlpack {
-namespace infomax_ica {
-
-PARAM(double, "lambda", "The learning rate.", "info", .001,
- false);
-PARAM_INT_REQ("B", "Infomax data window size.", "info");
-PARAM(double, "epsilon", "Infomax algorithm stop threshold.", "info", .001,
- false);
-PARAM_MODULE("info",
- "This performs ICO decomposition on a given dataset using the Infomax method");
-
-/**
- * Dummy constructor
- */
-InfomaxICA::InfomaxICA() { }
-
-InfomaxICA::InfomaxICA(double lambda, size_t b, double epsilon) :
- lambda_(lambda),
- b_(b),
- epsilon_(epsilon) { }
-
-/**
- * Sphere the data, apply ica. This is the first function to call
- * after initializing the variables.
- */
-void InfomaxICA::applyICA(const arma::mat& dataset) {
- double current_cos = DBL_MAX;
- w_.set_size(b_, b_);
- data_ = dataset; // Copy the matrix? Stupid!
-
- if (b_ < data_.n_cols) {
- sphere(data_);
-
- // initial estimate for w is Id
- arma::mat i1;
- i1.eye(data_.n_rows, data_.n_rows);
- w_ = i1; // Another copy? Stupid!
- while (epsilon_ <= current_cos){
- arma::mat w_prev = w_;
- evaluateICA();
- current_cos = w_delta(w_prev, w_);
- }
- } else
- Log::Fatal << "Window size must be less than number of instances."
- << std::endl;
-}
-
-/**
- * Run infomax. Call this after initialization and applyICA.
- */
-void InfomaxICA::evaluateICA() {
- arma::mat BI;
- BI.eye(w_.n_rows, w_.n_rows);
- BI *= b_;
-
- // intermediate calculation variables
- arma::mat icv(w_.n_rows, b_);
- arma::mat icv2(w_.n_rows, w_.n_cols);
- arma::mat icv4(w_.n_rows, w_.n_cols);
-
- for (size_t i = 0; i < data_.n_cols; i += b_) {
- if ((i + b_) < data_.n_cols) {
- // This is not tested.
- icv = -2.0 * arma::pow(arma::exp(
- -1 * (w_ * data_.cols(i, i + b_))) + 1, -1) + 1;
-
- icv2 = icv * arma::trans(data_.cols(i, i + b_));
-
- icv4 = lambda_ * (icv2 + BI);
-
- icv2 = icv4 * w_;
-
- w_ += icv2;
- }
- }
-}
-
-// sphereing functions
-/**
- * Sphere the input data.
- */
-void InfomaxICA::sphere(arma::mat& data) {
- arma::mat sample_covariance = sampleCovariance(data);
- arma::mat wz = sqrtm(sample_covariance);
- arma::mat data_sub_means = subMeans(data);
-
- arma::mat wz_inverse = inv(wz);
-
- // Not tested.
- wz_inverse *= 2.0;
- data = wz_inverse * data_sub_means;
-}
-
-// Covariance matrix.
-arma::mat InfomaxICA::sampleCovariance(const arma::mat& m) {
- // Not tested.
- arma::mat ttm = subMeans(m);
- arma::mat wm = trans(ttm);
-
- arma::mat twm(ttm.n_rows, ttm.n_rows);
- arma::mat output(ttm.n_rows, ttm.n_rows);
- output.zeros();
-
- arma::mat tttm(wm);
-
- tttm *= (1 / (double) (ttm.n_cols - 1));
- twm = trans(wm) * tttm;
- output = twm;
-
- return output;
-}
-
-arma::mat InfomaxICA::subMeans(const arma::mat& m){
- arma::mat output(m);
- arma::vec row_means = rowMean(output);
-
- for (size_t j = 0; j < output.n_cols; j++) {
- output.col(j) -= row_means;
- }
-
- return output;
-}
-
-/**
- * Compute the sample mean of a column
- */
-arma::vec InfomaxICA::rowMean(const arma::mat& m){
- arma::vec row_means(m.n_rows);
- row_means.zeros();
-
- for (size_t j = 0; j < m.n_cols; j++) {
- row_means += m.col(j);
- }
-
- row_means /= (double) m.n_cols;
-
- return row_means;
-}
-
-/**
- * Matrix square root using Cholesky decomposition method. Assumes the input
- * matrix is square.
- */
-arma::mat InfomaxICA::sqrtm(const arma::mat& m) {
- arma::mat output(m.n_rows, m.n_cols);
- arma::mat chol;
-
- if (arma::chol(chol, m)) {
- arma::mat u, v;
- arma::vec s;
-
- if (arma::svd(u, s, v, trans(chol))) {
- arma::mat S(s.n_elem, s.n_elem);
- S.zeros();
- S.diag() = s;
-
- arma::mat tm1 = u * S;
- output = tm1 * trans(u);
- } else
- Log::Warn << "InfomaxICA sqrtm: SVD failed." << std::endl;
- } else
- Log::Warn << "InfomaxICA sqrtm: Cholesky decomposition failed." << std::endl;
-
- return output;
-}
-
-// Compare w estimates for convergence
-double InfomaxICA::w_delta(const arma::mat& w_prev, const arma::mat& w_pres) {
- arma::mat temp = w_pres - w_prev;
- arma::vec delta = reshape(temp, temp.n_rows * temp.n_cols, 1);
- double delta_dot = arma::dot(delta, delta);
-
- Log::Info << "w change: " << delta_dot << std::endl;
- return delta_dot;
-}
-
-/**
- * Return the current unmixing matrix estimate. Requires a reference
- * to an uninitialized matrix.
- */
-void InfomaxICA::getUnmixing(arma::mat& w) {
- w = w_;
-}
-
-/**
- * Return the source estimates, S. S is a reference to an
- * uninitialized matrix.
- */
-void InfomaxICA::getSources(const arma::mat& dataset, arma::mat& s) {
- s = w_ * dataset;
-}
-
-void InfomaxICA::setLambda(const double lambda) {
- lambda_ = lambda;
-}
-
-void InfomaxICA::setB(const size_t b) {
- b_ = b;
-}
-
-void InfomaxICA::setEpsilon(const double epsilon) {
- epsilon_ = epsilon;
-}
-
-}; // namespace fastica
-}; // namespace mlpack
Copied: mlpack/trunk/src/mlpack/methods/infomax_ica/infomax_ica.cpp (from rev 10352, mlpack/trunk/src/mlpack/methods/infomax_ica/infomax_ica.cc)
===================================================================
--- mlpack/trunk/src/mlpack/methods/infomax_ica/infomax_ica.cpp (rev 0)
+++ mlpack/trunk/src/mlpack/methods/infomax_ica/infomax_ica.cpp 2011-11-23 23:36:43 UTC (rev 10365)
@@ -0,0 +1,234 @@
+/**
+ * @file infomax_ica.cpp
+ * @author Chip Mappus
+ *
+ * Methods for InfomaxICA.
+ *
+ * @see infomax_ica.h
+ */
+#include <mlpack/core.h>
+
+#include "infomax_ica.hpp"
+
+namespace mlpack {
+namespace infomax_ica {
+
+PARAM(double, "lambda", "The learning rate.", "info", .001,
+ false);
+PARAM_INT_REQ("B", "Infomax data window size.", "info");
+PARAM(double, "epsilon", "Infomax algorithm stop threshold.", "info", .001,
+ false);
+PARAM_MODULE("info",
+ "This performs ICO decomposition on a given dataset using the Infomax method");
+
+/**
+ * Dummy constructor
+ */
+InfomaxICA::InfomaxICA() { }
+
+InfomaxICA::InfomaxICA(double lambda, size_t b, double epsilon) :
+ lambda_(lambda),
+ b_(b),
+ epsilon_(epsilon) { }
+
+/**
+ * Sphere the data, apply ica. This is the first function to call
+ * after initializing the variables.
+ */
+void InfomaxICA::applyICA(const arma::mat& dataset)
+{
+ double current_cos = DBL_MAX;
+ w_.set_size(b_, b_);
+ data_ = dataset; // Copy the matrix? Stupid!
+
+ if (b_ < data_.n_cols)
+ {
+ sphere(data_);
+
+ // initial estimate for w is Id
+ arma::mat i1;
+ i1.eye(data_.n_rows, data_.n_rows);
+ w_ = i1; // Another copy? Stupid!
+ while (epsilon_ <= current_cos)
+ {
+ arma::mat w_prev = w_;
+ evaluateICA();
+ current_cos = w_delta(w_prev, w_);
+ }
+ }
+ else
+ Log::Fatal << "Window size must be less than number of instances."
+ << std::endl;
+}
+
+/**
+ * Run infomax. Call this after initialization and applyICA.
+ */
+void InfomaxICA::evaluateICA()
+{
+ arma::mat BI;
+ BI.eye(w_.n_rows, w_.n_rows);
+ BI *= b_;
+
+ // intermediate calculation variables
+ arma::mat icv(w_.n_rows, b_);
+ arma::mat icv2(w_.n_rows, w_.n_cols);
+ arma::mat icv4(w_.n_rows, w_.n_cols);
+
+ for (size_t i = 0; i < data_.n_cols; i += b_)
+ {
+ if ((i + b_) < data_.n_cols)
+ {
+ // This is not tested.
+ icv = -2.0 * arma::pow(arma::exp(
+ -1 * (w_ * data_.cols(i, i + b_))) + 1, -1) + 1;
+ icv2 = icv * arma::trans(data_.cols(i, i + b_));
+ icv4 = lambda_ * (icv2 + BI);
+ icv2 = icv4 * w_;
+ w_ += icv2;
+ }
+ }
+}
+
+// sphereing functions
+/**
+ * Sphere the input data.
+ */
+void InfomaxICA::sphere(arma::mat& data)
+{
+ arma::mat sample_covariance = sampleCovariance(data);
+ arma::mat wz = sqrtm(sample_covariance);
+ arma::mat data_sub_means = subMeans(data);
+
+ arma::mat wz_inverse = inv(wz);
+
+ // Not tested.
+ wz_inverse *= 2.0;
+ data = wz_inverse * data_sub_means;
+}
+
+// Covariance matrix.
+arma::mat InfomaxICA::sampleCovariance(const arma::mat& m)
+{
+ // Not tested.
+ arma::mat ttm = subMeans(m);
+ arma::mat wm = trans(ttm);
+
+ arma::mat twm(ttm.n_rows, ttm.n_rows);
+ arma::mat output(ttm.n_rows, ttm.n_rows);
+ output.zeros();
+
+ arma::mat tttm(wm);
+
+ tttm *= (1 / (double) (ttm.n_cols - 1));
+ twm = trans(wm) * tttm;
+ output = twm;
+
+ return output;
+}
+
+arma::mat InfomaxICA::subMeans(const arma::mat& m)
+{
+ arma::mat output(m);
+ arma::vec row_means = rowMean(output);
+
+ for (size_t j = 0; j < output.n_cols; j++)
+ output.col(j) -= row_means;
+
+ return output;
+}
+
+/**
+ * Compute the sample mean of a column
+ */
+arma::vec InfomaxICA::rowMean(const arma::mat& m)
+{
+ arma::vec row_means(m.n_rows);
+ row_means.zeros();
+
+ for (size_t j = 0; j < m.n_cols; j++)
+ row_means += m.col(j);
+
+ row_means /= (double) m.n_cols;
+
+ return row_means;
+}
+
+/**
+ * Matrix square root using Cholesky decomposition method. Assumes the input
+ * matrix is square.
+ */
+arma::mat InfomaxICA::sqrtm(const arma::mat& m)
+{
+ arma::mat output(m.n_rows, m.n_cols);
+ arma::mat chol;
+
+ if (arma::chol(chol, m))
+ {
+ arma::mat u, v;
+ arma::vec s;
+
+ if (arma::svd(u, s, v, trans(chol)))
+ {
+ arma::mat S(s.n_elem, s.n_elem);
+ S.zeros();
+ S.diag() = s;
+
+ arma::mat tm1 = u * S;
+ output = tm1 * trans(u);
+ }
+ else
+ Log::Warn << "InfomaxICA sqrtm: SVD failed." << std::endl;
+ }
+ else
+ Log::Warn << "InfomaxICA sqrtm: Cholesky decomposition failed." << std::endl;
+
+ return output;
+}
+
+// Compare w estimates for convergence
+double InfomaxICA::w_delta(const arma::mat& w_prev, const arma::mat& w_pres)
+{
+ arma::mat temp = w_pres - w_prev;
+ arma::vec delta = reshape(temp, temp.n_rows * temp.n_cols, 1);
+ double delta_dot = arma::dot(delta, delta);
+
+ Log::Info << "w change: " << delta_dot << std::endl;
+ return delta_dot;
+}
+
+/**
+ * Return the current unmixing matrix estimate. Requires a reference
+ * to an uninitialized matrix.
+ */
+void InfomaxICA::getUnmixing(arma::mat& w)
+{
+ w = w_;
+}
+
+/**
+ * Return the source estimates, S. S is a reference to an
+ * uninitialized matrix.
+ */
+void InfomaxICA::getSources(const arma::mat& dataset, arma::mat& s)
+{
+ s = w_ * dataset;
+}
+
+void InfomaxICA::setLambda(const double lambda)
+{
+ lambda_ = lambda;
+}
+
+void InfomaxICA::setB(const size_t b)
+{
+ b_ = b;
+}
+
+void InfomaxICA::setEpsilon(const double epsilon)
+{
+ epsilon_ = epsilon;
+}
+
+}; // namespace fastica
+}; // namespace mlpack
Deleted: mlpack/trunk/src/mlpack/methods/infomax_ica/infomax_ica.h
===================================================================
--- mlpack/trunk/src/mlpack/methods/infomax_ica/infomax_ica.h 2011-11-23 23:30:20 UTC (rev 10364)
+++ mlpack/trunk/src/mlpack/methods/infomax_ica/infomax_ica.h 2011-11-23 23:36:43 UTC (rev 10365)
@@ -1,77 +0,0 @@
-/**
- * @file infomax_ica.h
- * @author Chip Mappus
- *
- * Yet another infomax ICA implementation.
- *
- * Bell, A. and Sejnowski,T. (1995)
- * "An information maximisation approach to blind signal
- * separation." Neural Computation. 1129-1159.
- *
- * For details:
- * http://www.cnl.salk.edu/~tony/ica.html
- *
- */
-#ifndef U_INFOMAX_ICA
-#define U_INFOMAX_ICA
-
-#include <mlpack/core.h>
-
-namespace mlpack {
-namespace infomax_ica {
-
-class TestInfomaxICA; // forward reference
-
-/**
- * Infomax ICA. Given an observation matrix and input parameters,
- * return the corresponding unmixming matrix, W.
- * Exmaple use:
- *
- * @code
- * InfomaxICA *ica = new InfomaxICA(lambda, B, epsilon);
- * Matrix west;
- * ica->applyICA(dataset);
- * ica->getUnmixing(west);
- * @endcode
- */
-
-class InfomaxICA {
-
- friend class TestInfomaxICA;
-
- public:
- InfomaxICA();
- InfomaxICA(double lambda, size_t B, double epsilon);
- void applyICA(const arma::mat& dataset);
- void evaluateICA();
- void displayMatrix(const arma::mat& m);
- void displayVector(const arma::vec& m);
- void getUnmixing(arma::mat& w);
- void getSources(const arma::mat& dataset, arma::mat& s);
- void setLambda(const double lambda);
- void setB(const size_t b);
- void setEpsilon(const double epsilon);
-
- arma::mat sampleCovariance(const arma::mat& m);
- arma::mat sqrtm(const arma::mat& m);
-
- private:
- arma::mat w_;
- arma::mat data_;
- // learning rate
- double lambda_;
- // block size
- size_t b_;
- // epsilon for convergence
- double epsilon_;
- // utility functions
- void sphere(arma::mat& m);
- arma::mat subMeans(const arma::mat& m);
- arma::vec rowMean(const arma::mat& m);
- double w_delta(const arma::mat& w_prev, const arma::mat& w_pres);
-};
-
-}; // namespace infomax_ica
-}; // namespace mlpack
-
-#endif
Copied: mlpack/trunk/src/mlpack/methods/infomax_ica/infomax_ica.hpp (from rev 10352, mlpack/trunk/src/mlpack/methods/infomax_ica/infomax_ica.h)
===================================================================
--- mlpack/trunk/src/mlpack/methods/infomax_ica/infomax_ica.hpp (rev 0)
+++ mlpack/trunk/src/mlpack/methods/infomax_ica/infomax_ica.hpp 2011-11-23 23:36:43 UTC (rev 10365)
@@ -0,0 +1,74 @@
+/**
+ * @file infomax_ica.hpp
+ * @author Chip Mappus
+ *
+ * Yet another infomax ICA implementation.
+ *
+ * Bell, A. and Sejnowski,T. (1995)
+ * "An information maximisation approach to blind signal
+ * separation." Neural Computation. 1129-1159.
+ *
+ * For details:
+ * http://www.cnl.salk.edu/~tony/ica.html
+ *
+ */
+#ifndef __MLPACK_METHODS_INFOMAX_ICA_INFOMAX_ICA_HPP
+#define __MLPACK_METHODS_INFOMAX_ICA_INFOMAX_ICA_HPP
+
+#include <mlpack/core.h>
+
+namespace mlpack {
+namespace infomax_ica {
+
+class TestInfomaxICA; // forward reference
+
+/**
+ * Infomax ICA. Given an observation matrix and input parameters,
+ * return the corresponding unmixming matrix, W.
+ * Exmaple use:
+ *
+ * @code
+ * InfomaxICA *ica = new InfomaxICA(lambda, B, epsilon);
+ * Matrix west;
+ * ica->applyICA(dataset);
+ * ica->getUnmixing(west);
+ * @endcode
+ */
+class InfomaxICA
+{
+ public:
+ InfomaxICA();
+ InfomaxICA(double lambda, size_t B, double epsilon);
+ void applyICA(const arma::mat& dataset);
+ void evaluateICA();
+ void displayMatrix(const arma::mat& m);
+ void displayVector(const arma::vec& m);
+ void getUnmixing(arma::mat& w);
+ void getSources(const arma::mat& dataset, arma::mat& s);
+ void setLambda(const double lambda);
+ void setB(const size_t b);
+ void setEpsilon(const double epsilon);
+
+ arma::mat sampleCovariance(const arma::mat& m);
+ arma::mat sqrtm(const arma::mat& m);
+
+ private:
+ arma::mat w_;
+ arma::mat data_;
+ // learning rate
+ double lambda_;
+ // block size
+ size_t b_;
+ // epsilon for convergence
+ double epsilon_;
+ // utility functions
+ void sphere(arma::mat& m);
+ arma::mat subMeans(const arma::mat& m);
+ arma::vec rowMean(const arma::mat& m);
+ double w_delta(const arma::mat& w_prev, const arma::mat& w_pres);
+};
+
+}; // namespace infomax_ica
+}; // namespace mlpack
+
+#endif
Deleted: mlpack/trunk/src/mlpack/methods/infomax_ica/main.cc
===================================================================
--- mlpack/trunk/src/mlpack/methods/infomax_ica/main.cc 2011-11-23 23:30:20 UTC (rev 10364)
+++ mlpack/trunk/src/mlpack/methods/infomax_ica/main.cc 2011-11-23 23:36:43 UTC (rev 10365)
@@ -1,33 +0,0 @@
-/**
- * @file main.cc
- * @author Chip Mappus
- *
- * main for using infomax ICA method.
- */
-#include <mlpack/core.h>
-#include "infomax_ica.h"
-
-PARAM_STRING_REQ("data", "The name of the file containing mixture data.",
- "info");
-
-using namespace mlpack;
-using namespace infomax_ica;
-
-int main(int argc, char *argv[]) {
- CLI::ParseCommandLine(argc, argv);
-
- const char *data_file_name = CLI::GetParam<std::string>("info/data").c_str();
- double lambda = CLI::GetParam<double>("info/lambda");
- int B = CLI::GetParam<int>("info/B");
- double epsilon = CLI::GetParam<double>("info/epsilon");
-
- arma::mat dataset;
- data::Load(data_file_name, dataset, true);
-
- InfomaxICA ica(lambda, B, epsilon);
- ica.applyICA(dataset);
-
- arma::mat west;
- ica.getUnmixing(west);
- //ica->displayMatrix(west);
-}
Copied: mlpack/trunk/src/mlpack/methods/infomax_ica/main.cpp (from rev 10352, mlpack/trunk/src/mlpack/methods/infomax_ica/main.cc)
===================================================================
--- mlpack/trunk/src/mlpack/methods/infomax_ica/main.cpp (rev 0)
+++ mlpack/trunk/src/mlpack/methods/infomax_ica/main.cpp 2011-11-23 23:36:43 UTC (rev 10365)
@@ -0,0 +1,35 @@
+/**
+ * @file main.cpp
+ * @author Chip Mappus
+ *
+ * Main function for using infomax ICA method.
+ */
+#include <mlpack/core.h>
+
+#include "infomax_ica.hpp"
+
+PARAM_STRING_REQ("data", "The name of the file containing mixture data.",
+ "info");
+
+using namespace mlpack;
+using namespace infomax_ica;
+
+int main(int argc, char *argv[])
+{
+ CLI::ParseCommandLine(argc, argv);
+
+ const char *data_file_name = CLI::GetParam<std::string>("info/data").c_str();
+ double lambda = CLI::GetParam<double>("info/lambda");
+ int B = CLI::GetParam<int>("info/B");
+ double epsilon = CLI::GetParam<double>("info/epsilon");
+
+ arma::mat dataset;
+ data::Load(data_file_name, dataset, true);
+
+ InfomaxICA ica(lambda, B, epsilon);
+ ica.applyICA(dataset);
+
+ arma::mat west;
+ ica.getUnmixing(west);
+ //ica->displayMatrix(west);
+}
More information about the mlpack-svn
mailing list