From e5c2e71d69da76dcef187e93f6afae94fb9bc9cd Mon Sep 17 00:00:00 2001 From: stereomatchingkiss Date: Wed, 30 Sep 2015 11:47:19 +0800 Subject: [PATCH] 1 : add read only function FeatureSize 2 : add function InitializeWeights to avoid the case when RVO fail --- .../softmax_regression/softmax_regression.hpp | 4 +++ .../softmax_regression_function.cpp | 28 ++++++++++++------- .../softmax_regression_function.hpp | 17 ++++++++++- .../softmax_regression_impl.hpp | 5 ++-- 4 files changed, 41 insertions(+), 13 deletions(-) diff --git a/src/mlpack/methods/softmax_regression/softmax_regression.hpp b/src/mlpack/methods/softmax_regression/softmax_regression.hpp index fa7142a3ad..8875421578 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 9482e86f26..a3417af283 100644 --- a/src/mlpack/methods/softmax_regression/softmax_regression_function.cpp +++ b/src/mlpack/methods/softmax_regression/softmax_regression_function.cpp @@ -40,20 +40,28 @@ const arma::mat SoftmaxRegressionFunction:: 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 9cf7dbdffd..01fe6d7604 100644 --- a/src/mlpack/methods/softmax_regression/softmax_regression_function.hpp +++ b/src/mlpack/methods/softmax_regression/softmax_regression_function.hpp @@ -48,6 +48,20 @@ class SoftmaxRegressionFunction const size_t numClasses, 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. * @@ -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 122524fa09..43f6e016e3 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 class OptimizerType>