[mlpack-git] master: 1 : add read only function FeatureSize 2 : add function InitializeWeights to avoid the case when RVO fail (e5c2e71)
gitdub at big.cc.gt.atl.ga.us
gitdub at big.cc.gt.atl.ga.us
Wed Sep 30 09:27:23 EDT 2015
Repository : https://github.com/mlpack/mlpack
On branch : master
Link : https://github.com/mlpack/mlpack/compare/8a8b708650f72c2aecbd9b4a12c8b16b4e0a3508...dc2c5c68dc4bfcdd2075b1a0fd2d641fce651669
>---------------------------------------------------------------
commit e5c2e71d69da76dcef187e93f6afae94fb9bc9cd
Author: stereomatchingkiss <stereomatchingkiss at gmail.com>
Date: Wed Sep 30 11:47:19 2015 +0800
1 : add read only function FeatureSize
2 : add function InitializeWeights to avoid the case when RVO fail
>---------------------------------------------------------------
e5c2e71d69da76dcef187e93f6afae94fb9bc9cd
.../softmax_regression/softmax_regression.hpp | 4 ++++
.../softmax_regression_function.cpp | 26 ++++++++++++++--------
.../softmax_regression_function.hpp | 17 +++++++++++++-
.../softmax_regression/softmax_regression_impl.hpp | 5 +++--
4 files changed, 40 insertions(+), 12 deletions(-)
diff --git a/src/mlpack/methods/softmax_regression/softmax_regression.hpp b/src/mlpack/methods/softmax_regression/softmax_regression.hpp
index fa7142a..8875421 100644
--- a/src/mlpack/methods/softmax_regression/softmax_regression.hpp
+++ b/src/mlpack/methods/softmax_regression/softmax_regression.hpp
@@ -161,6 +161,10 @@ class SoftmaxRegression
//! Get the model parameters.
const arma::mat& Parameters() const { return parameters; }
+ //! Gets the features size of the training data
+ size_t FeatureSize() const
+ { return parameters.n_rows; }
+
/**
* Serialize the SoftmaxRegression model.
*/
diff --git a/src/mlpack/methods/softmax_regression/softmax_regression_function.cpp b/src/mlpack/methods/softmax_regression/softmax_regression_function.cpp
index 9482e86..a3417af 100644
--- a/src/mlpack/methods/softmax_regression/softmax_regression_function.cpp
+++ b/src/mlpack/methods/softmax_regression/softmax_regression_function.cpp
@@ -41,19 +41,27 @@ InitializeWeights(const size_t featureSize,
const size_t numClasses,
const bool fitIntercept)
{
- // Initialize values to 0.005 * r. 'r' is a matrix of random values taken from
- // a Gaussian distribution with mean zero and variance one.
- // If the fitIntercept flag is true, parameters.col(0) is the intercept.
arma::mat parameters;
- if (fitIntercept)
- parameters.randn(numClasses, featureSize + 1);
- else
- parameters.randn(numClasses, featureSize);
- parameters = 0.005 * parameters;
-
+ InitializeWeights(parameters, featureSize, numClasses, fitIntercept);
return parameters;
}
+void SoftmaxRegressionFunction::
+InitializeWeights(arma::mat &weights,
+ const size_t featureSize,
+ const size_t numClasses,
+ const bool fitIntercept)
+{
+ // Initialize values to 0.005 * r. 'r' is a matrix of random values taken from
+ // a Gaussian distribution with mean zero and variance one.
+ // If the fitIntercept flag is true, parameters.col(0) is the intercept.
+ if (fitIntercept)
+ weights.randn(numClasses, featureSize + 1);
+ else
+ weights.randn(numClasses, featureSize);
+ weights *= 0.005;
+}
+
/**
* This is equivalent to applying the indicator function to the training
* labels. The output is in the form of a matrix, which leads to simpler
diff --git a/src/mlpack/methods/softmax_regression/softmax_regression_function.hpp b/src/mlpack/methods/softmax_regression/softmax_regression_function.hpp
index 9cf7dbd..01fe6d7 100644
--- a/src/mlpack/methods/softmax_regression/softmax_regression_function.hpp
+++ b/src/mlpack/methods/softmax_regression/softmax_regression_function.hpp
@@ -49,6 +49,20 @@ class SoftmaxRegressionFunction
const bool fitIntercept = false);
/**
+ * Initialize Softmax Regression weights(trainable parameters) with
+ * the given parameters.
+ * @paaram weights weights want to initialize
+ * @param featureSize The features size of the training set
+ * @param numClasses Number of classes for classification.
+ * @param fitIntercept Intercept term flag.
+ * @return weights after initialize
+ */
+ static void InitializeWeights(arma::mat &weights,
+ const size_t featureSize,
+ const size_t numClasses,
+ const bool fitIntercept = false);
+
+ /**
* Constructs the ground truth label matrix with the passed labels.
*
* @param labels Labels associated with the training data.
@@ -98,7 +112,8 @@ class SoftmaxRegressionFunction
size_t NumClasses() const { return numClasses; }
//! Gets the features size of the training data
- size_t FeatureSize() const { return data.n_rows; }
+ size_t FeatureSize() const
+ { return initialPoint.n_rows; }
//! Sets the regularization parameter.
double& Lambda() { return lambda; }
diff --git a/src/mlpack/methods/softmax_regression/softmax_regression_impl.hpp b/src/mlpack/methods/softmax_regression/softmax_regression_impl.hpp
index 122524f..43f6e01 100644
--- a/src/mlpack/methods/softmax_regression/softmax_regression_impl.hpp
+++ b/src/mlpack/methods/softmax_regression/softmax_regression_impl.hpp
@@ -22,8 +22,9 @@ SoftmaxRegression(const size_t inputSize,
lambda(0.0001),
fitIntercept(fitIntercept)
{
- parameters = SoftmaxRegressionFunction::InitializeWeights(inputSize, numClasses,
- fitIntercept);
+ SoftmaxRegressionFunction::InitializeWeights(parameters,
+ inputSize, numClasses,
+ fitIntercept);
}
template<template<typename> class OptimizerType>
More information about the mlpack-git
mailing list