Merge remote-tracking branch 'origin/master' into sparse-coding-doc

This commit is contained in:
Ryan Curtin
2024-05-16 10:37:42 -04:00
70 changed files with 533 additions and 722 deletions
+2
View File
@@ -41,6 +41,8 @@
* Fix non-working `verbose` option for R bindings (#3691).
* Fix divide-by-zero edge case for LARS (#3701).
### mlpack 4.3.0
###### 2023-11-27
* Fix include ordering issue for `LinearRegression` (#3541).
+22 -24
View File
@@ -66,40 +66,38 @@ avoid copies.
---
* `MakeAlias(a, mat, rows, cols, strict=true)`
* `MakeAlias(a, vector, rows, cols, offset=0, strict=true)`
- Make `a` into an alias of `vector` with the given size.
- If `offset` is `0`, then the alias is identical: the first element of
`a` is the first element of `vector`. Otherwise, the first element of `a`
is the `offset`'th element of `vector`.
- If `strict` is `true`, the size of `a` cannot be changed.
- `vector` and `a` should have the same vector type (e.g. `arma::vec`,
`arma::fvec`).
- If an alias cannot be created, the vector will be copied.
* `MakeAlias(a, mat, rows, cols, offset=0, strict=true)`
- Make `a` into an alias of `mat` with the given size.
- If `offset` is `0`, then the alias is identical: the first element of
`a` is the first element of `mat`. Otherwise, the first element of `a`
is the `offset`'th element of `mat`; elements in `mat` are ordered in
a [column-major way](../matrices.md#representing-data-in-mlpack).
- If `strict` is `true`, the size of `a` cannot be changed.
- `mat` and `a` should have the same matrix type (e.g. `arma::mat`,
`arma::fmat`, `arma::sp_mat`).
- If an alias cannot be created, the matrix will be copied. Sparse types
cannot have aliases and will be copied.
* `MakeAlias(a, cube, rows, cols, slices, strict=true)`
* `MakeAlias(a, cube, rows, cols, slices, offset=0, strict=true)`
- Make `a` into an alias of `cube` with the given size.
- If `offset` is `0`, then the alias is identical: the first element of
`a` is the first element of `cube`. Otherwise, the first element of `a`
is the `offset`'th element of `cube`; elements in `cube` are ordered in
a [column-major way](../matrices.md#representing-data-in-mlpack).
- If `strict` is `true`, the size of `a` cannot be changed.
- `cube` and `a` should have the same matrix type (e.g. `arma::cube`,
- `cube` and `a` should have the same cube type (e.g. `arma::cube`,
`arma::fcube`).
- If an alias cannot be created, the matrix will be copied.
* `MakeAlias(a, memptr, rows, cols, strict=true)`
- Make `a` into an alias of the memory block starting at `memptr` of size
`rows` by `cols`.
- The memory at `memptr` should be arranged in a [column-major
ordering](matrices.md#representing-data-in-mlpack).
- If `strict` is `true`, the size of `a` cannot be changed.
- `a` should be a dense matrix type (e.g. `arma::mat`, `arma::fmat`), and
`memptr` should be a non-const pointer of the matrix's element type (e.g.
`double*`, `float*`).
* `MakeAlias(a, memptr, rows, cols, slices, strict=true)`
- Make `a` into an alias of the memory block starting at `memptr` of size
`rows` by `cols` by `slices`.
- The memory at `memptr` should be arranged in a [column-major
ordering](matrices.md#representing-data-in-mlpack).
- If `strict` is `true`, the size of `a` cannot be changed.
- `a` should be a cube type (e.g. `arma::cube`, `arma::fcube`), and `memptr`
should be a non-const pointer of the matrix's element type (e.g. `double*`,
`float*`).
- If an alias cannot be created, the cube will be copied.
---
@@ -104,11 +104,6 @@ inline void PrintHelp(util::Params& params, const std::string& param)
if ((pass == 2) && data.input) // Output options only (always optional).
continue;
// For reverse compatibility: this can be removed when these options are
// gone in mlpack 3.0.0. We don't want to print the deprecated options.
if (data.name == "inputFile")
continue;
if (!printedHeader)
{
printedHeader = true;
@@ -41,18 +41,6 @@ class RegressionDistribution
*/
RegressionDistribution() { /* nothing to do */ }
/**
* Create a Conditional Gaussian distribution with conditional mean function
* obtained by running RegressionFunction on predictors, responses.
*
* @param predictors Matrix of predictors (X).
* @param responses Vector of responses (y).
*/
mlpack_deprecated RegressionDistribution(const arma::mat& predictors,
const arma::vec& responses) :
RegressionDistribution(predictors, arma::rowvec(responses.t()))
{}
/**
* Create a Conditional Gaussian distribution with conditional mean function
* obtained by running RegressionFunction on predictors, responses.
@@ -97,15 +85,6 @@ class RegressionDistribution
*/
void Train(const arma::mat& observations);
/**
* Estimate parameters using provided observation weights.
*
* @param observations List of observations.
* @param weights Probability that given observation is from distribution.
*/
mlpack_deprecated void Train(const arma::mat& observations,
const arma::vec& weights);
/**
* Estimate parameters using provided observation weights.
*
@@ -131,15 +110,6 @@ class RegressionDistribution
return std::log(Probability(observation));
}
/**
* Calculate y_i for each data point in points.
*
* @param points The data points to calculate with.
* @param predictions Y, will contain calculated values on completion.
*/
mlpack_deprecated void Predict(const arma::mat& points,
arma::vec& predictions) const;
/**
* Calculate y_i for each data point in points.
*
@@ -32,17 +32,6 @@ inline void RegressionDistribution::Train(const arma::mat& observations)
err.Train(observations.row(0) - fitted);
}
/**
* Estimate parameters using provided observation weights.
*
* @param weights Probability that given observation is from distribution.
*/
inline void RegressionDistribution::Train(const arma::mat& observations,
const arma::vec& weights)
{
Train(observations, arma::rowvec(weights.t()));
}
inline void RegressionDistribution::Train(const arma::mat& observations,
const arma::rowvec& weights)
{
@@ -67,14 +56,6 @@ inline double RegressionDistribution::Probability(
return err.Probability(observation(0) - fitted.t());
}
inline void RegressionDistribution::Predict(const arma::mat& points,
arma::vec& predictions) const
{
arma::rowvec rowPredictions;
Predict(points, rowPredictions);
predictions = rowPredictions.t();
}
inline void RegressionDistribution::Predict(const arma::mat& points,
arma::rowvec& predictions) const
{
+43 -28
View File
@@ -16,55 +16,69 @@
namespace mlpack {
/**
* Reconstruct `m` as an alias around the memory `newMem`, with size `numRows` x
* Reconstruct `v` as an alias around the memory `newMem`, with size `numRows` x
* `numCols`.
*/
template<typename MatType>
void MakeAlias(MatType& m,
typename MatType::elem_type* newMem,
const size_t numRows,
const size_t numCols,
template<typename InVecType, typename OutVecType>
void MakeAlias(OutVecType& v,
const InVecType& oldVec,
const size_t numElems,
const size_t offset = 0,
const bool strict = true,
const typename std::enable_if_t<!IsCube<MatType>::value>* = 0)
const typename std::enable_if_t<IsVector<OutVecType>::value>* = 0)
{
// We use placement new to reinitialize the object, since the copy and move
// assignment operators in Armadillo will end up copying memory instead of
// making an alias.
m.~MatType();
new (&m) MatType(newMem, numRows, numCols, false, strict);
typename InVecType::elem_type* newMem =
const_cast<typename InVecType::elem_type*>(oldVec.memptr()) + offset;
v.~OutVecType();
new (&v) OutVecType(newMem, numElems, false, strict);
}
/**
* Reconstruct `m` as an alias around the memory `newMem`, with size `numRows` x
* `numCols`.
*/
template<typename InMatType, typename OutMatType>
void MakeAlias(OutMatType& m,
const InMatType& oldMat,
const size_t numRows,
const size_t numCols,
const size_t offset = 0,
const bool strict = true,
const typename std::enable_if_t<IsMatrix<OutMatType>::value>* = 0)
{
// We use placement new to reinitialize the object, since the copy and move
// assignment operators in Armadillo will end up copying memory instead of
// making an alias.
typename InMatType::elem_type* newMem =
const_cast<typename InMatType::elem_type*>(oldMat.memptr()) + offset;
m.~OutMatType();
new (&m) OutMatType(newMem, numRows, numCols, false, strict);
}
/**
* Reconstruct `c` as an alias around the memory` newMem`, with size `numRows` x
* `numCols` x `numSlices`.
*/
template<typename CubeType>
void MakeAlias(CubeType& c,
typename CubeType::elem_type* newMem,
template<typename InCubeType, typename OutCubeType>
void MakeAlias(OutCubeType& c,
const InCubeType& oldCube,
const size_t numRows,
const size_t numCols,
const size_t numSlices,
const size_t offset = 0,
const bool strict = true,
const typename std::enable_if_t<IsCube<CubeType>::value>* = 0)
const typename std::enable_if_t<IsCube<OutCubeType>::value>* = 0)
{
// We use placement new to reinitialize the object, since the copy and move
// assignment operators in Armadillo will end up copying memory instead of
// making an alias.
c.~CubeType();
new (&c) CubeType(newMem, numRows, numCols, numSlices, false, strict);
}
/**
* Make `m` an alias of `in`, using the given size.
*/
template<typename eT>
void MakeAlias(arma::Mat<eT>& m,
const arma::Mat<eT>& in,
const size_t numRows,
const size_t numCols,
const bool strict = true)
{
MakeAlias(m, (eT*) in.memptr(), numRows, numCols, strict);
typename InCubeType::elem_type* newMem =
const_cast<typename InCubeType::elem_type*>(oldCube.memptr()) + offset;
c.~OutCubeType();
new (&c) OutCubeType(newMem, numRows, numCols, numSlices, false, strict);
}
/**
@@ -75,6 +89,7 @@ void MakeAlias(arma::SpMat<eT>& m,
const arma::SpMat<eT>& in,
const size_t /* numRows */,
const size_t /* numCols */,
const size_t /* offset */,
const bool /* strict */)
{
// We can't make aliases of sparse objects, so just copy it.
@@ -61,7 +61,6 @@ BINDING_EXAMPLE(
// Classification options.
PARAM_MATRIX_IN_REQ("test", "Test dataset.", "T");
// PARAM_UROW_OUT("output") is deprecated and will be removed in mlpack 4.0.0.
PARAM_UROW_OUT("predictions", "Predicted labels for the test set.", "P");
// Loading/saving of a model.
+3 -15
View File
@@ -84,14 +84,7 @@ BINDING_LONG_DESC(
"classes for each point in the test dataset are output to the " +
PRINT_PARAM_STRING("predictions") + " output parameter. The AdaBoost "
"model itself is output to the " + PRINT_PARAM_STRING("output_model") +
" output parameter."
"\n\n"
"Note: the following parameter is deprecated and "
"will be removed in mlpack 4.0.0: " + PRINT_PARAM_STRING("output") +
"."
"\n"
"Use " + PRINT_PARAM_STRING("predictions") + " instead of " +
PRINT_PARAM_STRING("output") + '.');
" output parameter.");
// Example.
BINDING_EXAMPLE(
@@ -127,8 +120,6 @@ PARAM_UROW_IN("labels", "Labels for the training set.", "l");
// Classification options.
PARAM_MATRIX_IN("test", "Test dataset.", "T");
// PARAM_UROW_OUT("output") is deprecated and will be removed in mlpack 4.0.0.
PARAM_UROW_OUT("output", "Predicted labels for the test set.", "o");
PARAM_UROW_OUT("predictions", "Predicted labels for the test set.", "P");
PARAM_MATRIX_OUT("probabilities", "Predicted class probabilities for each "
"point in the test set.", "p");
@@ -178,10 +169,9 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& timers)
"no task will be performed");
}
RequireAtLeastOnePassed(params, { "output_model", "output", "predictions" },
false, "no results will be saved");
RequireAtLeastOnePassed(params, { "output_model", "predictions" }, false,
"no results will be saved");
// "output" will be removed in mlpack 4.0.0.
ReportIgnoredParam(params, {{ "test", false }}, "predictions");
AdaBoostModel* m;
@@ -266,8 +256,6 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& timers)
data::RevertLabels(predictedLabels, m->Mappings(), results);
// Save the predicted labels.
if (params.Has("output"))
params.Get<arma::Row<size_t>>("output") = results;
if (params.Has("predictions"))
params.Get<arma::Row<size_t>>("predictions") = std::move(results);
if (params.Has("probabilities"))
+15 -12
View File
@@ -204,11 +204,12 @@ void FFN<
const size_t effectiveBatchSize = std::min(batchSize,
size_t(predictors.n_cols) - i);
const MatType predictorAlias(
const_cast<typename MatType::elem_type*>(predictors.colptr(i)),
predictors.n_rows, effectiveBatchSize, false, true);
MatType resultAlias(results.colptr(i), results.n_rows,
effectiveBatchSize, false, true);
MatType predictorAlias, resultAlias;
MakeAlias(predictorAlias, predictors, predictors.n_rows,
effectiveBatchSize, i * predictors.n_rows);
MakeAlias(resultAlias, results, results.n_rows, effectiveBatchSize,
i * results.n_rows);
network.Forward(predictorAlias, resultAlias);
}
@@ -449,8 +450,10 @@ typename MatType::elem_type FFN<
// pass.
networkOutput.set_size(network.OutputSize(), batchSize);
MatType predictorsBatch, responsesBatch;
MakeAlias(predictorsBatch, predictors.colptr(begin), predictors.n_rows, batchSize);
MakeAlias(responsesBatch, responses.colptr(begin), responses.n_rows, batchSize);
MakeAlias(predictorsBatch, predictors, predictors.n_rows, batchSize,
begin * predictors.n_rows);
MakeAlias(responsesBatch, responses, responses.n_rows, batchSize,
begin * responses.n_rows);
network.Forward(predictorsBatch, networkOutput);
return outputLayer.Forward(networkOutput, responsesBatch) + network.Loss();
@@ -497,10 +500,10 @@ typename MatType::elem_type FFN<
// Alias the batches so we don't copy memory.
MatType predictorsBatch, responsesBatch;
MakeAlias(predictorsBatch, predictors.colptr(begin), predictors.n_rows,
batchSize);
MakeAlias(responsesBatch, responses.colptr(begin), responses.n_rows,
batchSize);
MakeAlias(predictorsBatch, predictors, predictors.n_rows,
batchSize, begin * predictors.n_rows);
MakeAlias(responsesBatch, responses, responses.n_rows,
batchSize, begin * responses.n_rows);
network.Forward(predictorsBatch, networkOutput);
@@ -596,7 +599,7 @@ void FFN<
"FFN::SetLayerMemory(): total layer weight size does not match parameter "
"size!");
network.SetWeights(parameters.memptr());
network.SetWeights(parameters);
layerMemoryIsSet = true;
}
+1 -1
View File
@@ -94,7 +94,7 @@ class AddType : public Layer<MatType>
void ComputeOutputDimensions();
//! Set the weights of the layer to use the given memory.
void SetWeights(typename MatType::elem_type* weightPtr);
void SetWeights(const MatType& weightsIn);
/**
* Serialize the layer.
+2 -2
View File
@@ -95,10 +95,10 @@ void AddType<MatType>::Gradient(
}
template<typename MatType>
void AddType<MatType>::SetWeights(typename MatType::elem_type* weightPtr)
void AddType<MatType>::SetWeights(const MatType& weightsIn)
{
// Set the weights to wrap the given memory.
MakeAlias(weights, weightPtr, 1, outSize);
MakeAlias(weights, weightsIn, 1, outSize);
}
template<typename MatType>
+3 -7
View File
@@ -118,7 +118,7 @@ class BatchNormType : public Layer<MatType>
/**
* Reset the layer parameters.
*/
void SetWeights(typename MatType::elem_type* weightsPtr);
void SetWeights(const MatType& weightsIn);
/**
* Initialize the weight matrix of the layer.
@@ -126,9 +126,7 @@ class BatchNormType : public Layer<MatType>
* @param W Weight matrix to initialize.
* @param elements Number of elements.
*/
void CustomInitialize(
MatType& W,
const size_t elements);
void CustomInitialize(MatType& W, const size_t elements);
/**
* Forward pass of the Batch Normalization layer. Transforms the input data
@@ -160,9 +158,7 @@ class BatchNormType : public Layer<MatType>
* @param error The calculated error
* @param gradient The calculated gradient.
*/
void Gradient(const MatType& input,
const MatType& error,
MatType& gradient);
void Gradient(const MatType& input, const MatType& error, MatType& gradient);
//! Get the parameters.
const MatType& Parameters() const { return weights; }
@@ -148,14 +148,13 @@ BatchNormType<MatType>::operator=(
}
template<typename MatType>
void BatchNormType<MatType>::SetWeights(
typename MatType::elem_type* weightsPtr)
void BatchNormType<MatType>::SetWeights(const MatType& weightsIn)
{
MakeAlias(weights, weightsPtr, WeightSize(), 1);
MakeAlias(weights, weightsIn, WeightSize(), 1);
// Gamma acts as the scaling parameters for the normalized output.
MakeAlias(gamma, weightsPtr, size, 1);
MakeAlias(gamma, weightsIn, size, 1);
// Beta acts as the shifting parameters for the normalized output.
MakeAlias(beta, weightsPtr + gamma.n_elem, size, 1);
MakeAlias(beta, weightsIn, size, 1, gamma.n_elem);
}
template<typename MatType>
@@ -170,9 +169,9 @@ void BatchNormType<MatType>::CustomInitialize(
MatType gammaTemp;
MatType betaTemp;
// Gamma acts as the scaling parameters for the normalized output.
MakeAlias(gammaTemp, W.memptr(), size, 1);
MakeAlias(gammaTemp, W, size, 1);
// Beta acts as the shifting parameters for the normalized output.
MakeAlias(betaTemp, W.memptr() + gammaTemp.n_elem, size, 1);
MakeAlias(betaTemp, W, size, 1, gammaTemp.n_elem);
gammaTemp.fill(1.0);
betaTemp.fill(0.0);
+10 -43
View File
@@ -123,19 +123,12 @@ void ConcatType<MatType>::Forward(const MatType& input, MatType& output)
this->layerOutputs.size());
for (size_t i = 0; i < this->layerOutputs.size(); ++i)
{
MakeAlias(layerOutputAliases[i],
(typename MatType::elem_type*) this->layerOutputs[i].memptr(),
rows,
this->network[i]->OutputDimensions()[axis],
slices);
MakeAlias(layerOutputAliases[i], this->layerOutputs[i], rows,
this->network[i]->OutputDimensions()[axis], slices);
}
arma::Cube<typename MatType::elem_type> outputAlias;
MakeAlias(outputAlias,
(typename MatType::elem_type*) output.memptr(),
rows,
this->outputDimensions[axis],
slices);
MakeAlias(outputAlias, output, rows, this->outputDimensions[axis], slices);
// Now get the columns from each output.
size_t startCol = 0;
@@ -171,11 +164,7 @@ void ConcatType<MatType>::Backward(
slices *= this->outputDimensions[i];
arma::Cube<typename MatType::elem_type> gyTmp;
MakeAlias(gyTmp,
(typename MatType::elem_type*) gy.memptr(),
rows,
this->outputDimensions[axis],
slices);
MakeAlias(gyTmp, gy, rows, this->outputDimensions[axis], slices);
size_t startCol = 0;
for (size_t i = 0; i < this->network.size(); ++i)
@@ -221,11 +210,7 @@ void ConcatType<MatType>::Backward(
slices *= this->outputDimensions[i];
arma::Cube<typename MatType::elem_type> gyTmp;
MakeAlias(gyTmp,
(typename MatType::elem_type*) gy.memptr(),
rows,
this->outputDimensions[axis],
slices);
MakeAlias(gyTmp, gy, rows, this->outputDimensions[axis], slices);
size_t startCol = 0;
for (size_t i = 0; i < index; ++i)
@@ -238,11 +223,7 @@ void ConcatType<MatType>::Backward(
// Reshape so that the batch size is the number of columns.
delta.reshape(delta.n_elem / gy.n_cols, gy.n_cols);
this->network[index]->Backward(
input,
this->layerOutputs[index],
delta,
g);
this->network[index]->Backward(input, this->layerOutputs[index], delta, g);
}
template<typename MatType>
@@ -263,11 +244,7 @@ void ConcatType<MatType>::Gradient(
slices *= this->outputDimensions[i];
arma::Cube<typename MatType::elem_type> errorTmp;
MakeAlias(errorTmp,
(typename MatType::elem_type*) error.memptr(),
rows,
this->outputDimensions[axis],
slices);
MakeAlias(errorTmp, error, rows, this->outputDimensions[axis], slices);
size_t startCol = 0;
size_t startParam = 0;
@@ -279,10 +256,7 @@ void ConcatType<MatType>::Gradient(
MatType err = errorTmp.cols(startCol, startCol + cols - 1);
err.reshape(err.n_elem / input.n_cols, input.n_cols);
MatType gradientAlias;
MakeAlias(gradientAlias,
(typename MatType::elem_type*) gradient.memptr() + startParam,
params,
1);
MakeAlias(gradientAlias, gradient, params, 1, startParam);
this->network[i]->Gradient(input, err, gradientAlias);
startCol += cols;
@@ -309,11 +283,7 @@ void ConcatType<MatType>::Gradient(
slices *= this->outputDimensions[i];
arma::Cube<typename MatType::elem_type> errorTmp;
MakeAlias(errorTmp,
(typename MatType::elem_type*) error.memptr(),
rows,
this->outputDimensions[axis],
slices);
MakeAlias(errorTmp, error, rows, this->outputDimensions[axis], slices);
size_t startCol = 0;
size_t startParam = 0;
@@ -329,10 +299,7 @@ void ConcatType<MatType>::Gradient(
MatType err = errorTmp.cols(startCol, startCol + cols - 1);
err.reshape(err.n_elem / input.n_cols, input.n_cols);
MatType gradientAlias;
MakeAlias(gradientAlias,
(typename MatType::elem_type*) gradient.memptr() + startParam,
params,
1);
MakeAlias(gradientAlias, gradient, params, 1, startParam);
this->network[index]->Gradient(input, err, gradientAlias);
}
+1 -1
View File
@@ -155,7 +155,7 @@ class ConvolutionType : public Layer<MatType>
/*
* Set the weight and bias term.
*/
void SetWeights(typename MatType::elem_type* weightsPtr);
void SetWeights(const MatType& weightsIn);
/**
* Ordinary feed forward pass of a neural network, evaluating the function
@@ -272,17 +272,17 @@ void ConvolutionType<
BackwardConvolutionRule,
GradientConvolutionRule,
MatType
>::SetWeights(typename MatType::elem_type* weightPtr)
>::SetWeights(const MatType& weightsIn)
{
MakeAlias(weight, weightPtr, kernelWidth, kernelHeight, maps * inMaps);
MakeAlias(weight, weightsIn, kernelWidth, kernelHeight, maps * inMaps);
if (useBias)
{
MakeAlias(bias, weightPtr + weight.n_elem, maps, 1);
MakeAlias(weights, weightPtr, weight.n_elem + bias.n_elem, 1);
MakeAlias(bias, weightsIn, maps, 1, weight.n_elem);
MakeAlias(weights, weightsIn, weight.n_elem + bias.n_elem, 1);
}
else
{
MakeAlias(weights, weightPtr, weight.n_elem, 1);
MakeAlias(weights, weightsIn, weight.n_elem, 1);
}
}
@@ -314,11 +314,10 @@ void ConvolutionType<
}
CubeType inputTemp;
MakeAlias(inputTemp,
const_cast<MatType&>(usingPadding ? inputPadded : input).memptr(),
paddedRows, paddedCols, inMaps * higherInDimensions * batchSize);
MakeAlias(inputTemp, (usingPadding ? inputPadded : input), paddedRows,
paddedCols, inMaps * higherInDimensions * batchSize);
MakeAlias(outputTemp, output.memptr(), this->outputDimensions[0],
MakeAlias(outputTemp, output, this->outputDimensions[0],
this->outputDimensions[1], maps * higherInDimensions * batchSize);
outputTemp.zeros();
@@ -376,11 +375,11 @@ void ConvolutionType<
MatType& g)
{
CubeType mappedError;
MakeAlias(mappedError, ((MatType&) gy).memptr(), this->outputDimensions[0],
MakeAlias(mappedError, gy, this->outputDimensions[0],
this->outputDimensions[1], higherInDimensions * maps * batchSize);
MakeAlias(gTemp, g.memptr(), this->inputDimensions[0],
this->inputDimensions[1], inMaps * higherInDimensions * batchSize);
MakeAlias(gTemp, g, this->inputDimensions[0], this->inputDimensions[1],
inMaps * higherInDimensions * batchSize);
gTemp.zeros();
const bool usingPadding =
@@ -394,8 +393,8 @@ void ConvolutionType<
CubeType dilatedMappedError;
if (strideHeight == 1 && strideWidth == 1)
{
MakeAlias(dilatedMappedError, mappedError.memptr(),
mappedError.n_rows, mappedError.n_cols, mappedError.n_slices);
MakeAlias(dilatedMappedError, mappedError, mappedError.n_rows,
mappedError.n_cols, mappedError.n_slices);
}
else
{
@@ -425,7 +424,7 @@ void ConvolutionType<
MatType output(apparentWidth * apparentHeight * inMaps * higherInDimensions,
batchSize, arma::fill::zeros);
CubeType outputCube;
MakeAlias(outputCube, output.memptr(), apparentWidth, apparentHeight,
MakeAlias(outputCube, output, apparentWidth, apparentHeight,
inMaps * higherInDimensions * batchSize);
// See Forward() for the overall iteration strategy.
@@ -457,7 +456,7 @@ void ConvolutionType<
MatType temp(padding.OutputDimensions()[0] * padding.OutputDimensions()[1] *
inMaps * higherInDimensions, batchSize);
CubeType tempCube;
MakeAlias(tempCube, temp.memptr(), padding.OutputDimensions()[0],
MakeAlias(tempCube, temp, padding.OutputDimensions()[0],
padding.OutputDimensions()[1], inMaps * higherInDimensions * batchSize);
paddingBackward.Forward(output, temp);
if (usingPadding)
@@ -491,9 +490,8 @@ void ConvolutionType<
MatType& gradient)
{
CubeType mappedError;
MakeAlias(mappedError, ((MatType&) error).memptr(),
this->outputDimensions[0], this->outputDimensions[1],
higherInDimensions * maps * batchSize);
MakeAlias(mappedError, error, this->outputDimensions[0],
this->outputDimensions[1], higherInDimensions * maps * batchSize);
// We are depending here on `inputPadded` being properly set from a call to
// Forward().
@@ -509,7 +507,7 @@ void ConvolutionType<
MatType temp(apparentWidth * apparentHeight * inMaps * higherInDimensions,
batchSize);
CubeType tempCube;
MakeAlias(tempCube, temp.memptr(), apparentWidth, apparentHeight,
MakeAlias(tempCube, temp, apparentWidth, apparentHeight,
inMaps * higherInDimensions * batchSize);
paddingBackward.Backward(input, {} /* unused */, usingPadding ? inputPadded : input, temp);
@@ -517,8 +515,7 @@ void ConvolutionType<
// convolution map weights! The bias will be handled by direct accesses into
// `gradient`.
gradient.zeros();
MakeAlias(gradientTemp, gradient.memptr(), weight.n_rows, weight.n_cols,
weight.n_slices);
MakeAlias(gradientTemp, gradient, weight.n_rows, weight.n_cols, weight.n_slices);
// See Forward() for our iteration strategy.
for (size_t offset = 0; offset < higherInDimensions * batchSize; ++offset)
+1 -1
View File
@@ -124,7 +124,7 @@ class DropConnectType : public Layer<MatType>
size_t WeightSize() const { return baseLayer->WeightSize(); }
// Set the weights to use the given memory `weightsPtr`.
void SetWeights(typename MatType::elem_type* weightsPtr);
void SetWeights(const MatType& weightsIn);
/**
* Serialize the layer.
@@ -155,10 +155,9 @@ void DropConnectType<MatType>::ComputeOutputDimensions()
}
template<typename MatType>
void DropConnectType<MatType>::SetWeights(
typename MatType::elem_type* weightsPtr)
void DropConnectType<MatType>::SetWeights(const MatType& weightsIn)
{
baseLayer->SetWeights(weightsPtr);
baseLayer->SetWeights(weightsIn);
}
template<typename MatType>
@@ -83,7 +83,7 @@ class FlexibleReLUType : public Layer<MatType>
* Reset the layer parameter (alpha). The method is called to
* assign the allocated memory to the learnable layer parameter.
*/
void SetWeights(typename MatType::elem_type* weightsPtr);
void SetWeights(const MatType& weightsIn);
/**
* Initialize the weight matrix of the layer.
@@ -74,10 +74,9 @@ FlexibleReLUType<MatType>::operator=(FlexibleReLUType&& other)
}
template<typename MatType>
void FlexibleReLUType<MatType>::SetWeights(
typename MatType::elem_type* weightsPtr)
void FlexibleReLUType<MatType>::SetWeights(const MatType& weights)
{
MakeAlias(alpha, weightsPtr, 1, 1);
MakeAlias(alpha, weights, 1, 1);
}
template<typename MatType>
@@ -163,7 +163,7 @@ class GroupedConvolutionType : public Layer<MatType>
/*
* Set the weight and bias term.
*/
void SetWeights(typename MatType::elem_type* weightsPtr);
void SetWeights(const MatType& weightsIn);
/**
* Ordinary feed forward pass of a neural network, evaluating the function
@@ -281,18 +281,18 @@ void GroupedConvolutionType<
BackwardConvolutionRule,
GradientConvolutionRule,
MatType
>::SetWeights(typename MatType::elem_type* weightPtr)
>::SetWeights(const MatType& weightsIn)
{
MakeAlias(weight, weightPtr, kernelWidth, kernelHeight,
MakeAlias(weight, weightsIn, kernelWidth, kernelHeight,
(maps * inMaps) / groups);
if (useBias)
{
MakeAlias(bias, weightPtr + weight.n_elem, maps, 1);
MakeAlias(weights, weightPtr, weight.n_elem + bias.n_elem, 1);
MakeAlias(bias, weightsIn, maps, 1, weight.n_elem);
MakeAlias(weights, weightsIn, weight.n_elem + bias.n_elem, 1);
}
else
{
MakeAlias(weights, weightPtr, weight.n_elem, 1);
MakeAlias(weights, weightsIn, weight.n_elem, 1);
}
}
@@ -324,11 +324,10 @@ void GroupedConvolutionType<
}
CubeType inputTemp;
MakeAlias(inputTemp,
const_cast<MatType&>(usingPadding ? inputPadded : input).memptr(),
MakeAlias(inputTemp, (usingPadding ? inputPadded : input),
paddedRows, paddedCols, inMaps * higherInDimensions * batchSize);
MakeAlias(outputTemp, output.memptr(), this->outputDimensions[0],
MakeAlias(outputTemp, output, this->outputDimensions[0],
this->outputDimensions[1], maps * higherInDimensions * batchSize);
outputTemp.zeros();
@@ -394,11 +393,11 @@ void GroupedConvolutionType<
MatType& g)
{
CubeType mappedError;
MakeAlias(mappedError, ((MatType&) gy).memptr(), this->outputDimensions[0],
MakeAlias(mappedError, gy, this->outputDimensions[0],
this->outputDimensions[1], higherInDimensions * maps * batchSize);
MakeAlias(gTemp, g.memptr(), this->inputDimensions[0],
this->inputDimensions[1], inMaps * higherInDimensions * batchSize);
MakeAlias(gTemp, g, this->inputDimensions[0], this->inputDimensions[1],
inMaps * higherInDimensions * batchSize);
gTemp.zeros();
const bool usingPadding =
@@ -418,8 +417,8 @@ void GroupedConvolutionType<
CubeType dilatedMappedError;
if (strideHeight == 1 && strideWidth == 1)
{
MakeAlias(dilatedMappedError, mappedError.memptr(),
mappedError.n_rows, mappedError.n_cols, mappedError.n_slices);
MakeAlias(dilatedMappedError, mappedError, mappedError.n_rows,
mappedError.n_cols, mappedError.n_slices);
}
else
{
@@ -443,7 +442,7 @@ void GroupedConvolutionType<
MatType output(apparentWidth * apparentHeight * inMaps * higherInDimensions,
batchSize, arma::fill::zeros);
CubeType outputCube;
MakeAlias(outputCube, output.memptr(), apparentWidth, apparentHeight,
MakeAlias(outputCube, output, apparentWidth, apparentHeight,
inMaps * higherInDimensions * batchSize);
size_t inGroupSize = inMaps / groups;
@@ -484,7 +483,7 @@ void GroupedConvolutionType<
MatType temp(padding.OutputDimensions()[0] * padding.OutputDimensions()[1] *
inMaps * higherInDimensions, batchSize);
CubeType tempCube;
MakeAlias(tempCube, temp.memptr(), padding.OutputDimensions()[0],
MakeAlias(tempCube, temp, padding.OutputDimensions()[0],
padding.OutputDimensions()[1], inMaps * higherInDimensions * batchSize);
paddingBackward.Forward(output, temp);
if (usingPadding)
@@ -518,9 +517,8 @@ void GroupedConvolutionType<
MatType& gradient)
{
CubeType mappedError;
MakeAlias(mappedError, ((MatType&) error).memptr(),
this->outputDimensions[0], this->outputDimensions[1],
higherInDimensions * maps * batchSize);
MakeAlias(mappedError, error, this->outputDimensions[0],
this->outputDimensions[1], higherInDimensions * maps * batchSize);
// We are depending here on `inputPadded` being properly set from a call to
// Forward().
@@ -536,7 +534,7 @@ void GroupedConvolutionType<
MatType temp(apparentWidth * apparentHeight * inMaps * higherInDimensions,
batchSize);
CubeType tempCube;
MakeAlias(tempCube, temp.memptr(), apparentWidth, apparentHeight,
MakeAlias(tempCube, temp, apparentWidth, apparentHeight,
inMaps * higherInDimensions * batchSize);
paddingBackward.Backward(input, {} /* unused */, usingPadding ? inputPadded : input, temp);
@@ -544,7 +542,7 @@ void GroupedConvolutionType<
// convolution map weights! The bias will be handled by direct accesses into
// `gradient`.
gradient.zeros();
MakeAlias(gradientTemp, gradient.memptr(), weight.n_rows, weight.n_cols,
MakeAlias(gradientTemp, gradient, weight.n_rows, weight.n_cols,
weight.n_slices);
size_t inGroupSize = inMaps / groups;
+1 -1
View File
@@ -191,7 +191,7 @@ class Layer
* implementations should use MakeAlias() with weightsPtr to wrap the
* weights of a layer.
*/
virtual void SetWeights(typename MatType::elem_type* /* weightsPtr */) { }
virtual void SetWeights(const MatType& /* weightsIn */) { }
/**
* Get the total number of trainable weights in the layer.
+1 -1
View File
@@ -134,7 +134,7 @@ class LayerNormType : public Layer<MatType>
size *= this->inputDimensions[i];
}
void SetWeights(typename MatType::elem_type* /* weightsPtr */) override;
void SetWeights(const MatType& weightsIn) override;
void CustomInitialize(
MatType& /* W */,
@@ -27,12 +27,11 @@ LayerNormType<MatType>::LayerNormType(const double eps) :
}
template<typename MatType>
void LayerNormType<MatType>::SetWeights(
typename MatType::elem_type* weightsPtr)
void LayerNormType<MatType>::SetWeights(const MatType& weightsIn)
{
MakeAlias(weights, weightsPtr, 2 * size, 1);
MakeAlias(gamma, weightsPtr, size, 1);
MakeAlias(beta, weightsPtr + gamma.n_elem, size, 1);
MakeAlias(weights, weightsIn, 2 * size, 1);
MakeAlias(gamma, weightsIn, size, 1);
MakeAlias(beta, weightsIn, size, 1, gamma.n_elem);
}
template<typename MatType>
@@ -48,9 +47,9 @@ void LayerNormType<MatType>::CustomInitialize(
MatType gammaTemp;
MatType betaTemp;
// Gamma acts as the scaling parameters for the normalized output.
MakeAlias(gammaTemp, W.memptr(), size, 1);
MakeAlias(gammaTemp, W, size, 1);
// Beta acts as the shifting parameters for the normalized output.
MakeAlias(betaTemp, W.memptr() + gammaTemp.n_elem, size, 1);
MakeAlias(betaTemp, W, size, 1, gammaTemp.n_elem);
gammaTemp.fill(1.0);
betaTemp.fill(0.0);
+1 -1
View File
@@ -76,7 +76,7 @@ class LinearType : public Layer<MatType>
* Reset the layer parameter (weights and bias). The method is called to
* assign the allocated memory to the internal learnable parameters.
*/
void SetWeights(typename MatType::elem_type* weightsPtr);
void SetWeights(const MatType& weightsIn);
/**
* Ordinary feed forward pass of a neural network, evaluating the function
+1 -1
View File
@@ -68,7 +68,7 @@ class Linear3DType : public Layer<MatType>
/*
* Reset the layer parameter.
*/
void SetWeights(typename MatType::elem_type* weightsPtr);
void SetWeights(const MatType& weightsIn);
/**
* Ordinary feed forward pass of a neural network, evaluating the function
@@ -85,13 +85,12 @@ Linear3DType<MatType, RegularizerType>::operator=(
}
template<typename MatType, typename RegularizerType>
void Linear3DType<MatType, RegularizerType>::SetWeights(
typename MatType::elem_type* weightsPtr)
void Linear3DType<MatType, RegularizerType>::SetWeights(const MatType& weightsIn)
{
MakeAlias(weights, weightsPtr, outSize * this->inputDimensions[0] + outSize,
MakeAlias(weights, weightsIn, outSize * this->inputDimensions[0] + outSize,
1);
MakeAlias(weight, weightsPtr, outSize, this->inputDimensions[0]);
MakeAlias(bias, weightsPtr + weight.n_elem, outSize, 1);
MakeAlias(weight, weightsIn, outSize, this->inputDimensions[0]);
MakeAlias(bias, weightsIn, outSize, 1, weight.n_elem);
}
template<typename MatType, typename RegularizerType>
+4 -5
View File
@@ -93,12 +93,11 @@ LinearType<MatType, RegularizerType>::operator=(
}
template<typename MatType, typename RegularizerType>
void LinearType<MatType, RegularizerType>::SetWeights(
typename MatType::elem_type* weightsPtr)
void LinearType<MatType, RegularizerType>::SetWeights(const MatType& weightsIn)
{
MakeAlias(weights, weightsPtr, outSize * inSize + outSize, 1);
MakeAlias(weight, weightsPtr, outSize, inSize);
MakeAlias(bias, weightsPtr + weight.n_elem, outSize, 1);
MakeAlias(weights, weightsIn, outSize * inSize + outSize, 1);
MakeAlias(weight, weightsIn, outSize, inSize);
MakeAlias(bias, weightsIn, outSize, 1, weight.n_elem);
}
template<typename MatType, typename RegularizerType>
@@ -52,7 +52,7 @@ class LinearNoBiasType : public Layer<MatType>
LinearNoBiasType* Clone() const { return new LinearNoBiasType(*this); }
//! Reset the layer parameter.
void SetWeights(typename MatType::elem_type* weightsPtr);
void SetWeights(const MatType& weightsIn);
//! Copy constructor.
LinearNoBiasType(const LinearNoBiasType& layer);
@@ -95,9 +95,9 @@ LinearNoBiasType<MatType, RegularizerType>::operator=(
template<typename MatType, typename RegularizerType>
void LinearNoBiasType<MatType, RegularizerType>::SetWeights(
typename MatType::elem_type* weightsPtr)
const MatType& weights)
{
MakeAlias(weight, weightsPtr, outSize, inSize);
MakeAlias(weight, weights, outSize, inSize);
}
template<typename MatType, typename RegularizerType>
+1 -1
View File
@@ -87,7 +87,7 @@ class LSTMType : public RecurrentLayer<MatType>
* Reset the layer parameter. The method is called to
* assign the allocated memory to the internal learnable parameters.
*/
void SetWeights(typename MatType::elem_type* weightsPtr);
void SetWeights(const MatType& weightsIn);
/**
* Ordinary feed-forward pass of a neural network, evaluating the function
+16 -17
View File
@@ -93,59 +93,58 @@ void LSTMType<MatType>::ClearRecurrentState(
}
template<typename MatType>
void LSTMType<MatType>::SetWeights(
typename MatType::elem_type* weightsPtr)
void LSTMType<MatType>::SetWeights(const MatType& weights)
{
// Set the weight parameter for the output gate.
MakeAlias(input2GateOutputWeight, weightsPtr, outSize, inSize);
MakeAlias(input2GateOutputWeight, weights, outSize, inSize);
size_t offset = input2GateOutputWeight.n_elem;
MakeAlias(input2GateOutputBias, weightsPtr + offset, outSize, 1);
MakeAlias(input2GateOutputBias, weights, outSize, 1, offset);
offset += input2GateOutputBias.n_elem;
// Set the weight parameter for the forget gate.
MakeAlias(input2GateForgetWeight, weightsPtr + offset, outSize, inSize);
MakeAlias(input2GateForgetWeight, weights, outSize, inSize, offset);
offset += input2GateForgetWeight.n_elem;
MakeAlias(input2GateForgetBias, weightsPtr + offset, outSize, 1);
MakeAlias(input2GateForgetBias, weights, outSize, 1, offset);
offset += input2GateForgetBias.n_elem;
// Set the weight parameter for the input gate.
MakeAlias(input2GateInputWeight, weightsPtr + offset, outSize, inSize);
MakeAlias(input2GateInputWeight, weights, outSize, inSize, offset);
offset += input2GateInputWeight.n_elem;
MakeAlias(input2GateInputBias, weightsPtr + offset, outSize, 1);
MakeAlias(input2GateInputBias, weights, outSize, 1, offset);
offset += input2GateInputBias.n_elem;
// Set the weight parameter for the hidden gate.
MakeAlias(input2HiddenWeight, weightsPtr + offset, outSize, inSize);
MakeAlias(input2HiddenWeight, weights, outSize, inSize, offset);
offset += input2HiddenWeight.n_elem;
MakeAlias(input2HiddenBias, weightsPtr + offset, outSize, 1);
MakeAlias(input2HiddenBias, weights, outSize, 1, offset);
offset += input2HiddenBias.n_elem;
// Set the weight parameter for the output multiplication.
MakeAlias(output2GateOutputWeight, weightsPtr + offset, outSize, outSize);
MakeAlias(output2GateOutputWeight, weights, outSize, outSize, offset);
offset += output2GateOutputWeight.n_elem;
// Set the weight parameter for the output multiplication.
MakeAlias(output2GateForgetWeight, weightsPtr + offset, outSize, outSize);
MakeAlias(output2GateForgetWeight, weights, outSize, outSize, offset);
offset += output2GateForgetWeight.n_elem;
// Set the weight parameter for the input multiplication.
MakeAlias(output2GateInputWeight, weightsPtr + offset, outSize, outSize);
MakeAlias(output2GateInputWeight, weights, outSize, outSize, offset);
offset += output2GateInputWeight.n_elem;
// Set the weight parameter for the hidden multiplication.
MakeAlias(output2HiddenWeight, weightsPtr + offset, outSize, outSize);
MakeAlias(output2HiddenWeight, weights, outSize, outSize, offset);
offset += output2HiddenWeight.n_elem;
// Set the weight parameter for the cell multiplication.
MakeAlias(cell2GateOutputWeight, weightsPtr + offset, outSize, 1);
MakeAlias(cell2GateOutputWeight, weights, outSize, 1, offset);
offset += cell2GateOutputWeight.n_elem;
// Set the weight parameter for the cell - forget gate multiplication.
MakeAlias(cell2GateForgetWeight, weightsPtr + offset, outSize, 1);
MakeAlias(cell2GateForgetWeight, weights, outSize, 1, offset);
offset += cell2GateOutputWeight.n_elem;
// Set the weight parameter for the cell - input gate multiplication.
MakeAlias(cell2GateInputWeight, weightsPtr + offset, outSize, 1);
MakeAlias(cell2GateInputWeight, weights, outSize, 1, offset);
}
// Forward when cellState is not needed.
+1 -1
View File
@@ -128,7 +128,7 @@ class MultiLayer : public Layer<MatType>
/**
* Set the weights of the layer to use the memory given as `weightsPtr`.
*/
virtual void SetWeights(typename MatType::elem_type* weightsPtr);
virtual void SetWeights(const MatType& weightsIn);
/**
* Initialize the weight matrix of the layer.
@@ -235,7 +235,7 @@ void MultiLayer<MatType>::Gradient(
}
template<typename MatType>
void MultiLayer<MatType>::SetWeights(typename MatType::elem_type* weightsPtr)
void MultiLayer<MatType>::SetWeights(const MatType& weightsIn)
{
size_t start = 0;
const size_t totalWeightSize = WeightSize();
@@ -248,8 +248,9 @@ void MultiLayer<MatType>::SetWeights(typename MatType::elem_type* weightsPtr)
Log::Assert(start + weightSize <= totalWeightSize,
"FNN::SetLayerMemory(): parameter size does not match total layer "
"weight size!");
network[i]->SetWeights(weightsPtr + start);
MatType tmpWeights;
MakeAlias(tmpWeights, weightsIn, weightSize, 1, start);
network[i]->SetWeights(tmpWeights);
start += weightSize;
}
@@ -278,7 +279,7 @@ void MultiLayer<MatType>::CustomInitialize(
"weight size!");
MatType WTemp;
MakeAlias(WTemp, W.memptr() + start, weightSize, 1);
MakeAlias(WTemp, W, weightSize, 1, start);
network[i]->CustomInitialize(WTemp, weightSize);
start += weightSize;
@@ -388,8 +389,8 @@ void MultiLayer<MatType>::InitializeForwardPassMemory(const size_t batchSize)
for (size_t i = 0; i < layerOutputs.size(); ++i)
{
const size_t layerOutputSize = network[i]->OutputSize();
MakeAlias(layerOutputs[i], layerOutputMatrix.colptr(start),
layerOutputSize, batchSize);
MakeAlias(layerOutputs[i], layerOutputMatrix, layerOutputSize, batchSize,
start * layerOutputMatrix.n_rows);
start += batchSize * layerOutputSize;
}
}
@@ -417,8 +418,8 @@ void MultiLayer<MatType>::InitializeBackwardPassMemory(
for (size_t j = 0; j < this->network[i]->InputDimensions().size(); ++j)
layerInputSize *= this->network[i]->InputDimensions()[j];
MakeAlias(layerDeltas[i], layerDeltaMatrix.colptr(start), layerInputSize,
batchSize);
MakeAlias(layerDeltas[i], layerDeltaMatrix, layerInputSize,
batchSize, start * layerDeltaMatrix.n_rows);
start += batchSize * layerInputSize;
}
}
@@ -432,8 +433,7 @@ void MultiLayer<MatType>::InitializeGradientPassMemory(MatType& gradient)
for (size_t i = 0; i < network.size(); ++i)
{
const size_t weightSize = network[i]->WeightSize();
MakeAlias(layerGradients[i], gradient.memptr() + gradientStart,
weightSize, 1);
MakeAlias(layerGradients[i], gradient, weightSize, 1, gradientStart);
gradientStart += weightSize;
}
}
@@ -102,7 +102,7 @@ class MultiheadAttentionType : public Layer<MatType>
/**
* Reset the layer parameters.
*/
void SetWeights(typename MatType::elem_type* weightsPtr) override;
void SetWeights(const MatType& weightsIn) override;
/**
* Ordinary feed forward pass of a neural network, evaluating the function
@@ -54,19 +54,19 @@ MultiheadAttentionType(
template <typename MatType, typename RegularizerType>
void MultiheadAttentionType<MatType, RegularizerType>::SetWeights(
typename MatType::elem_type* weightsPtr)
const MatType& weightsIn)
{
MakeAlias(weights, weightsPtr, (4 * embedDim + 4) * embedDim, 1);
MakeAlias(weights, weightsIn, (4 * embedDim + 4) * embedDim, 1);
MakeAlias(queryWt, weightsPtr, embedDim, embedDim);
MakeAlias(keyWt, weightsPtr + embedDim * embedDim, embedDim, embedDim);
MakeAlias(valueWt, weightsPtr + 2 * embedDim * embedDim, embedDim, embedDim);
MakeAlias(outWt, weightsPtr + 3 * embedDim * embedDim, embedDim, embedDim);
MakeAlias(queryWt, weightsIn, embedDim, embedDim);
MakeAlias(keyWt, weightsIn, embedDim, embedDim, embedDim * embedDim);
MakeAlias(valueWt, weightsIn, embedDim, embedDim, 2 * embedDim * embedDim);
MakeAlias(outWt, weightsIn, embedDim, embedDim, 3 * embedDim * embedDim);
MakeAlias(qBias, weightsPtr + 4 * embedDim * embedDim, embedDim, 1);
MakeAlias(kBias, weightsPtr + (4 * embedDim + 1) * embedDim, embedDim, 1);
MakeAlias(vBias, weightsPtr + (4 * embedDim + 2) * embedDim, embedDim, 1);
MakeAlias(outBias, weightsPtr + (4 * embedDim + 3) * embedDim, 1, embedDim);
MakeAlias(qBias, weightsIn, embedDim, 1, 4 * embedDim * embedDim);
MakeAlias(kBias, weightsIn, embedDim, 1, (4 * embedDim + 1) * embedDim);
MakeAlias(vBias, weightsIn, embedDim, 1, (4 * embedDim + 2) * embedDim);
MakeAlias(outBias, weightsIn, 1, embedDim, (4 * embedDim + 3) * embedDim);
}
template <typename MatType, typename RegularizerType>
+1 -1
View File
@@ -52,7 +52,7 @@ class NoisyLinearType : public Layer<MatType>
NoisyLinearType& operator=(NoisyLinearType&& other);
//! Reset the layer parameter.
void SetWeights(typename MatType::elem_type* weightsPtr);
void SetWeights(const MatType& weightsIn);
//! Reset the noise parameters (epsilons).
void ResetNoise();
@@ -73,17 +73,15 @@ NoisyLinearType<MatType>::operator=(NoisyLinearType&& other)
}
template<typename MatType>
void NoisyLinearType<MatType>::SetWeights(
typename MatType::elem_type* weightsPtr)
void NoisyLinearType<MatType>::SetWeights(const MatType& weightsIn)
{
MakeAlias(weights, weightsPtr, 1, (outSize * inSize + outSize) * 2);
MakeAlias(weightMu, weightsPtr, outSize, inSize);
MakeAlias(biasMu, weightsPtr + weightMu.n_elem, outSize, 1);
MakeAlias(weightSigma, weightsPtr + weightMu.n_elem + biasMu.n_elem, outSize,
inSize);
MakeAlias(biasSigma, weightsPtr + weightMu.n_elem * 2 + biasMu.n_elem,
outSize, 1);
MakeAlias(weights, weightsIn, 1, (outSize * inSize + outSize) * 2);
MakeAlias(weightMu, weightsIn, outSize, inSize);
MakeAlias(biasMu, weightsIn, outSize, 1, weightMu.n_elem);
MakeAlias(weightSigma, weightsIn, outSize, inSize,
weightMu.n_elem + biasMu.n_elem);
MakeAlias(biasSigma, weightsIn, outSize, 1,
weightMu.n_elem * 2 + biasMu.n_elem);
this->ResetNoise();
}
@@ -68,7 +68,7 @@ class PReLUType : public Layer<MatType>
PReLUType& operator=(PReLUType&& other);
//! Reset the layer parameter.
void SetWeights(typename MatType::elem_type* weightsPtr);
void SetWeights(const MatType& weightsIn);
/**
* Initialize the weight matrix of the layer.
@@ -73,10 +73,9 @@ PReLUType<MatType>::operator=(PReLUType&& other)
}
template<typename MatType>
void PReLUType<MatType>::SetWeights(
typename MatType::elem_type* weightsPtr)
void PReLUType<MatType>::SetWeights(const MatType& weightsIn)
{
MakeAlias(alpha, weightsPtr, 1, 1);
MakeAlias(alpha, weightsIn, 1, 1);
}
template<typename MatType>
+27 -27
View File
@@ -239,11 +239,10 @@ void RNN<
SetPreviousStep(size_t(0));
// Create aliases for the input and output.
MakeAlias(inputAlias,
(typename MatType::elem_type*) predictors.slice(t).colptr(i),
predictors.n_rows, effectiveBatchSize);
MakeAlias(outputAlias, results.slice(t).colptr(i), results.n_rows,
effectiveBatchSize);
MakeAlias(inputAlias, predictors.slice(t), predictors.n_rows,
effectiveBatchSize, i * predictors.slice(t).n_rows);
MakeAlias(outputAlias, results.slice(t), results.n_rows,
effectiveBatchSize, i * results.slice(t).n_rows);
network.Forward(inputAlias, outputAlias);
}
@@ -349,11 +348,12 @@ typename MatType::elem_type RNN<
// Manually reset the data of the network to be an alias of the current time
// step.
MakeAlias(network.predictors, predictors.slice(t).colptr(begin),
predictors.n_rows, batchSize);
MakeAlias(network.predictors, predictors.slice(t), predictors.n_rows,
batchSize, begin * predictors.slice(t).n_rows);
const size_t responseStep = (single) ? 0 : t;
MakeAlias(network.responses, responses.slice(responseStep).colptr(begin),
responses.n_rows, batchSize);
MakeAlias(network.responses, responses.slice(responseStep),
responses.n_rows, batchSize,
begin * responses.slice(responseStep).n_rows);
loss += network.Evaluate(output, begin, batchSize);
}
@@ -418,15 +418,15 @@ typename MatType::elem_type RNN<
SetCurrentStep(0);
// Make an alias of the step's data.
MakeAlias(stepData, predictors.slice(t).colptr(begin), predictors.n_rows,
batchSize);
MakeAlias(outputData, outputs.slice(t).memptr(), outputs.n_rows,
outputs.n_cols);
MakeAlias(stepData, predictors.slice(t), predictors.n_rows, batchSize,
begin * predictors.slice(t).n_rows);
MakeAlias(outputData, outputs.slice(t), outputs.n_rows, outputs.n_cols);
network.network.Forward(stepData, outputData);
const size_t responseStep = (single) ? 0 : t;
MakeAlias(responseData, responses.slice(responseStep).colptr(begin),
responses.n_rows, batchSize);
MakeAlias(responseData, responses.slice(responseStep),
responses.n_rows, batchSize,
begin * responses.slice(responseStep).n_rows);
loss += network.outputLayer.Forward(outputData, responseData);
@@ -440,15 +440,15 @@ typename MatType::elem_type RNN<
SetCurrentStep(t - extraSteps + 1);
// Wrap a matrix around our data to avoid a copy.
MakeAlias(stepData, predictors.slice(t).colptr(begin), predictors.n_rows,
batchSize);
MakeAlias(outputData, outputs.slice(t).memptr(), outputs.n_rows,
outputs.n_cols);
MakeAlias(stepData, predictors.slice(t), predictors.n_rows, batchSize,
begin * predictors.slice(t).n_rows);
MakeAlias(outputData, outputs.slice(t), outputs.n_rows, outputs.n_cols);
network.network.Forward(stepData, outputData);
const size_t responseStep = (single) ? 0 : t;
MakeAlias(responseData, responses.slice(responseStep).colptr(begin),
responses.n_rows, batchSize);
MakeAlias(responseData, responses.slice(responseStep),
responses.n_rows, batchSize,
begin * responses.slice(responseStep).n_rows);
loss += network.outputLayer.Forward(outputData, responseData);
@@ -484,18 +484,18 @@ typename MatType::elem_type RNN<
}
else
{
MakeAlias(outputData, outputs.slice(t - 1).colptr(0), outputs.n_rows,
MakeAlias(outputData, outputs.slice(t - 1), outputs.n_rows,
outputs.n_cols);
const size_t respStep = (single) ? 0 : t - 1;
MakeAlias(responseData, responses.slice(respStep).colptr(begin),
responses.n_rows, batchSize);
MakeAlias(responseData, responses.slice(respStep), responses.n_rows,
batchSize, begin * responses.slice(respStep).n_rows);
network.outputLayer.Backward(outputData, responseData, error);
}
// Now pass that error backwards through the network.
MakeAlias(stepData, predictors.slice(t - 1).colptr(begin),
predictors.n_rows, batchSize);
MakeAlias(outputData, outputs.slice(t - 1).colptr(0), outputs.n_rows,
MakeAlias(stepData, predictors.slice(t - 1), predictors.n_rows, batchSize,
begin * predictors.slice(t - 1).n_rows);
MakeAlias(outputData, outputs.slice(t - 1), outputs.n_rows,
outputs.n_cols);
MatType networkDelta;
@@ -72,20 +72,20 @@ inline void RandomizedBlockKrylovSVD::Apply(const InMatType& data,
// Create a working matrix using data from writable auxiliary memory
// (K matrix). Doing so avoids an unnecessary copy in upcoming step.
MakeAlias(block, K.memptr(), data.n_rows, blockSize, false);
MakeAlias(block, K, data.n_rows, blockSize, false);
arma::qr_econ(block, R, data * G);
for (size_t blockOffset = block.n_elem; blockOffset < K.n_elem;
blockOffset += block.n_elem)
{
// Temporary working matrix to store the result in the correct place.
MakeAlias(blockIteration, K.memptr() + blockOffset, block.n_rows,
block.n_cols, false);
MakeAlias(blockIteration, K, block.n_rows, block.n_cols, blockOffset,
false);
arma::qr_econ(blockIteration, R, data * (data.t() * block));
// Update working matrix for the next iteration.
MakeAlias(block, K.memptr() + blockOffset, block.n_rows, block.n_cols,
MakeAlias(block, K, block.n_rows, block.n_cols, blockOffset,
false);
}
@@ -57,8 +57,8 @@ BINDING_LONG_DESC(
"the minimum gain that is needed for the node to split. The " +
PRINT_PARAM_STRING("maximum_depth") + " parameter specifies "
"the maximum depth of the tree. If " +
PRINT_PARAM_STRING("print_training_error") + " is specified, the training "
"error will be printed."
PRINT_PARAM_STRING("print_training_accuracy") + " is specified, the "
"training accuracy will be printed."
"\n\n"
"Test data may be specified with the " + PRINT_PARAM_STRING("test") + " "
"parameter, and if performance numbers are desired for that test set, "
@@ -115,9 +115,6 @@ PARAM_DOUBLE_IN("minimum_gain_split", "Minimum gain for node splitting.", "g",
1e-7);
PARAM_INT_IN("maximum_depth", "Maximum depth of the tree (0 means no limit).",
"D", 0);
// This is deprecated and should be removed in mlpack 4.0.0.
PARAM_FLAG("print_training_error", "Print the training error (deprecated; will "
"be removed in mlpack 4.0.0).", "e");
PARAM_FLAG("print_training_accuracy", "Print the training accuracy.", "a");
// Output parameters.
@@ -180,12 +177,6 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& /* timers */)
[](double x) { return (x > 0.0 && x < 1.0); }, true,
"gain split must be a fraction in range [0,1]");
if (params.Has("print_training_error"))
{
Log::Warn << "The option " << PRINT_PARAM_STRING("print_training_error")
<< " is deprecated and will be removed in mlpack 4.0.0." << std::endl;
}
// Load the model or build the tree.
DecisionTreeModel* model;
arma::mat trainingSet;
@@ -223,8 +214,7 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& /* timers */)
{
arma::Row<double> weights =
std::move(params.Get<arma::Mat<double>>("weights"));
if (params.Has("print_training_error") ||
params.Has("print_training_accuracy"))
if (params.Has("print_training_accuracy"))
{
model->tree = DecisionTree<>(trainingSet, model->info, labels,
numClasses, std::move(weights), minLeafSize, minimumGainSplit,
@@ -239,7 +229,7 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& /* timers */)
}
else
{
if (params.Has("print_training_error"))
if (params.Has("print_training_accuracy"))
{
model->tree = DecisionTree<>(trainingSet, model->info, labels,
numClasses, minLeafSize, minimumGainSplit, maxDepth);
@@ -253,8 +243,7 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& /* timers */)
}
// Do we need to print training error?
if (params.Has("print_training_error") ||
params.Has("print_training_accuracy"))
if (params.Has("print_training_accuracy"))
{
arma::Row<size_t> predictions;
arma::mat probabilities;
+6 -26
View File
@@ -658,31 +658,6 @@ LARS<ModelMatType>::Train(const MatType& matX,
if (maxCorr < tolerance)
break;
// Floats require a really large tolerance for this condition.
const ElemType tol = (std::is_same<ElemType, double>::value) ? 1e-6 : 0.01;
if ((matGram != &matGramInternal) &&
((maxActiveCorr - minActiveCorr) / maxActiveCorr) > tol)
{
// Construct the error message to match the user's settings.
std::cout << "maxActiveCorr: " << maxActiveCorr << " minActiveCorr: " << minActiveCorr << "; result " << ((maxActiveCorr - minActiveCorr) / maxActiveCorr) << "; tol " << tol << "\n";
std::ostringstream oss;
oss << "LARS::Train(): correlation conditions violated; check that your "
<< "given Gram matrix is properly computed on ";
if (fitIntercept)
oss << "mean-centered ";
else
oss << "non-mean-centered ";
if (normalizeData)
oss << "unit-variance (normalized) ";
else
oss << "non-normalized ";
oss << "data";
if (lambda2 > 0.0)
oss << " with lambda2 = " << lambda2 << " added to the diagonal";
oss << "!";
throw std::runtime_error(oss.str());
}
// Add the variable to the active set and update the Gram matrix as
// necessary.
if (!lassocond)
@@ -707,7 +682,10 @@ LARS<ModelMatType>::Train(const MatType& matX,
// Compute signs of correlations.
arma::Col<ElemType> s(activeSet.size());
for (size_t i = 0; i < activeSet.size(); ++i)
s(i) = corr(activeSet[i]) / fabs(corr(activeSet[i]));
{
const size_t j = activeSet[i];
s[i] = (ElemType) (corr(j) == 0.0 ? 0.0 : (corr(j) > 0) ? 1.0 : -1.0);
}
// Compute the "equiangular" direction in parameter space (betaDirection).
// We use quotes because in the case of non-unit norm variables, this need
@@ -794,6 +772,8 @@ LARS<ModelMatType>::Train(const MatType& matX,
// need to take a step with the previous beta direction towards the next
// variable we will add.
s = s.subvec(0, activeSet.size() - 1); // Drop last element.
matGramActive = matGramActive.submat(0, 0, activeSet.size() - 1,
activeSet.size() - 1);
matS = s * ones<MatType>(1, activeSet.size());
// This worked last iteration, so there can't be a singularity.
solve(unnormalizedBetaDirection,
@@ -35,7 +35,7 @@ LinearSVMFunction<MatType, ParametersType>::LinearSVMFunction(
delta(delta),
fitIntercept(fitIntercept)
{
MakeAlias(dataset, datasetIn, datasetIn.n_rows, datasetIn.n_cols, false);
MakeAlias(dataset, datasetIn, datasetIn.n_rows, datasetIn.n_cols, 0, false);
InitializeWeights(initialPoint, dataset.n_rows, numClasses, fitIntercept);
initialPoint *= 0.005;
@@ -34,8 +34,8 @@ LMNNFunction<MetricType>::LMNNFunction(const arma::mat& datasetIn,
points(datasetIn.n_cols),
impBounds(false)
{
MakeAlias(dataset, datasetIn, datasetIn.n_rows, datasetIn.n_cols, false);
MakeAlias(labels, labelsIn, labelsIn.n_rows, labelsIn.n_cols, false);
MakeAlias(dataset, datasetIn, datasetIn.n_rows, datasetIn.n_cols, 0, false);
MakeAlias(labels, labelsIn, labelsIn.n_elem, 0, false);
// Initialize the initial learning point.
initialPoint.eye(dataset.n_rows, dataset.n_rows);
@@ -28,9 +28,8 @@ LogisticRegressionFunction<MatType>::LogisticRegressionFunction(
{
// We promise to be well-behaved... the elements won't be modified.
MakeAlias(this->predictors, predictorsIn, predictorsIn.n_rows,
predictorsIn.n_cols, false);
MakeAlias(this->responses, responsesIn, responsesIn.n_rows,
responsesIn.n_cols, false);
predictorsIn.n_cols, 0, false);
MakeAlias(this->responses, responsesIn, responsesIn.n_elem, 0, false);
// Sanity check.
if (responses.n_elem != predictors.n_cols)
+10 -25
View File
@@ -61,12 +61,7 @@ BINDING_LONG_DESC(
" may be saved with the " + PRINT_PARAM_STRING("predictions") +"predictions"
" parameter. If saving the trained model is desired, this may be "
"done with the " + PRINT_PARAM_STRING("output_model") + " output "
"parameter."
"\n\n"
"Note: the " + PRINT_PARAM_STRING("output") + " and " +
PRINT_PARAM_STRING("output_probs") + " parameters are deprecated and will "
"be removed in mlpack 4.0.0. Use " + PRINT_PARAM_STRING("predictions") +
" and " + PRINT_PARAM_STRING("probabilities") + " instead.");
"parameter.");
// Example.
BINDING_EXAMPLE(
@@ -83,8 +78,8 @@ BINDING_EXAMPLE(
"classes to " + PRINT_DATASET("predictions") + ", the following command "
"may be used:"
"\n\n" +
PRINT_CALL("nbc", "input_model", "nbc_model", "test", "test_set", "output",
"predictions"));
PRINT_CALL("nbc", "input_model", "nbc_model", "test", "test_set",
"predictions", "predictions"));
// See also...
BINDING_SEE_ALSO("@softmax_regression", "#softmax_regression");
@@ -126,14 +121,8 @@ PARAM_FLAG("incremental_variance", "The variance of each class will be "
// Test parameters.
PARAM_MATRIX_IN("test", "A matrix containing the test set.", "T");
// The parameter 'output' is deprecated and will be removed in mlpack 4.
PARAM_UROW_OUT("output", "The matrix in which the predicted labels for the"
" test set will be written (deprecated).", "o");
PARAM_UROW_OUT("predictions", "The matrix in which the predicted labels for the"
" test set will be written.", "a");
// The parameter 'output_probs' is deprecated and can be removed in mlpack 4.
PARAM_MATRIX_OUT("output_probs", "The matrix in which the predicted probability"
" of labels for the test set will be written (deprecated).", "");
PARAM_MATRIX_OUT("probabilities", "The matrix in which the predicted"
" probability of labels for the test set will be written.", "p");
@@ -143,9 +132,8 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& timers)
RequireOnlyOnePassed(params, { "training", "input_model" }, true);
ReportIgnoredParam(params, {{ "training", false }}, "labels");
ReportIgnoredParam(params, {{ "training", false }}, "incremental_variance");
RequireAtLeastOnePassed(params, { "output", "predictions", "output_model",
"output_probs", "probabilities" }, false, "no output will be saved");
ReportIgnoredParam(params, {{ "test", false }}, "output");
RequireAtLeastOnePassed(params, { "predictions", "output_model",
"probabilities" }, false, "no output will be saved");
ReportIgnoredParam(params, {{ "test", false }}, "predictions");
if (params.Has("input_model") && !params.Has("test"))
Log::Warn << "No test set given; no task will be performed!" << std::endl;
@@ -208,23 +196,20 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& timers)
model->nbc.Classify(testingData, predictions, probabilities);
timers.Stop("nbc_testing");
if (params.Has("output") || params.Has("predictions"))
if (params.Has("predictions"))
{
// Un-normalize labels to prepare output.
Row<size_t> rawResults;
data::RevertLabels(predictions, model->mappings, rawResults);
if (params.Has("predictions"))
params.Get<Row<size_t>>("predictions") = rawResults;
if (params.Has("output"))
params.Get<Row<size_t>>("output") = std::move(rawResults);
params.Get<Row<size_t>>("predictions") = std::move(rawResults);
}
if (params.Has("output_probs") || params.Has("probabilities"))
if (params.Has("probabilities"))
{
if (params.Has("probabilities"))
params.Get<mat>("probabilities") = probabilities;
if (params.Has("output_probs"))
params.Get<mat>("output_probs") = std::move(probabilities);
params.Get<mat>("probabilities") = std::move(probabilities);
}
}
@@ -28,8 +28,8 @@ SoftmaxErrorFunction<MetricType>::SoftmaxErrorFunction(
metric(metric),
precalculated(false)
{
MakeAlias(dataset, datasetIn, datasetIn.n_rows, datasetIn.n_cols, false);
MakeAlias(labels, labelsIn, labelsIn.n_rows, labelsIn.n_cols, false);
MakeAlias(dataset, datasetIn, datasetIn.n_rows, datasetIn.n_cols, 0, false);
MakeAlias(labels, labelsIn, labelsIn.n_elem, 0, false);
}
//! Shuffle the dataset.
@@ -56,14 +56,7 @@ BINDING_LONG_DESC(
"on the test set may be saved with the " +
PRINT_PARAM_STRING("predictions") +
" output parameter. The perceptron model may be saved with the " +
PRINT_PARAM_STRING("output_model") + " output parameter."
"\n\n"
"Note: the following parameter is deprecated and "
"will be removed in mlpack 4.0.0: " + PRINT_PARAM_STRING("output") +
"."
"\n"
"Use " + PRINT_PARAM_STRING("predictions") + " instead of " +
PRINT_PARAM_STRING("output") + '.');
PRINT_PARAM_STRING("output_model") + " output parameter.");
// Example.
BINDING_EXAMPLE(
@@ -144,9 +137,6 @@ PARAM_MODEL_OUT(PerceptronModel, "output_model", "Output for trained perceptron"
// Testing/classification parameters.
PARAM_MATRIX_IN("test", "A matrix containing the test set.", "T");
// PARAM_UROW_OUT("output") is deprecated and will be removed in
PARAM_UROW_OUT("output", "The matrix in which the predicted labels for the"
" test set will be written.", "o");
PARAM_UROW_OUT("predictions", "The matrix in which the predicted labels for the"
" test set will be written.", "P");
@@ -160,9 +150,8 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& timers)
// If the user isn't going to save the output model or any predictions, we
// should issue a warning.
RequireAtLeastOnePassed(params, { "output_model", "output", "predictions" },
false, "no output will be saved");
// "output" will be removed in mlpack 4.0.0.
RequireAtLeastOnePassed(params, { "output_model", "predictions" }, false,
"no output will be saved");
ReportIgnoredParam(params, {{ "test", false }}, "predictions");
// Check parameter validity.
@@ -320,8 +309,6 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& timers)
data::RevertLabels(predictedLabels, p->Map(), results);
// Save the predicted labels.
if (params.Has("output"))
params.Get<arma::Row<size_t>>("output") = results;
if (params.Has("predictions"))
params.Get<arma::Row<size_t>>("predictions") = std::move(results);
}
@@ -27,7 +27,7 @@ inline SoftmaxRegressionFunction<MatType>::SoftmaxRegressionFunction(
lambda(lambda),
fitIntercept(fitIntercept)
{
MakeAlias(data, dataIn, dataIn.n_rows, dataIn.n_cols, false);
MakeAlias(data, dataIn, dataIn.n_rows, dataIn.n_cols, 0, false);
// Initialize the parameters to suitable values.
initialPoint = InitializeWeights();
@@ -447,7 +447,7 @@ TEST_CASE("Issue2986", "[ConvolutionalNetworkTest]")
c.InputDimensions() = std::vector<size_t>({ 6, 6 });
c.ComputeOutputDimensions();
arma::mat weights(c.WeightSize(), 1, arma::fill::randu);
c.SetWeights(weights.memptr());
c.SetWeights(weights);
output.set_size(c.OutputSize(), 1);
delta.set_size(input.size());
@@ -463,7 +463,7 @@ TEST_CASE("Issue2986", "[ConvolutionalNetworkTest]")
c.ComputeOutputDimensions();
weights.set_size(c.WeightSize(), 1);
weights.randu();
c.SetWeights(weights.memptr());
c.SetWeights(weights);
output.set_size(c.OutputSize(), 1);
delta.set_size(input.size());
@@ -479,7 +479,7 @@ TEST_CASE("Issue2986", "[ConvolutionalNetworkTest]")
c.ComputeOutputDimensions();
weights.set_size(c.WeightSize(), 1);
weights.randu();
c.SetWeights(weights.memptr());
c.SetWeights(weights);
output.set_size(c.OutputSize(), 1);
delta.set_size(input.size());
@@ -508,7 +508,7 @@ TEST_CASE("CustomPaddingTest", "[ConvolutionalNetworkTest]")
weights.set_size(c.WeightSize(), 1);
weights.ones();
c.SetWeights(weights.memptr());
c.SetWeights(weights);
// Now make sure that the forward pass returns the correct output.
output.set_size(c.OutputSize(), 1);
+2 -2
View File
@@ -32,7 +32,7 @@ TEST_CASE("AddManualWeightTestCase", "[ANNLayerTest]")
Add module;
module.InputDimensions() = std::vector<size_t>({ 1 });
module.ComputeOutputDimensions();
module.SetWeights(weights.memptr());
module.SetWeights(weights);
module.Parameters()[0] = 3.0;
arma::mat output(1, 1);
@@ -67,7 +67,7 @@ TEST_CASE("AddManualWeightBatchTestCase", "[ANNLayerTest]")
Add module;
module.InputDimensions() = std::vector<size_t>({ 1 });
module.ComputeOutputDimensions();
module.SetWeights(weights.memptr());
module.SetWeights(weights);
module.Parameters()[0] = 3.0;
arma::mat output(1, 5);
+2 -2
View File
@@ -109,7 +109,7 @@ TEST_CASE("AddMergeAdvanceTestCase", "[ANNLayerTest]")
r.InputDimensions() = std::vector<size_t>({ 5 });
r.ComputeOutputDimensions();
arma::mat rParams(r.WeightSize(), 1);
r.SetWeights((double*) rParams.memptr());
r.SetWeights(rParams);
r.Network()[0]->Parameters().fill(2.0);
((AddMerge*) r.Network()[1])->Network()[0]->Parameters().fill(-1.0);
@@ -117,7 +117,7 @@ TEST_CASE("AddMergeAdvanceTestCase", "[ANNLayerTest]")
l.InputDimensions() = std::vector<size_t>({ 5 });
l.ComputeOutputDimensions();
arma::mat lParams(l.WeightSize(), 1);
l.SetWeights((double*) lParams.memptr());
l.SetWeights(lParams);
l.Parameters().fill(1.0);
arma::mat input(arma::randn(5, 10));
+4 -4
View File
@@ -44,7 +44,7 @@ TEST_CASE("BatchNormTest", "[ANNLayerTest]")
module1.ComputeOutputDimensions();
arma::mat moduleParams(module1.WeightSize(), 1);
module1.CustomInitialize(moduleParams, module1.WeightSize());
module1.SetWeights((double*) moduleParams.memptr());
module1.SetWeights(moduleParams);
// BatchNorm layer with average parameter set to false (using momentum).
BatchNorm module2(2, 2, 1e-5, false);
@@ -53,7 +53,7 @@ TEST_CASE("BatchNormTest", "[ANNLayerTest]")
module2.ComputeOutputDimensions();
arma::mat moduleParams2(module2.WeightSize(), 1);
module2.CustomInitialize(moduleParams2, module2.WeightSize());
module2.SetWeights((double*) moduleParams2.memptr());
module2.SetWeights(moduleParams2);
// Training Forward Pass Test.
output.set_size(module1.OutputSize(), 1);
@@ -250,7 +250,7 @@ TEST_CASE("BatchNormWithMinBatchesTest", "[ANNLayerTest]")
module1.ComputeOutputDimensions();
arma::mat moduleParams(module1.WeightSize(), 1);
module1.CustomInitialize(moduleParams, module1.WeightSize());
module1.SetWeights((double*) moduleParams.memptr());
module1.SetWeights(moduleParams);
output.set_size(8, 3);
module1.Forward(input, output);
CheckMatrices(output, result, 1e-1);
@@ -275,7 +275,7 @@ TEST_CASE("BatchNormWithMinBatchesTest", "[ANNLayerTest]")
module2.ComputeOutputDimensions();
arma::mat moduleParams2(module2.WeightSize(), 1);
module2.CustomInitialize(moduleParams2, module2.WeightSize());
module2.SetWeights((double*) moduleParams2.memptr());
module2.SetWeights(moduleParams2);
output.set_size(8, 3);
module2.Forward(input, output);
CheckMatrices(output, result, 1e-1);
+4 -4
View File
@@ -31,14 +31,14 @@ TEST_CASE("SimpleConcatLayerTest", "[ANNLayerTest]")
moduleA->InputDimensions() = std::vector<size_t>({ 10 });
moduleA->ComputeOutputDimensions();
arma::mat weightsA(moduleA->WeightSize(), 1);
moduleA->SetWeights((double*) weightsA.memptr());
moduleA->SetWeights(weightsA);
moduleA->Parameters().randu();
Linear* moduleB = new Linear(10);
moduleB->InputDimensions() = std::vector<size_t>({ 10 });
moduleB->ComputeOutputDimensions();
arma::mat weightsB(moduleB->WeightSize(), 1);
moduleB->SetWeights((double*) weightsB.memptr());
moduleB->SetWeights(weightsB);
moduleB->Parameters().randu();
Concat module;
@@ -92,13 +92,13 @@ TEST_CASE("ConcatAlongAxisTest", "[ANNLayerTest]")
moduleA->InputDimensions() = std::vector<size_t>({ inputWidth, inputHeight });
moduleA->ComputeOutputDimensions();
arma::mat weightsA(moduleA->WeightSize(), 1);
moduleA->SetWeights((double*) weightsA.memptr());
moduleA->SetWeights(weightsA);
moduleA->Parameters().randu();
moduleB->InputDimensions() = std::vector<size_t>({ inputWidth, inputHeight });
moduleB->ComputeOutputDimensions();
arma::mat weightsB(moduleB->WeightSize(), 1);
moduleB->SetWeights((double*) weightsB.memptr());
moduleB->SetWeights(weightsB);
moduleB->Parameters().randu();
// Compute output of each layer.
+8 -8
View File
@@ -77,7 +77,7 @@ TEST_CASE("ConvolutionLayerPaddingTest", "[ANNLayerTest]")
module1.ComputeOutputDimensions();
arma::mat weights1(module1.WeightSize(), 1);
REQUIRE(weights1.n_elem == 10);
module1.SetWeights(weights1.memptr());
module1.SetWeights(weights1);
// Test the Forward function.
input = arma::linspace<arma::colvec>(0, 48, 49);
@@ -100,7 +100,7 @@ TEST_CASE("ConvolutionLayerPaddingTest", "[ANNLayerTest]")
module2.ComputeOutputDimensions();
arma::mat weights2(module2.WeightSize(), 1);
REQUIRE(weights2.n_elem == 10);
module2.SetWeights(weights2.memptr());
module2.SetWeights(weights2);
// Test the forward function.
input = arma::linspace<arma::colvec>(0, 48, 49);
@@ -218,7 +218,7 @@ TEST_CASE("ConvolutionLayerTestCase", "[ANNLayerTest]")
layer.InputDimensions() = std::vector<size_t>({ 4, 1, 2 });
layer.ComputeOutputDimensions();
arma::mat layerWeights(layer.WeightSize(), 1);
layer.SetWeights(layerWeights.memptr());
layer.SetWeights(layerWeights);
output.set_size(layer.OutputSize(), 3);
// Set weights to 1.0 and bias to 0.0.
@@ -250,7 +250,7 @@ TEST_CASE("ConvolutionLayerWeightInitializationTest", "[ANNLayerTest]")
module.InputDimensions() = std::vector<size_t>({ 12, 13, 2 });
module.ComputeOutputDimensions();
arma::mat weights(module.WeightSize(), 1);
module.SetWeights(weights.memptr());
module.SetWeights(weights);
RandomInitialization().Initialize(module.Weight());
module.Bias().ones();
@@ -290,7 +290,7 @@ TEST_CASE("NoBiasConvolutionLayerTestCase", "[ANNLayerTest]")
layer.ComputeOutputDimensions();
REQUIRE(layer.WeightSize() == 8);
arma::mat layerWeights(layer.WeightSize(), 1);
layer.SetWeights(layerWeights.memptr());
layer.SetWeights(layerWeights);
REQUIRE(layer.Bias().n_elem == 0);
output.set_size(layer.OutputSize(), 3);
@@ -350,7 +350,7 @@ TEST_CASE("AdvancedConvolutionLayerTest", "[ANNLayerTest]")
layerWeights(15) = -0.2283206;
layerWeights(16) = 0.3204123974;
layerWeights(17) = 0.2334779799;
layer.SetWeights(layerWeights.memptr());
layer.SetWeights(layerWeights);
output.set_size(layer.OutputSize(), 3);
layer.Forward(input, output);
@@ -428,7 +428,7 @@ TEST_CASE("AdvancedConvolutionLayerWithStrideTest", "[ANNLayerTest]")
layerWeights(15) = -0.12563613;
layerWeights(16) = -0.1114468053;
layerWeights(17) = -0.3029643595;
layer.SetWeights(layerWeights.memptr());
layer.SetWeights(layerWeights);
output.set_size(layer.OutputSize(), 3);
layer.Forward(input, output);
@@ -451,7 +451,7 @@ TEST_CASE("NonSquareConvolutionTest", "[ANNLayerTest]")
module1.InputDimensions() = std::vector<size_t>({ 7, 7 });
module1.ComputeOutputDimensions();
arma::mat weights1(module1.WeightSize(), 1);
module1.SetWeights(weights1.memptr());
module1.SetWeights(weights1);
arma::mat data(49, 10, arma::fill::randu);
arma::mat forwardResult(module1.OutputSize(), 10, arma::fill::zeros);
+1 -1
View File
@@ -35,7 +35,7 @@ TEST_CASE("JacobianFlexibleReLULayerTest", "[ANNLayerTest]")
FlexibleReLU module;
arma::mat moduleParams(module.WeightSize(), 1);
module.CustomInitialize(moduleParams, module.WeightSize());
module.SetWeights((double*) moduleParams.memptr());
module.SetWeights(moduleParams);
double error = JacobianTest(module, input);
REQUIRE(error <= 1e-5);
@@ -49,7 +49,7 @@ TEST_CASE("GroupedConvolutionLayerTest", "[ANNLayerTest]")
layerWeights(5) = -0.7586858273;
layerWeights(6) = -0.1721059084;
layerWeights(7) = -0.1972532272;
layer.SetWeights(layerWeights.memptr());
layer.SetWeights(layerWeights);
output.set_size(layer.OutputSize(), 3);
layer.Forward(input, output);
@@ -104,7 +104,7 @@ TEST_CASE("GroupedConvolutionEquivalenceTest", "[ANNLayerTest]")
layerWeights(15) = -0.2283206;
layerWeights(16) = 0.3204123974;
layerWeights(17) = 0.2334779799;
layer.SetWeights(layerWeights.memptr());
layer.SetWeights(layerWeights);
output.set_size(layer.OutputSize(), 3);
GroupedConvolution layerG(2, 2, 2, 1, 1, 1, 0, 0);
@@ -129,7 +129,7 @@ TEST_CASE("GroupedConvolutionEquivalenceTest", "[ANNLayerTest]")
layerWeightsG(15) = -0.2283206;
layerWeightsG(16) = 0.3204123974;
layerWeightsG(17) = 0.2334779799;
layerG.SetWeights(layerWeightsG.memptr());
layerG.SetWeights(layerWeightsG);
outputG.set_size(layerG.OutputSize(), 3);
layer.Forward(input, output);
@@ -212,7 +212,7 @@ TEST_CASE("NonSquareGroupedConvolutionTest", "[ANNLayerTest]")
module1.InputDimensions() = std::vector<size_t>({ 7, 7 });
module1.ComputeOutputDimensions();
arma::mat weights1(module1.WeightSize(), 1);
module1.SetWeights(weights1.memptr());
module1.SetWeights(weights1);
arma::mat data(49, 10, arma::fill::randu);
arma::mat forwardResult(module1.OutputSize(), 10, arma::fill::zeros);
+1 -1
View File
@@ -36,7 +36,7 @@ TEST_CASE("LayerNormTest", "[ANNLayerTest]")
model.InputDimensions() = std::vector<size_t>({ 3 });
model.ComputeOutputDimensions();
arma::mat weights(model.WeightSize(), 1);
model.SetWeights(weights.memptr());
model.SetWeights(weights);
model.CustomInitialize(weights, model.WeightSize());
model.Forward(input, output);
+2 -2
View File
@@ -36,7 +36,7 @@ TEST_CASE("SimpleLinear3DLayerTest", "[ANNLayerTest]")
module.InputDimensions() = std::vector<size_t>({ 4, 2 });
module.ComputeOutputDimensions();
arma::mat weights(module.WeightSize(), 1);
module.SetWeights(weights.memptr());
module.SetWeights(weights);
module.Parameters().randu();
@@ -74,7 +74,7 @@ TEST_CASE("JacobianLinear3DLayerTest", "[ANNLayerTest]")
module.InputDimensions() = std::vector<size_t>({ inSize, nPoints });
module.ComputeOutputDimensions();
arma::mat weights(module.WeightSize(), 1);
module.SetWeights(weights.memptr());
module.SetWeights(weights);
module.Parameters().randu();
@@ -30,7 +30,7 @@ TEST_CASE("SimpleLinearNoBiasLayerTest", "[ANNLayerTest]")
arma::mat weights(10 * 10, 1);
module.InputDimensions() = std::vector<size_t>({ 10 });
module.ComputeOutputDimensions();
module.SetWeights(weights.memptr());
module.SetWeights(weights);
module.Parameters().randu();
@@ -61,7 +61,7 @@ TEST_CASE("JacobianLinearNoBiasLayerTest", "[ANNLayerTest]")
arma::mat weights(inputElements * outputElements, 1);
module.InputDimensions() = std::vector<size_t>({ inputElements });
module.ComputeOutputDimensions();
module.SetWeights(weights.memptr());
module.SetWeights(weights);
module.Parameters().randu();
@@ -53,7 +53,7 @@ TEST_CASE("SimpleMultiheadAttentionTest", "[ANNLayerTest]")
module.ComputeOutputDimensions();
arma::mat weights(module.WeightSize(), 1);
weights.randu();
module.SetWeights(weights.memptr());
module.SetWeights(weights);
module.AttentionMask() = attnMask;
module.KeyPaddingMask() = keyPaddingMask;
@@ -101,7 +101,7 @@ TEST_CASE("JacobianMultiheadAttentionTest", "[ANNLayerTest]")
module.ComputeOutputDimensions();
arma::mat weights(module.WeightSize(), 1);
weights.randu();
module.SetWeights(weights.memptr());
module.SetWeights(weights);
double error = CustomJacobianTest(module, input);
REQUIRE(error <= 1e-5);
@@ -125,7 +125,7 @@ TEST_CASE("JacobianMultiheadAttentionTest", "[ANNLayerTest]")
module.ComputeOutputDimensions();
arma::mat weights(module.WeightSize(), 1);
weights.randu();
module.SetWeights(weights.memptr());
module.SetWeights(weights);
double error = CustomJacobianTest(module, input);
REQUIRE(error <= 1e-5);
@@ -150,7 +150,7 @@ TEST_CASE("JacobianMultiheadAttentionTest", "[ANNLayerTest]")
module.ComputeOutputDimensions();
arma::mat weights(module.WeightSize(), 1);
weights.randu();
module.SetWeights(weights.memptr());
module.SetWeights(weights);
double error = JacobianTest(module, input);
REQUIRE(error <= 1e-5);
@@ -31,7 +31,7 @@ TEST_CASE("PReLUFORWARDTest", "[ANNLayerTest]")
PReLU module(0.01);
arma::mat moduleParams(module.WeightSize(), 1);
module.CustomInitialize(moduleParams, module.WeightSize());
module.SetWeights((double*) moduleParams.memptr());
module.SetWeights(moduleParams);
arma::mat predOutput;
module.Forward(input, predOutput);
arma::mat actualOutput = {{0.5, 1.2, 3.1},
@@ -54,7 +54,7 @@ TEST_CASE("PReLUBACKWARDTest", "[ANNLayerTest]")
PReLU module(0.01);
arma::mat moduleParams(module.WeightSize(), 1);
module.CustomInitialize(moduleParams, module.WeightSize());
module.SetWeights((double*) moduleParams.memptr());
module.SetWeights(moduleParams);
arma::mat gy = {{0.2, -0.5, 0.8},
{1.5, -0.6, 0.1},
{-0.3, 0.2, -0.5},
@@ -84,7 +84,7 @@ TEST_CASE("PReLUGRADIENTTest", "[ANNLayerTest]")
PReLU module(0.01);
arma::mat moduleParams(module.WeightSize(), 1);
module.CustomInitialize(moduleParams, module.WeightSize());
module.SetWeights((double*) moduleParams.memptr());
module.SetWeights(moduleParams);
arma::mat error = {{0.2, -0.5, 0.8},
{-0.015, -0.006, 0.001},
{-0.3, 0.002, -0.005},
+149 -18
View File
@@ -35,7 +35,7 @@ void LARSVerifyCorrectness(const VecType& beta,
size_t nDims = beta.n_elem;
// floats require a much larger tolerance.
const ElemType tol = (std::is_same<ElemType, double>::value) ? 1e-10 : 1e-3;
const ElemType tol = (std::is_same<ElemType, double>::value) ? 1e-8 : 5e-3;
for (size_t j = 0; j < nDims; ++j)
{
@@ -200,39 +200,35 @@ TEST_CASE("NoCholeskySingularityTest", "[LARSTest]")
}
// Make sure that Predict() provides reasonable enough solutions.
TEMPLATE_TEST_CASE("PredictTest", "[LARSTest]", arma::fmat, arma::mat)
TEST_CASE("PredictTest", "[LARSTest]")
{
typedef TestType MatType;
typedef typename MatType::elem_type ElemType;
for (size_t i = 0; i < 2; ++i)
{
// Run with both true and false.
bool useCholesky = bool(i);
MatType X;
arma::Row<ElemType> y;
arma::mat X;
arma::rowvec y;
GenerateProblem(X, y, 1000, 100);
for (ElemType lambda1 = 0.0; lambda1 < 1.0; lambda1 += 0.2)
for (double lambda1 = 0.0; lambda1 < 1.0; lambda1 += 0.2)
{
for (ElemType lambda2 = 0.0; lambda2 < 1.0; lambda2 += 0.2)
for (double lambda2 = 0.0; lambda2 < 1.0; lambda2 += 0.2)
{
LARS<MatType> lars(useCholesky, lambda1, lambda2);
LARS<> lars(useCholesky, lambda1, lambda2);
lars.FitIntercept(false);
lars.NormalizeData(false);
lars.Train(X, y);
// Calculate what the actual error should be with these regression
// parameters.
arma::Col<ElemType> betaOptPred = (X * X.t()) * lars.Beta();
arma::Row<ElemType> predictions;
arma::vec betaOptPred = (X * X.t()) * lars.Beta();
arma::rowvec predictions;
lars.Predict(X, predictions);
arma::Col<ElemType> adjPred = X * predictions.t();
arma::vec adjPred = X * predictions.t();
const ElemType tol = (std::is_same<ElemType, double>::value) ? 1e-7 :
1e-3;
const double tol = 1e-7;
REQUIRE(predictions.n_elem == 1000);
for (size_t i = 0; i < betaOptPred.n_elem; ++i)
@@ -244,7 +240,8 @@ TEMPLATE_TEST_CASE("PredictTest", "[LARSTest]", arma::fmat, arma::mat)
}
// Now check with single-point Predict(), in two ways: we will pass
// different types into Predict() to test templating support.
// different types into Predict() to test templating support. We allow
// a looser tolerance for predictions.
for (size_t i = 0; i < X.n_cols; ++i)
predictions[i] = lars.Predict(X.col(i));
@@ -273,6 +270,140 @@ TEMPLATE_TEST_CASE("PredictTest", "[LARSTest]", arma::fmat, arma::mat)
}
}
// This is the same as PredictTest, but for arma::fmat, and it allows multiple
// trials for run to deal with the lower precision of floats.
TEST_CASE("PredictFloatTest", "[LARSTest]")
{
for (size_t i = 0; i < 2; ++i)
{
// Run with both true and false.
bool useCholesky = bool(i);
arma::fmat X;
arma::frowvec y;
for (float lambda1 = 0.0; lambda1 < 1.0; lambda1 += 0.2)
{
for (float lambda2 = 0.0; lambda2 < 1.0; lambda2 += 0.2)
{
// For float data, sometimes the solutions are further away from the
// true solution due to precision issues, so we allow multiple trials.
bool success = false;
for (size_t trial = 0; trial < 3; ++trial)
{
// Generate a new problem so that we hopefully end up with a better
// fit.
GenerateProblem(X, y, 1000, 100);
LARS<arma::fmat> lars(useCholesky, lambda1, lambda2);
lars.FitIntercept(false);
lars.NormalizeData(false);
lars.Train(X, y);
// Calculate what the actual error should be with these regression
// parameters.
arma::fvec betaOptPred = (X * X.t()) * lars.Beta();
arma::frowvec predictions;
lars.Predict(X, predictions);
arma::fvec adjPred = X * predictions.t();
const float tol = 3e-5;
REQUIRE(predictions.n_elem == 1000);
bool trialSuccess = true;
for (size_t i = 0; i < betaOptPred.n_elem; ++i)
{
if (std::abs(betaOptPred[i]) < 1e-5)
{
if (adjPred[i] != Approx(0.0).margin(1e-5))
{
trialSuccess = false;
break;
}
}
else
{
if (adjPred[i] != Approx(betaOptPred[i]).epsilon(tol))
{
trialSuccess = false;
break;
}
}
}
// If this trial didn't succeed, skip to the next trial.
if (!trialSuccess)
continue;
// Now check with single-point Predict(), in two ways: we will pass
// different types into Predict() to test templating support. We allow
// a looser tolerance for predictions.
for (size_t i = 0; i < X.n_cols; ++i)
predictions[i] = lars.Predict(X.col(i));
adjPred = X * predictions.t();
for (size_t i = 0; i < betaOptPred.n_elem; ++i)
{
if (std::abs(betaOptPred[i]) < 1e-5)
{
if (adjPred[i] != Approx(0.0).margin(1e-5))
{
trialSuccess = false;
break;
}
}
else
{
if (adjPred[i] != Approx(betaOptPred[i]).epsilon(10 * tol))
{
trialSuccess = false;
break;
}
}
}
// If this trial didn't succeed, skip to the next trial.
if (!trialSuccess)
continue;
for (size_t i = 0; i < X.n_cols; ++i)
predictions[i] = lars.Predict(X.unsafe_col(i));
adjPred = X * predictions.t();
for (size_t i = 0; i < betaOptPred.n_elem; ++i)
{
if (std::abs(betaOptPred[i]) < 1e-5)
{
if (adjPred[i] != Approx(0.0).margin(1e-5))
{
trialSuccess = false;
break;
}
}
else
{
if (adjPred[i] != Approx(betaOptPred[i]).epsilon(10 * tol))
{
trialSuccess = false;
break;
}
}
}
// If this trial succeeded, we're done.
if (trialSuccess)
{
success = true;
break;
}
}
REQUIRE(success == true);
}
}
}
}
TEST_CASE("PredictRowMajorTest", "[LARSTest]")
{
arma::mat X;
@@ -719,7 +850,7 @@ void CheckKKT(const arma::vec& beta,
const arma::rowvec& y,
const double lambda)
{
const double epsilon = 1e-10; // For numerical precision.
const double epsilon = 1e-6; // For numerical precision.
arma::vec v = X.t() * X * beta - X.t() * y.t() + lambda * sign(beta);
// Active set indices with global numbering: could be empty.
@@ -1094,7 +1225,7 @@ TEMPLATE_TEST_CASE("LARSSelectBetaTest", "[LARSTest]", arma::fmat, arma::mat)
typedef TestType MatType;
typedef typename MatType::elem_type ElemType;
const ElemType tol = (std::is_same<ElemType, double>::value) ? 1e-5 : 1e-3;
const ElemType tol = (std::is_same<ElemType, double>::value) ? 1e-5 : 5e-3;
// Train a model on a randomly generated problem. Then, we will iterate
// through different selected lambda values, ensuring that the error on the
+8 -32
View File
@@ -52,8 +52,8 @@ TEST_CASE_METHOD(AdaBoostTestFixture, "AdaBoostOutputDimensionTest",
RUN_BINDING();
// Check that number of predicted labels is equal to the input test points.
REQUIRE(params.Get<arma::Row<size_t>>("output").n_cols == testSize);
REQUIRE(params.Get<arma::Row<size_t>>("output").n_rows == 1);
REQUIRE(params.Get<arma::Row<size_t>>("predictions").n_cols == testSize);
REQUIRE(params.Get<arma::Row<size_t>>("predictions").n_rows == 1);
}
/**
@@ -120,7 +120,7 @@ TEST_CASE_METHOD(AdaBoostTestFixture, "AdaBoostModelReuseTest",
RUN_BINDING();
arma::Row<size_t> output;
output = std::move(params.Get<arma::Row<size_t>>("output"));
output = std::move(params.Get<arma::Row<size_t>>("predictions"));
AdaBoostModel* model = params.Get<AdaBoostModel*>("output_model");
ResetSettings();
@@ -131,7 +131,7 @@ TEST_CASE_METHOD(AdaBoostTestFixture, "AdaBoostModelReuseTest",
RUN_BINDING();
// Check that initial output and output using saved model are same.
CheckMatrices(output, params.Get<arma::Row<size_t>>("output"));
CheckMatrices(output, params.Get<arma::Row<size_t>>("predictions"));
}
/**
@@ -182,7 +182,7 @@ TEST_CASE_METHOD(AdaBoostTestFixture, "AdaBoostWithoutLabelTest",
RUN_BINDING();
arma::Row<size_t> output;
output = std::move(params.Get<arma::Row<size_t>>("output"));
output = std::move(params.Get<arma::Row<size_t>>("predictions"));
CleanMemory();
ResetSettings();
@@ -197,7 +197,7 @@ TEST_CASE_METHOD(AdaBoostTestFixture, "AdaBoostWithoutLabelTest",
RUN_BINDING();
// Check that initial output and final output matrix are same.
CheckMatrices(output, params.Get<arma::Row<size_t>>("output"));
CheckMatrices(output, params.Get<arma::Row<size_t>>("predictions"));
}
/**
@@ -220,30 +220,6 @@ TEST_CASE_METHOD(AdaBoostTestFixture, "AdaBoostTrainingDataOrModelTest",
REQUIRE_THROWS_AS(RUN_BINDING(), std::runtime_error);
}
/**
* This test can be removed in mlpack 4.0.0. This tests that the output and
* predictions outputs are the same.
*/
TEST_CASE_METHOD(AdaBoostTestFixture, "AdaBoostOutputPredictionsTest",
"[AdaBoostMainTest][BindingTests]")
{
arma::mat trainData;
if (!data::Load("vc2.csv", trainData))
FAIL("Unable to load train dataset vc2.csv!");
arma::Row<size_t> labels;
if (!data::Load("vc2_labels.txt", labels))
FAIL("Unable to load label dataset vc2_labels.txt!");
SetInputParam("training", std::move(trainData));
SetInputParam("labels", std::move(labels));
RUN_BINDING();
CheckMatrices(params.Get<arma::Row<size_t>>("output"),
params.Get<arma::Row<size_t>>("predictions"));
}
/**
* Weak learner should be either Decision Stump or Perceptron.
*/
@@ -285,7 +261,7 @@ TEST_CASE_METHOD(AdaBoostTestFixture, "AdaBoostDiffWeakLearnerOutputTest",
RUN_BINDING();
arma::Row<size_t> output;
output = std::move(params.Get<arma::Row<size_t>>("output"));
output = std::move(params.Get<arma::Row<size_t>>("predictions"));
CleanMemory();
ResetSettings();
@@ -298,7 +274,7 @@ TEST_CASE_METHOD(AdaBoostTestFixture, "AdaBoostDiffWeakLearnerOutputTest",
RUN_BINDING();
arma::Row<size_t> outputPerceptron;
outputPerceptron = std::move(params.Get<arma::Row<size_t>>("output"));
outputPerceptron = std::move(params.Get<arma::Row<size_t>>("predictions"));
REQUIRE(accu(output != outputPerceptron) > 1);
}
+36 -133
View File
@@ -62,12 +62,12 @@ TEST_CASE_METHOD(NBCTestFixture, "NBCOutputDimensionTest",
RUN_BINDING();
// Check that number of output points are equal to number of input points.
REQUIRE(params.Get<arma::Row<size_t>>("output").n_cols == testSize);
REQUIRE(params.Get<arma::mat>("output_probs").n_cols == testSize);
REQUIRE(params.Get<arma::Row<size_t>>("predictions").n_cols == testSize);
REQUIRE(params.Get<arma::mat>("probabilities").n_cols == testSize);
// Check output have only single row.
REQUIRE(params.Get<arma::Row<size_t>>("output").n_rows == 1);
REQUIRE(params.Get<arma::mat>("output_probs").n_rows == 2);
REQUIRE(params.Get<arma::Row<size_t>>("predictions").n_rows == 1);
REQUIRE(params.Get<arma::mat>("probabilities").n_rows == 2);
}
/**
@@ -106,18 +106,18 @@ TEST_CASE_METHOD(NBCTestFixture, "NBCLabelsLessDimensionTest",
RUN_BINDING();
// Check that number of output points are equal to number of input points.
REQUIRE(params.Get<arma::Row<size_t>>("output").n_cols == testSize);
REQUIRE(params.Get<arma::mat>("output_probs").n_cols == testSize);
REQUIRE(params.Get<arma::Row<size_t>>("predictions").n_cols == testSize);
REQUIRE(params.Get<arma::mat>("probabilities").n_cols == testSize);
// Check output have only single row.
REQUIRE(params.Get<arma::Row<size_t>>("output").n_rows == 1);
REQUIRE(params.Get<arma::mat>("output_probs").n_rows == 2);
REQUIRE(params.Get<arma::Row<size_t>>("predictions").n_rows == 1);
REQUIRE(params.Get<arma::mat>("probabilities").n_rows == 2);
// Store outputs.
arma::Row<size_t> output;
arma::mat output_probs;
output = std::move(params.Get<arma::Row<size_t>>("output"));
output_probs = std::move(params.Get<arma::mat>("output_probs"));
output = std::move(params.Get<arma::Row<size_t>>("predictions"));
output_probs = std::move(params.Get<arma::mat>("probabilities"));
// Reset data passed.
CleanMemory();
@@ -136,17 +136,17 @@ TEST_CASE_METHOD(NBCTestFixture, "NBCLabelsLessDimensionTest",
RUN_BINDING();
// Check that number of output points are equal to number of input points.
REQUIRE(params.Get<arma::Row<size_t>>("output").n_cols == testSize);
REQUIRE(params.Get<arma::mat>("output_probs").n_cols == testSize);
REQUIRE(params.Get<arma::Row<size_t>>("predictions").n_cols == testSize);
REQUIRE(params.Get<arma::mat>("probabilities").n_cols == testSize);
// Check output have only single row.
REQUIRE(params.Get<arma::Row<size_t>>("output").n_rows == 1);
REQUIRE(params.Get<arma::mat>("output_probs").n_rows == 2);
REQUIRE(params.Get<arma::Row<size_t>>("predictions").n_rows == 1);
REQUIRE(params.Get<arma::mat>("probabilities").n_rows == 2);
// Check that initial output and final output matrix
// from two models are same.
CheckMatrices(output, params.Get<arma::Row<size_t>>("output"));
CheckMatrices(output_probs, params.Get<arma::mat>("output_probs"));
CheckMatrices(output, params.Get<arma::Row<size_t>>("predictions"));
CheckMatrices(output_probs, params.Get<arma::mat>("probabilities"));
}
/**
@@ -178,8 +178,8 @@ TEST_CASE_METHOD(NBCTestFixture, "NBCModelReuseTest",
arma::Row<size_t> output;
arma::mat output_probs;
output = std::move(params.Get<arma::Row<size_t>>("output"));
output_probs = std::move(params.Get<arma::mat>("output_probs"));
output = std::move(params.Get<arma::Row<size_t>>("predictions"));
output_probs = std::move(params.Get<arma::mat>("probabilities"));
// Reset passed parameters.
NBCModel* m = params.Get<NBCModel*>("output_model");
@@ -194,17 +194,17 @@ TEST_CASE_METHOD(NBCTestFixture, "NBCModelReuseTest",
RUN_BINDING();
// Check that number of output points are equal to number of input points.
REQUIRE(params.Get<arma::Row<size_t>>("output").n_cols == testSize);
REQUIRE(params.Get<arma::mat>("output_probs").n_cols == testSize);
REQUIRE(params.Get<arma::Row<size_t>>("predictions").n_cols == testSize);
REQUIRE(params.Get<arma::mat>("probabilities").n_cols == testSize);
// Check output have only single row.
REQUIRE(params.Get<arma::Row<size_t>>("output").n_rows == 1);
REQUIRE(params.Get<arma::mat>("output_probs").n_rows == 2);
REQUIRE(params.Get<arma::Row<size_t>>("predictions").n_rows == 1);
REQUIRE(params.Get<arma::mat>("probabilities").n_rows == 2);
// Check that initial output and final output
// matrix using saved model are same.
CheckMatrices(output, params.Get<arma::Row<size_t>>("output"));
CheckMatrices(output_probs, params.Get<arma::mat>("output_probs"));
CheckMatrices(output, params.Get<arma::Row<size_t>>("predictions"));
CheckMatrices(output_probs, params.Get<arma::mat>("probabilities"));
}
/**
@@ -260,18 +260,18 @@ TEST_CASE_METHOD(NBCTestFixture, "NBCIncrementalVarianceTest",
RUN_BINDING();
// Check that number of output points are equal to number of input points.
REQUIRE(params.Get<arma::Row<size_t>>("output").n_cols == testSize);
REQUIRE(params.Get<arma::mat>("output_probs").n_cols == testSize);
REQUIRE(params.Get<arma::Row<size_t>>("predictions").n_cols == testSize);
REQUIRE(params.Get<arma::mat>("probabilities").n_cols == testSize);
// Check output have only single row.
REQUIRE(params.Get<arma::Row<size_t>>("output").n_rows == 1);
REQUIRE(params.Get<arma::mat>("output_probs").n_rows == 2);
REQUIRE(params.Get<arma::Row<size_t>>("predictions").n_rows == 1);
REQUIRE(params.Get<arma::mat>("probabilities").n_rows == 2);
// Store outputs.
arma::Row<size_t> output;
arma::mat output_probs;
output = std::move(params.Get<arma::Row<size_t>>("output"));
output_probs = std::move(params.Get<arma::mat>("output_probs"));
output = std::move(params.Get<arma::Row<size_t>>("predictions"));
output_probs = std::move(params.Get<arma::mat>("probabilities"));
CleanMemory();
ResetSettings();
@@ -286,112 +286,15 @@ TEST_CASE_METHOD(NBCTestFixture, "NBCIncrementalVarianceTest",
RUN_BINDING();
// Check that number of output points are equal to number of input points.
REQUIRE(params.Get<arma::Row<size_t>>("output").n_cols == testSize);
REQUIRE(params.Get<arma::mat>("output_probs").n_cols == testSize);
REQUIRE(params.Get<arma::Row<size_t>>("predictions").n_cols == testSize);
REQUIRE(params.Get<arma::mat>("probabilities").n_cols == testSize);
// Check output have only single row.
REQUIRE(params.Get<arma::Row<size_t>>("output").n_rows == 1);
REQUIRE(params.Get<arma::mat>("output_probs").n_rows == 2);
REQUIRE(params.Get<arma::Row<size_t>>("predictions").n_rows == 1);
REQUIRE(params.Get<arma::mat>("probabilities").n_rows == 2);
// Check that initial output and final output matrix
// from two models are same.
CheckMatrices(output, params.Get<arma::Row<size_t>>("output"));
CheckMatrices(output_probs, params.Get<arma::mat>("output_probs"));
}
/**
* Ensure that the parameter 'output' and the parameter 'predictions' give the
* same output. This test case should be removed in mlpack 4 when the
* deprecated parameter 'output' is removed.
*/
TEST_CASE_METHOD(NBCTestFixture, "NBCOptionConsistencyTest",
"[NBCMainTest][BindingTests]")
{
arma::mat inputData;
if (!data::Load("trainSet.csv", inputData))
FAIL("Cannot load train dataset trainSet.csv!");
// Get the labels out.
arma::Row<size_t> labels(inputData.n_cols);
for (size_t i = 0; i < inputData.n_cols; ++i)
labels[i] = inputData(inputData.n_rows - 1, i);
// Delete the last row containing labels from input dataset.
inputData.shed_row(inputData.n_rows - 1);
arma::mat testData;
if (!data::Load("testSet.csv", testData))
FAIL("Cannot load test dataset testSet.csv!");
// Delete the last row containing labels from test dataset.
testData.shed_row(testData.n_rows - 1);
// Input training data.
SetInputParam("training", std::move(inputData));
SetInputParam("labels", std::move(labels));
// Input test data.
SetInputParam("test", std::move(testData));
RUN_BINDING();
// Get the output from the 'output' parameter.
const arma::Row<size_t> testY1 =
std::move(params.Get<arma::Row<size_t>>("output"));
// Get output from 'predictions' parameter.
const arma::Row<size_t> testY2 =
params.Get<arma::Row<size_t>>("predictions");
// Both solutions must be equal.
CheckMatrices(testY1, testY2);
}
/**
* This test ensures that the parameter 'output_probabilities' and the parameter
* 'probabilities' give the same output. This test case should be removed in
* mlpack 4 when the deprecated parameter: 'output_probabilities' is removed.
*/
TEST_CASE_METHOD(NBCTestFixture, "NBCOptionConsistencyTest2",
"[NBCMainTest][BindingTests]")
{
arma::mat inputData;
if (!data::Load("trainSet.csv", inputData))
FAIL("Cannot load train dataset trainSet.csv!");
// Get the labels out.
arma::Row<size_t> labels(inputData.n_cols);
for (size_t i = 0; i < inputData.n_cols; ++i)
labels[i] = inputData(inputData.n_rows - 1, i);
// Delete the last row containing labels from input dataset.
inputData.shed_row(inputData.n_rows - 1);
arma::mat testData;
if (!data::Load("testSet.csv", testData))
FAIL("Cannot load test dataset testSet.csv!");
// Delete the last row containing labels from test dataset.
testData.shed_row(testData.n_rows - 1);
// Input training data.
SetInputParam("training", std::move(inputData));
SetInputParam("labels", std::move(labels));
// Input test data.
SetInputParam("test", std::move(testData));
RUN_BINDING();
// Get the output probabilites which is a deprecated parameter.
const arma::mat testY1 =
std::move(params.Get<arma::mat>("output_probs"));
// Get probabilities from 'predictions' parameter.
const arma::mat testY2 =
params.Get<arma::mat>("probabilities");
// Both solutions must be equal.
CheckMatrices(testY1, testY2);
CheckMatrices(output, params.Get<arma::Row<size_t>>("predictions"));
CheckMatrices(output_probs, params.Get<arma::mat>("probabilities"));
}
+12 -46
View File
@@ -62,10 +62,10 @@ TEST_CASE_METHOD(PerceptronTestFixture, "PerceptronOutputDimensionTest",
RUN_BINDING();
// Check that number of output points are equal to number of input points.
REQUIRE(params.Get<arma::Row<size_t>>("output").n_cols == testSize);
REQUIRE(params.Get<arma::Row<size_t>>("predictions").n_cols == testSize);
// Check output have only single row.
REQUIRE(params.Get<arma::Row<size_t>>("output").n_rows == 1);
REQUIRE(params.Get<arma::Row<size_t>>("predictions").n_rows == 1);
}
/**
@@ -104,16 +104,16 @@ TEST_CASE_METHOD(PerceptronTestFixture, "PerceptronLabelsLessDimensionTest",
RUN_BINDING();
// Check that number of output points are equal to number of input points.
REQUIRE(params.Get<arma::Row<size_t>>("output").n_cols == testSize);
REQUIRE(params.Get<arma::Row<size_t>>("predictions").n_cols == testSize);
// Check output have only single row.
REQUIRE(params.Get<arma::Row<size_t>>("output").n_rows == 1);
REQUIRE(params.Get<arma::Row<size_t>>("predictions").n_rows == 1);
inputData.shed_row(inputData.n_rows - 1);
// Store outputs.
arma::Row<size_t> output;
output = std::move(params.Get<arma::Row<size_t>>("output"));
output = std::move(params.Get<arma::Row<size_t>>("predictions"));
// Reset data passed.
CleanMemory();
@@ -130,48 +130,14 @@ TEST_CASE_METHOD(PerceptronTestFixture, "PerceptronLabelsLessDimensionTest",
RUN_BINDING();
// Check that number of output points are equal to number of input points.
REQUIRE(params.Get<arma::Row<size_t>>("output").n_cols == testSize);
REQUIRE(params.Get<arma::Row<size_t>>("predictions").n_cols == testSize);
// Check output have only single row.
REQUIRE(params.Get<arma::Row<size_t>>("output").n_rows == 1);
REQUIRE(params.Get<arma::Row<size_t>>("predictions").n_rows == 1);
// Check that initial output and final output matrix
// from two models are same.
CheckMatrices(output, params.Get<arma::Row<size_t>>("output"));
}
/**
* This test can be removed in mlpack 4.0.0. This tests that the output and
* predictions outputs are the same.
*/
TEST_CASE_METHOD(PerceptronTestFixture, "PerceptronOutputPredictionsCheck",
"[PerceptronMainTest][BindingTests]")
{
arma::mat trainX1;
arma::Row<size_t> labelsX1;
// Loading a train data set with 3 classes.
if (!data::Load("vc2.csv", trainX1))
{
FAIL("Could not load the train data (vc2.csv)");
}
// Loading the corresponding labels to the dataset.
if (!data::Load("vc2_labels.txt", labelsX1))
{
FAIL("Could not load the train data (vc2_labels.csv)");
}
SetInputParam("training", std::move(trainX1)); // Training data.
// Labels for the training data.
SetInputParam("labels", std::move(labelsX1));
// Training model using first training dataset.
RUN_BINDING();
// Check that the outputs are the same.
CheckMatrices(params.Get<arma::Row<size_t>>("output"),
params.Get<arma::Row<size_t>>("predictions"));
CheckMatrices(output, params.Get<arma::Row<size_t>>("predictions"));
}
/**
@@ -202,7 +168,7 @@ TEST_CASE_METHOD(PerceptronTestFixture, "PerceptronModelReuseTest",
RUN_BINDING();
arma::Row<size_t> output;
output = std::move(params.Get<arma::Row<size_t>>("output"));
output = std::move(params.Get<arma::Row<size_t>>("predictions"));
// Reset passed parameters.
PerceptronModel* m = params.Get<PerceptronModel*>("output_model");
@@ -217,14 +183,14 @@ TEST_CASE_METHOD(PerceptronTestFixture, "PerceptronModelReuseTest",
RUN_BINDING();
// Check that number of output points are equal to number of input points.
REQUIRE(params.Get<arma::Row<size_t>>("output").n_cols == testSize);
REQUIRE(params.Get<arma::Row<size_t>>("predictions").n_cols == testSize);
// Check output have only single row.
REQUIRE(params.Get<arma::Row<size_t>>("output").n_rows == 1);
REQUIRE(params.Get<arma::Row<size_t>>("predictions").n_rows == 1);
// Check that initial output and final output matrix
// using saved model are same.
CheckMatrices(output, params.Get<arma::Row<size_t>>("output"));
CheckMatrices(output, params.Get<arma::Row<size_t>>("predictions"));
}
/**
-4
View File
@@ -8,10 +8,6 @@
* 3-clause BSD license along with mlpack. If not, see
* http://www.opensource.org/licenses/BSD-3-Clause for more information.
*/
// Note: We don't use BOOST_REQUIRE_CLOSE in the code below because we need
// to use FPC_WEAK, and it's not at all intuitive how to do that.
#include <mlpack/core.hpp>
#include <mlpack/methods/sparse_coding.hpp>