1 : add read only function FeatureSize
2 : add function InitializeWeights to avoid the case when RVO fail
This commit is contained in:
@@ -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.
|
||||
*/
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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; }
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user