diff --git a/HISTORY.md b/HISTORY.md index cf727b6014..eebe1f387a 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -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). diff --git a/doc/user/core.md b/doc/user/core.md index 4fd8c02da8..ee3c0dd1f6 100644 --- a/doc/user/core.md +++ b/doc/user/core.md @@ -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. --- diff --git a/src/mlpack/bindings/cli/print_help_impl.hpp b/src/mlpack/bindings/cli/print_help_impl.hpp index ea10bd055b..dc886d6c59 100644 --- a/src/mlpack/bindings/cli/print_help_impl.hpp +++ b/src/mlpack/bindings/cli/print_help_impl.hpp @@ -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; diff --git a/src/mlpack/core/dists/regression_distribution.hpp b/src/mlpack/core/dists/regression_distribution.hpp index 51bcd2cf22..703bffd7c0 100644 --- a/src/mlpack/core/dists/regression_distribution.hpp +++ b/src/mlpack/core/dists/regression_distribution.hpp @@ -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. * diff --git a/src/mlpack/core/dists/regression_distribution_impl.hpp b/src/mlpack/core/dists/regression_distribution_impl.hpp index 1aa270d5d1..c1c9edde34 100644 --- a/src/mlpack/core/dists/regression_distribution_impl.hpp +++ b/src/mlpack/core/dists/regression_distribution_impl.hpp @@ -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 { diff --git a/src/mlpack/core/math/make_alias.hpp b/src/mlpack/core/math/make_alias.hpp index 93bcd0138a..473e466852 100644 --- a/src/mlpack/core/math/make_alias.hpp +++ b/src/mlpack/core/math/make_alias.hpp @@ -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 -void MakeAlias(MatType& m, - typename MatType::elem_type* newMem, - const size_t numRows, - const size_t numCols, +template +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::value>* = 0) + const typename std::enable_if_t::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(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 +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::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(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 -void MakeAlias(CubeType& c, - typename CubeType::elem_type* newMem, +template +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::value>* = 0) + const typename std::enable_if_t::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 -void MakeAlias(arma::Mat& m, - const arma::Mat& 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(oldCube.memptr()) + offset; + c.~OutCubeType(); + new (&c) OutCubeType(newMem, numRows, numCols, numSlices, false, strict); } /** @@ -75,6 +89,7 @@ void MakeAlias(arma::SpMat& m, const arma::SpMat& 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. diff --git a/src/mlpack/methods/adaboost/adaboost_classify_main.cpp b/src/mlpack/methods/adaboost/adaboost_classify_main.cpp index 9a6a8c0fa5..cf8ac7ec6a 100644 --- a/src/mlpack/methods/adaboost/adaboost_classify_main.cpp +++ b/src/mlpack/methods/adaboost/adaboost_classify_main.cpp @@ -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. diff --git a/src/mlpack/methods/adaboost/adaboost_main.cpp b/src/mlpack/methods/adaboost/adaboost_main.cpp index 7347660cb9..c47c4a74d8 100644 --- a/src/mlpack/methods/adaboost/adaboost_main.cpp +++ b/src/mlpack/methods/adaboost/adaboost_main.cpp @@ -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>("output") = results; if (params.Has("predictions")) params.Get>("predictions") = std::move(results); if (params.Has("probabilities")) diff --git a/src/mlpack/methods/ann/ffn_impl.hpp b/src/mlpack/methods/ann/ffn_impl.hpp index f95eec3ea1..dd0c6ea8ff 100644 --- a/src/mlpack/methods/ann/ffn_impl.hpp +++ b/src/mlpack/methods/ann/ffn_impl.hpp @@ -204,11 +204,12 @@ void FFN< const size_t effectiveBatchSize = std::min(batchSize, size_t(predictors.n_cols) - i); - const MatType predictorAlias( - const_cast(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; } diff --git a/src/mlpack/methods/ann/layer/add.hpp b/src/mlpack/methods/ann/layer/add.hpp index f21755e0ea..7d9158091b 100644 --- a/src/mlpack/methods/ann/layer/add.hpp +++ b/src/mlpack/methods/ann/layer/add.hpp @@ -94,7 +94,7 @@ class AddType : public Layer 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. diff --git a/src/mlpack/methods/ann/layer/add_impl.hpp b/src/mlpack/methods/ann/layer/add_impl.hpp index 036e464c1f..880b20c889 100644 --- a/src/mlpack/methods/ann/layer/add_impl.hpp +++ b/src/mlpack/methods/ann/layer/add_impl.hpp @@ -95,10 +95,10 @@ void AddType::Gradient( } template -void AddType::SetWeights(typename MatType::elem_type* weightPtr) +void AddType::SetWeights(const MatType& weightsIn) { // Set the weights to wrap the given memory. - MakeAlias(weights, weightPtr, 1, outSize); + MakeAlias(weights, weightsIn, 1, outSize); } template diff --git a/src/mlpack/methods/ann/layer/batch_norm.hpp b/src/mlpack/methods/ann/layer/batch_norm.hpp index 5b17df3a33..a9fe61478c 100644 --- a/src/mlpack/methods/ann/layer/batch_norm.hpp +++ b/src/mlpack/methods/ann/layer/batch_norm.hpp @@ -118,7 +118,7 @@ class BatchNormType : public Layer /** * 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 * @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 * @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; } diff --git a/src/mlpack/methods/ann/layer/batch_norm_impl.hpp b/src/mlpack/methods/ann/layer/batch_norm_impl.hpp index 6deaa29c27..d26d1204e1 100644 --- a/src/mlpack/methods/ann/layer/batch_norm_impl.hpp +++ b/src/mlpack/methods/ann/layer/batch_norm_impl.hpp @@ -148,14 +148,13 @@ BatchNormType::operator=( } template -void BatchNormType::SetWeights( - typename MatType::elem_type* weightsPtr) +void BatchNormType::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 @@ -170,9 +169,9 @@ void BatchNormType::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); diff --git a/src/mlpack/methods/ann/layer/concat_impl.hpp b/src/mlpack/methods/ann/layer/concat_impl.hpp index 0b94e10f27..f8e58d5824 100644 --- a/src/mlpack/methods/ann/layer/concat_impl.hpp +++ b/src/mlpack/methods/ann/layer/concat_impl.hpp @@ -123,19 +123,12 @@ void ConcatType::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 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::Backward( slices *= this->outputDimensions[i]; arma::Cube 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::Backward( slices *= this->outputDimensions[i]; arma::Cube 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::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 @@ -263,11 +244,7 @@ void ConcatType::Gradient( slices *= this->outputDimensions[i]; arma::Cube 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::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::Gradient( slices *= this->outputDimensions[i]; arma::Cube 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::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); } diff --git a/src/mlpack/methods/ann/layer/convolution.hpp b/src/mlpack/methods/ann/layer/convolution.hpp index 7cafe3d81f..087ab4e085 100644 --- a/src/mlpack/methods/ann/layer/convolution.hpp +++ b/src/mlpack/methods/ann/layer/convolution.hpp @@ -155,7 +155,7 @@ class ConvolutionType : public Layer /* * 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 diff --git a/src/mlpack/methods/ann/layer/convolution_impl.hpp b/src/mlpack/methods/ann/layer/convolution_impl.hpp index 6db085719a..c40a703f2c 100644 --- a/src/mlpack/methods/ann/layer/convolution_impl.hpp +++ b/src/mlpack/methods/ann/layer/convolution_impl.hpp @@ -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(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) diff --git a/src/mlpack/methods/ann/layer/dropconnect.hpp b/src/mlpack/methods/ann/layer/dropconnect.hpp index 3d6f19e773..b14587632a 100644 --- a/src/mlpack/methods/ann/layer/dropconnect.hpp +++ b/src/mlpack/methods/ann/layer/dropconnect.hpp @@ -124,7 +124,7 @@ class DropConnectType : public Layer 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. diff --git a/src/mlpack/methods/ann/layer/dropconnect_impl.hpp b/src/mlpack/methods/ann/layer/dropconnect_impl.hpp index c6333abb92..deec32b4af 100644 --- a/src/mlpack/methods/ann/layer/dropconnect_impl.hpp +++ b/src/mlpack/methods/ann/layer/dropconnect_impl.hpp @@ -155,10 +155,9 @@ void DropConnectType::ComputeOutputDimensions() } template -void DropConnectType::SetWeights( - typename MatType::elem_type* weightsPtr) +void DropConnectType::SetWeights(const MatType& weightsIn) { - baseLayer->SetWeights(weightsPtr); + baseLayer->SetWeights(weightsIn); } template diff --git a/src/mlpack/methods/ann/layer/flexible_relu.hpp b/src/mlpack/methods/ann/layer/flexible_relu.hpp index d4d920323a..71efbdde58 100644 --- a/src/mlpack/methods/ann/layer/flexible_relu.hpp +++ b/src/mlpack/methods/ann/layer/flexible_relu.hpp @@ -83,7 +83,7 @@ class FlexibleReLUType : public Layer * 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. diff --git a/src/mlpack/methods/ann/layer/flexible_relu_impl.hpp b/src/mlpack/methods/ann/layer/flexible_relu_impl.hpp index 1dd839219a..ef2d0cb8f1 100644 --- a/src/mlpack/methods/ann/layer/flexible_relu_impl.hpp +++ b/src/mlpack/methods/ann/layer/flexible_relu_impl.hpp @@ -74,10 +74,9 @@ FlexibleReLUType::operator=(FlexibleReLUType&& other) } template -void FlexibleReLUType::SetWeights( - typename MatType::elem_type* weightsPtr) +void FlexibleReLUType::SetWeights(const MatType& weights) { - MakeAlias(alpha, weightsPtr, 1, 1); + MakeAlias(alpha, weights, 1, 1); } template diff --git a/src/mlpack/methods/ann/layer/grouped_convolution.hpp b/src/mlpack/methods/ann/layer/grouped_convolution.hpp index fe22b3cba0..da478e60db 100644 --- a/src/mlpack/methods/ann/layer/grouped_convolution.hpp +++ b/src/mlpack/methods/ann/layer/grouped_convolution.hpp @@ -163,7 +163,7 @@ class GroupedConvolutionType : public Layer /* * 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 diff --git a/src/mlpack/methods/ann/layer/grouped_convolution_impl.hpp b/src/mlpack/methods/ann/layer/grouped_convolution_impl.hpp index 96152109cd..6c60e9b128 100644 --- a/src/mlpack/methods/ann/layer/grouped_convolution_impl.hpp +++ b/src/mlpack/methods/ann/layer/grouped_convolution_impl.hpp @@ -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(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; diff --git a/src/mlpack/methods/ann/layer/layer.hpp b/src/mlpack/methods/ann/layer/layer.hpp index a864e9c829..7906663b01 100644 --- a/src/mlpack/methods/ann/layer/layer.hpp +++ b/src/mlpack/methods/ann/layer/layer.hpp @@ -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. diff --git a/src/mlpack/methods/ann/layer/layer_norm.hpp b/src/mlpack/methods/ann/layer/layer_norm.hpp index 2d26e9ffd1..7a391a3377 100644 --- a/src/mlpack/methods/ann/layer/layer_norm.hpp +++ b/src/mlpack/methods/ann/layer/layer_norm.hpp @@ -134,7 +134,7 @@ class LayerNormType : public Layer size *= this->inputDimensions[i]; } - void SetWeights(typename MatType::elem_type* /* weightsPtr */) override; + void SetWeights(const MatType& weightsIn) override; void CustomInitialize( MatType& /* W */, diff --git a/src/mlpack/methods/ann/layer/layer_norm_impl.hpp b/src/mlpack/methods/ann/layer/layer_norm_impl.hpp index 7fafccae6e..ee5529f348 100644 --- a/src/mlpack/methods/ann/layer/layer_norm_impl.hpp +++ b/src/mlpack/methods/ann/layer/layer_norm_impl.hpp @@ -27,12 +27,11 @@ LayerNormType::LayerNormType(const double eps) : } template -void LayerNormType::SetWeights( - typename MatType::elem_type* weightsPtr) +void LayerNormType::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 @@ -48,9 +47,9 @@ void LayerNormType::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); diff --git a/src/mlpack/methods/ann/layer/linear.hpp b/src/mlpack/methods/ann/layer/linear.hpp index e43e9605d0..4adb13c31c 100644 --- a/src/mlpack/methods/ann/layer/linear.hpp +++ b/src/mlpack/methods/ann/layer/linear.hpp @@ -76,7 +76,7 @@ class LinearType : public Layer * 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 diff --git a/src/mlpack/methods/ann/layer/linear3d.hpp b/src/mlpack/methods/ann/layer/linear3d.hpp index 58c47c9508..6c20dc9618 100644 --- a/src/mlpack/methods/ann/layer/linear3d.hpp +++ b/src/mlpack/methods/ann/layer/linear3d.hpp @@ -68,7 +68,7 @@ class Linear3DType : public Layer /* * 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 diff --git a/src/mlpack/methods/ann/layer/linear3d_impl.hpp b/src/mlpack/methods/ann/layer/linear3d_impl.hpp index e4bf36d8ed..92d60c53e4 100644 --- a/src/mlpack/methods/ann/layer/linear3d_impl.hpp +++ b/src/mlpack/methods/ann/layer/linear3d_impl.hpp @@ -85,13 +85,12 @@ Linear3DType::operator=( } template -void Linear3DType::SetWeights( - typename MatType::elem_type* weightsPtr) +void Linear3DType::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 diff --git a/src/mlpack/methods/ann/layer/linear_impl.hpp b/src/mlpack/methods/ann/layer/linear_impl.hpp index 7bbc3a2ebb..59cee4bac5 100644 --- a/src/mlpack/methods/ann/layer/linear_impl.hpp +++ b/src/mlpack/methods/ann/layer/linear_impl.hpp @@ -93,12 +93,11 @@ LinearType::operator=( } template -void LinearType::SetWeights( - typename MatType::elem_type* weightsPtr) +void LinearType::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 diff --git a/src/mlpack/methods/ann/layer/linear_no_bias.hpp b/src/mlpack/methods/ann/layer/linear_no_bias.hpp index ad5d6f870c..15913a21db 100644 --- a/src/mlpack/methods/ann/layer/linear_no_bias.hpp +++ b/src/mlpack/methods/ann/layer/linear_no_bias.hpp @@ -52,7 +52,7 @@ class LinearNoBiasType : public Layer 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); diff --git a/src/mlpack/methods/ann/layer/linear_no_bias_impl.hpp b/src/mlpack/methods/ann/layer/linear_no_bias_impl.hpp index 537f33ee7f..0736b2a056 100644 --- a/src/mlpack/methods/ann/layer/linear_no_bias_impl.hpp +++ b/src/mlpack/methods/ann/layer/linear_no_bias_impl.hpp @@ -95,9 +95,9 @@ LinearNoBiasType::operator=( template void LinearNoBiasType::SetWeights( - typename MatType::elem_type* weightsPtr) + const MatType& weights) { - MakeAlias(weight, weightsPtr, outSize, inSize); + MakeAlias(weight, weights, outSize, inSize); } template diff --git a/src/mlpack/methods/ann/layer/lstm.hpp b/src/mlpack/methods/ann/layer/lstm.hpp index 6de2dac10c..2df33e21ea 100644 --- a/src/mlpack/methods/ann/layer/lstm.hpp +++ b/src/mlpack/methods/ann/layer/lstm.hpp @@ -87,7 +87,7 @@ class LSTMType : public RecurrentLayer * 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 diff --git a/src/mlpack/methods/ann/layer/lstm_impl.hpp b/src/mlpack/methods/ann/layer/lstm_impl.hpp index b240ea9811..66020982fc 100644 --- a/src/mlpack/methods/ann/layer/lstm_impl.hpp +++ b/src/mlpack/methods/ann/layer/lstm_impl.hpp @@ -93,59 +93,58 @@ void LSTMType::ClearRecurrentState( } template -void LSTMType::SetWeights( - typename MatType::elem_type* weightsPtr) +void LSTMType::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. diff --git a/src/mlpack/methods/ann/layer/multi_layer.hpp b/src/mlpack/methods/ann/layer/multi_layer.hpp index bad1ef6038..482bc012e2 100644 --- a/src/mlpack/methods/ann/layer/multi_layer.hpp +++ b/src/mlpack/methods/ann/layer/multi_layer.hpp @@ -128,7 +128,7 @@ class MultiLayer : public Layer /** * 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. diff --git a/src/mlpack/methods/ann/layer/multi_layer_impl.hpp b/src/mlpack/methods/ann/layer/multi_layer_impl.hpp index a286242663..d9027ffc51 100644 --- a/src/mlpack/methods/ann/layer/multi_layer_impl.hpp +++ b/src/mlpack/methods/ann/layer/multi_layer_impl.hpp @@ -235,7 +235,7 @@ void MultiLayer::Gradient( } template -void MultiLayer::SetWeights(typename MatType::elem_type* weightsPtr) +void MultiLayer::SetWeights(const MatType& weightsIn) { size_t start = 0; const size_t totalWeightSize = WeightSize(); @@ -248,8 +248,9 @@ void MultiLayer::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::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::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::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::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; } } diff --git a/src/mlpack/methods/ann/layer/multihead_attention.hpp b/src/mlpack/methods/ann/layer/multihead_attention.hpp index ec7e1b43ae..19faf6ca3a 100644 --- a/src/mlpack/methods/ann/layer/multihead_attention.hpp +++ b/src/mlpack/methods/ann/layer/multihead_attention.hpp @@ -102,7 +102,7 @@ class MultiheadAttentionType : public Layer /** * 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 diff --git a/src/mlpack/methods/ann/layer/multihead_attention_impl.hpp b/src/mlpack/methods/ann/layer/multihead_attention_impl.hpp index 62c2ff699f..b4d70bd60b 100644 --- a/src/mlpack/methods/ann/layer/multihead_attention_impl.hpp +++ b/src/mlpack/methods/ann/layer/multihead_attention_impl.hpp @@ -54,19 +54,19 @@ MultiheadAttentionType( template void MultiheadAttentionType::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 diff --git a/src/mlpack/methods/ann/layer/noisylinear.hpp b/src/mlpack/methods/ann/layer/noisylinear.hpp index 20bd2a4f74..ae442ce1db 100644 --- a/src/mlpack/methods/ann/layer/noisylinear.hpp +++ b/src/mlpack/methods/ann/layer/noisylinear.hpp @@ -52,7 +52,7 @@ class NoisyLinearType : public Layer 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(); diff --git a/src/mlpack/methods/ann/layer/noisylinear_impl.hpp b/src/mlpack/methods/ann/layer/noisylinear_impl.hpp index baf11dab2b..3fcb63ed97 100644 --- a/src/mlpack/methods/ann/layer/noisylinear_impl.hpp +++ b/src/mlpack/methods/ann/layer/noisylinear_impl.hpp @@ -73,17 +73,15 @@ NoisyLinearType::operator=(NoisyLinearType&& other) } template -void NoisyLinearType::SetWeights( - typename MatType::elem_type* weightsPtr) +void NoisyLinearType::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(); } diff --git a/src/mlpack/methods/ann/layer/parametric_relu.hpp b/src/mlpack/methods/ann/layer/parametric_relu.hpp index d36c14f93d..f8d6efd91c 100644 --- a/src/mlpack/methods/ann/layer/parametric_relu.hpp +++ b/src/mlpack/methods/ann/layer/parametric_relu.hpp @@ -68,7 +68,7 @@ class PReLUType : public Layer 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. diff --git a/src/mlpack/methods/ann/layer/parametric_relu_impl.hpp b/src/mlpack/methods/ann/layer/parametric_relu_impl.hpp index 4511f67f56..d42c880115 100644 --- a/src/mlpack/methods/ann/layer/parametric_relu_impl.hpp +++ b/src/mlpack/methods/ann/layer/parametric_relu_impl.hpp @@ -73,10 +73,9 @@ PReLUType::operator=(PReLUType&& other) } template -void PReLUType::SetWeights( - typename MatType::elem_type* weightsPtr) +void PReLUType::SetWeights(const MatType& weightsIn) { - MakeAlias(alpha, weightsPtr, 1, 1); + MakeAlias(alpha, weightsIn, 1, 1); } template diff --git a/src/mlpack/methods/ann/rnn_impl.hpp b/src/mlpack/methods/ann/rnn_impl.hpp index 39fb3c2e39..1f1b7dd525 100644 --- a/src/mlpack/methods/ann/rnn_impl.hpp +++ b/src/mlpack/methods/ann/rnn_impl.hpp @@ -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; diff --git a/src/mlpack/methods/block_krylov_svd/randomized_block_krylov_svd_impl.hpp b/src/mlpack/methods/block_krylov_svd/randomized_block_krylov_svd_impl.hpp index 567dd2ccbf..04c45c8e03 100644 --- a/src/mlpack/methods/block_krylov_svd/randomized_block_krylov_svd_impl.hpp +++ b/src/mlpack/methods/block_krylov_svd/randomized_block_krylov_svd_impl.hpp @@ -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); } diff --git a/src/mlpack/methods/decision_tree/decision_tree_main.cpp b/src/mlpack/methods/decision_tree/decision_tree_main.cpp index f4b68302bd..ae5a17776a 100644 --- a/src/mlpack/methods/decision_tree/decision_tree_main.cpp +++ b/src/mlpack/methods/decision_tree/decision_tree_main.cpp @@ -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 weights = std::move(params.Get>("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 predictions; arma::mat probabilities; diff --git a/src/mlpack/methods/lars/lars_impl.hpp b/src/mlpack/methods/lars/lars_impl.hpp index 3450ed304c..5c26a7a5dc 100644 --- a/src/mlpack/methods/lars/lars_impl.hpp +++ b/src/mlpack/methods/lars/lars_impl.hpp @@ -658,31 +658,6 @@ LARS::Train(const MatType& matX, if (maxCorr < tolerance) break; - // Floats require a really large tolerance for this condition. - const ElemType tol = (std::is_same::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::Train(const MatType& matX, // Compute signs of correlations. arma::Col 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::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(1, activeSet.size()); // This worked last iteration, so there can't be a singularity. solve(unnormalizedBetaDirection, diff --git a/src/mlpack/methods/linear_svm/linear_svm_function_impl.hpp b/src/mlpack/methods/linear_svm/linear_svm_function_impl.hpp index 86bffc0dc4..2dab353dab 100644 --- a/src/mlpack/methods/linear_svm/linear_svm_function_impl.hpp +++ b/src/mlpack/methods/linear_svm/linear_svm_function_impl.hpp @@ -35,7 +35,7 @@ LinearSVMFunction::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; diff --git a/src/mlpack/methods/lmnn/lmnn_function_impl.hpp b/src/mlpack/methods/lmnn/lmnn_function_impl.hpp index 1197ec49f0..38f0d95e95 100644 --- a/src/mlpack/methods/lmnn/lmnn_function_impl.hpp +++ b/src/mlpack/methods/lmnn/lmnn_function_impl.hpp @@ -34,8 +34,8 @@ LMNNFunction::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); diff --git a/src/mlpack/methods/logistic_regression/logistic_regression_function_impl.hpp b/src/mlpack/methods/logistic_regression/logistic_regression_function_impl.hpp index 3b7c2282b5..1489bc820c 100644 --- a/src/mlpack/methods/logistic_regression/logistic_regression_function_impl.hpp +++ b/src/mlpack/methods/logistic_regression/logistic_regression_function_impl.hpp @@ -28,9 +28,8 @@ LogisticRegressionFunction::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) diff --git a/src/mlpack/methods/naive_bayes/nbc_main.cpp b/src/mlpack/methods/naive_bayes/nbc_main.cpp index ad68c055c4..4bd9205a4b 100644 --- a/src/mlpack/methods/naive_bayes/nbc_main.cpp +++ b/src/mlpack/methods/naive_bayes/nbc_main.cpp @@ -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 rawResults; data::RevertLabels(predictions, model->mappings, rawResults); if (params.Has("predictions")) - params.Get>("predictions") = rawResults; - if (params.Has("output")) - params.Get>("output") = std::move(rawResults); + params.Get>("predictions") = std::move(rawResults); } - if (params.Has("output_probs") || params.Has("probabilities")) + + if (params.Has("probabilities")) { if (params.Has("probabilities")) - params.Get("probabilities") = probabilities; - if (params.Has("output_probs")) - params.Get("output_probs") = std::move(probabilities); + params.Get("probabilities") = std::move(probabilities); } } diff --git a/src/mlpack/methods/nca/nca_softmax_error_function_impl.hpp b/src/mlpack/methods/nca/nca_softmax_error_function_impl.hpp index 1ce40fb853..b7a4cfac8e 100644 --- a/src/mlpack/methods/nca/nca_softmax_error_function_impl.hpp +++ b/src/mlpack/methods/nca/nca_softmax_error_function_impl.hpp @@ -28,8 +28,8 @@ SoftmaxErrorFunction::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. diff --git a/src/mlpack/methods/perceptron/perceptron_main.cpp b/src/mlpack/methods/perceptron/perceptron_main.cpp index c024dccd13..96b4890085 100644 --- a/src/mlpack/methods/perceptron/perceptron_main.cpp +++ b/src/mlpack/methods/perceptron/perceptron_main.cpp @@ -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>("output") = results; if (params.Has("predictions")) params.Get>("predictions") = std::move(results); } diff --git a/src/mlpack/methods/softmax_regression/softmax_regression_function_impl.hpp b/src/mlpack/methods/softmax_regression/softmax_regression_function_impl.hpp index abe6da34f0..bbb6ba1974 100644 --- a/src/mlpack/methods/softmax_regression/softmax_regression_function_impl.hpp +++ b/src/mlpack/methods/softmax_regression/softmax_regression_function_impl.hpp @@ -27,7 +27,7 @@ inline SoftmaxRegressionFunction::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(); diff --git a/src/mlpack/tests/ann/convolutional_network_test.cpp b/src/mlpack/tests/ann/convolutional_network_test.cpp index 527b0a1816..313cee7c17 100644 --- a/src/mlpack/tests/ann/convolutional_network_test.cpp +++ b/src/mlpack/tests/ann/convolutional_network_test.cpp @@ -447,7 +447,7 @@ TEST_CASE("Issue2986", "[ConvolutionalNetworkTest]") c.InputDimensions() = std::vector({ 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); diff --git a/src/mlpack/tests/ann/layer/add.cpp b/src/mlpack/tests/ann/layer/add.cpp index c8aac866d1..ff69fd1041 100644 --- a/src/mlpack/tests/ann/layer/add.cpp +++ b/src/mlpack/tests/ann/layer/add.cpp @@ -32,7 +32,7 @@ TEST_CASE("AddManualWeightTestCase", "[ANNLayerTest]") Add module; module.InputDimensions() = std::vector({ 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({ 1 }); module.ComputeOutputDimensions(); - module.SetWeights(weights.memptr()); + module.SetWeights(weights); module.Parameters()[0] = 3.0; arma::mat output(1, 5); diff --git a/src/mlpack/tests/ann/layer/add_merge.cpp b/src/mlpack/tests/ann/layer/add_merge.cpp index 8631c1e7e5..3a6e84abd8 100644 --- a/src/mlpack/tests/ann/layer/add_merge.cpp +++ b/src/mlpack/tests/ann/layer/add_merge.cpp @@ -109,7 +109,7 @@ TEST_CASE("AddMergeAdvanceTestCase", "[ANNLayerTest]") r.InputDimensions() = std::vector({ 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({ 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)); diff --git a/src/mlpack/tests/ann/layer/batch_norm.cpp b/src/mlpack/tests/ann/layer/batch_norm.cpp index 2e73a3e135..63f13b0cac 100644 --- a/src/mlpack/tests/ann/layer/batch_norm.cpp +++ b/src/mlpack/tests/ann/layer/batch_norm.cpp @@ -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); diff --git a/src/mlpack/tests/ann/layer/concat.cpp b/src/mlpack/tests/ann/layer/concat.cpp index bd58f5b715..ab865d20b3 100644 --- a/src/mlpack/tests/ann/layer/concat.cpp +++ b/src/mlpack/tests/ann/layer/concat.cpp @@ -31,14 +31,14 @@ TEST_CASE("SimpleConcatLayerTest", "[ANNLayerTest]") moduleA->InputDimensions() = std::vector({ 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({ 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({ 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({ 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. diff --git a/src/mlpack/tests/ann/layer/convolution.cpp b/src/mlpack/tests/ann/layer/convolution.cpp index bc6fcf6721..4b932bdfe1 100644 --- a/src/mlpack/tests/ann/layer/convolution.cpp +++ b/src/mlpack/tests/ann/layer/convolution.cpp @@ -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(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(0, 48, 49); @@ -218,7 +218,7 @@ TEST_CASE("ConvolutionLayerTestCase", "[ANNLayerTest]") layer.InputDimensions() = std::vector({ 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({ 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({ 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); diff --git a/src/mlpack/tests/ann/layer/flexible_relu.cpp b/src/mlpack/tests/ann/layer/flexible_relu.cpp index 3400e6d17b..82b49769f2 100644 --- a/src/mlpack/tests/ann/layer/flexible_relu.cpp +++ b/src/mlpack/tests/ann/layer/flexible_relu.cpp @@ -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); diff --git a/src/mlpack/tests/ann/layer/grouped_convolution.cpp b/src/mlpack/tests/ann/layer/grouped_convolution.cpp index 4aeeb38349..d203e669e6 100644 --- a/src/mlpack/tests/ann/layer/grouped_convolution.cpp +++ b/src/mlpack/tests/ann/layer/grouped_convolution.cpp @@ -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({ 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); diff --git a/src/mlpack/tests/ann/layer/layer_norm.cpp b/src/mlpack/tests/ann/layer/layer_norm.cpp index 0da3a93941..94bece6073 100644 --- a/src/mlpack/tests/ann/layer/layer_norm.cpp +++ b/src/mlpack/tests/ann/layer/layer_norm.cpp @@ -36,7 +36,7 @@ TEST_CASE("LayerNormTest", "[ANNLayerTest]") model.InputDimensions() = std::vector({ 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); diff --git a/src/mlpack/tests/ann/layer/linear3d.cpp b/src/mlpack/tests/ann/layer/linear3d.cpp index bff130ca69..b65a7de681 100644 --- a/src/mlpack/tests/ann/layer/linear3d.cpp +++ b/src/mlpack/tests/ann/layer/linear3d.cpp @@ -36,7 +36,7 @@ TEST_CASE("SimpleLinear3DLayerTest", "[ANNLayerTest]") module.InputDimensions() = std::vector({ 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({ inSize, nPoints }); module.ComputeOutputDimensions(); arma::mat weights(module.WeightSize(), 1); - module.SetWeights(weights.memptr()); + module.SetWeights(weights); module.Parameters().randu(); diff --git a/src/mlpack/tests/ann/layer/linear_no_bias.cpp b/src/mlpack/tests/ann/layer/linear_no_bias.cpp index db46042a52..83523f6980 100644 --- a/src/mlpack/tests/ann/layer/linear_no_bias.cpp +++ b/src/mlpack/tests/ann/layer/linear_no_bias.cpp @@ -30,7 +30,7 @@ TEST_CASE("SimpleLinearNoBiasLayerTest", "[ANNLayerTest]") arma::mat weights(10 * 10, 1); module.InputDimensions() = std::vector({ 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({ inputElements }); module.ComputeOutputDimensions(); - module.SetWeights(weights.memptr()); + module.SetWeights(weights); module.Parameters().randu(); diff --git a/src/mlpack/tests/ann/layer/multihead_attention.cpp b/src/mlpack/tests/ann/layer/multihead_attention.cpp index a46046c712..c2da925b6d 100644 --- a/src/mlpack/tests/ann/layer/multihead_attention.cpp +++ b/src/mlpack/tests/ann/layer/multihead_attention.cpp @@ -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); diff --git a/src/mlpack/tests/ann/layer/parametric_relu.cpp b/src/mlpack/tests/ann/layer/parametric_relu.cpp index c300050087..56113fe73e 100644 --- a/src/mlpack/tests/ann/layer/parametric_relu.cpp +++ b/src/mlpack/tests/ann/layer/parametric_relu.cpp @@ -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}, diff --git a/src/mlpack/tests/lars_test.cpp b/src/mlpack/tests/lars_test.cpp index 6ce2214aa2..34ab5cb92f 100644 --- a/src/mlpack/tests/lars_test.cpp +++ b/src/mlpack/tests/lars_test.cpp @@ -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::value) ? 1e-10 : 1e-3; + const ElemType tol = (std::is_same::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 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 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 betaOptPred = (X * X.t()) * lars.Beta(); - arma::Row predictions; + arma::vec betaOptPred = (X * X.t()) * lars.Beta(); + arma::rowvec predictions; lars.Predict(X, predictions); - arma::Col adjPred = X * predictions.t(); + arma::vec adjPred = X * predictions.t(); - const ElemType tol = (std::is_same::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 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::value) ? 1e-5 : 1e-3; + const ElemType tol = (std::is_same::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 diff --git a/src/mlpack/tests/main_tests/adaboost_test.cpp b/src/mlpack/tests/main_tests/adaboost_test.cpp index c178989b3d..0661eaefd0 100644 --- a/src/mlpack/tests/main_tests/adaboost_test.cpp +++ b/src/mlpack/tests/main_tests/adaboost_test.cpp @@ -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>("output").n_cols == testSize); - REQUIRE(params.Get>("output").n_rows == 1); + REQUIRE(params.Get>("predictions").n_cols == testSize); + REQUIRE(params.Get>("predictions").n_rows == 1); } /** @@ -120,7 +120,7 @@ TEST_CASE_METHOD(AdaBoostTestFixture, "AdaBoostModelReuseTest", RUN_BINDING(); arma::Row output; - output = std::move(params.Get>("output")); + output = std::move(params.Get>("predictions")); AdaBoostModel* model = params.Get("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>("output")); + CheckMatrices(output, params.Get>("predictions")); } /** @@ -182,7 +182,7 @@ TEST_CASE_METHOD(AdaBoostTestFixture, "AdaBoostWithoutLabelTest", RUN_BINDING(); arma::Row output; - output = std::move(params.Get>("output")); + output = std::move(params.Get>("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>("output")); + CheckMatrices(output, params.Get>("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 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>("output"), - params.Get>("predictions")); -} - /** * Weak learner should be either Decision Stump or Perceptron. */ @@ -285,7 +261,7 @@ TEST_CASE_METHOD(AdaBoostTestFixture, "AdaBoostDiffWeakLearnerOutputTest", RUN_BINDING(); arma::Row output; - output = std::move(params.Get>("output")); + output = std::move(params.Get>("predictions")); CleanMemory(); ResetSettings(); @@ -298,7 +274,7 @@ TEST_CASE_METHOD(AdaBoostTestFixture, "AdaBoostDiffWeakLearnerOutputTest", RUN_BINDING(); arma::Row outputPerceptron; - outputPerceptron = std::move(params.Get>("output")); + outputPerceptron = std::move(params.Get>("predictions")); REQUIRE(accu(output != outputPerceptron) > 1); } diff --git a/src/mlpack/tests/main_tests/nbc_test.cpp b/src/mlpack/tests/main_tests/nbc_test.cpp index 094c1c5cbb..77378f4274 100644 --- a/src/mlpack/tests/main_tests/nbc_test.cpp +++ b/src/mlpack/tests/main_tests/nbc_test.cpp @@ -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>("output").n_cols == testSize); - REQUIRE(params.Get("output_probs").n_cols == testSize); + REQUIRE(params.Get>("predictions").n_cols == testSize); + REQUIRE(params.Get("probabilities").n_cols == testSize); // Check output have only single row. - REQUIRE(params.Get>("output").n_rows == 1); - REQUIRE(params.Get("output_probs").n_rows == 2); + REQUIRE(params.Get>("predictions").n_rows == 1); + REQUIRE(params.Get("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>("output").n_cols == testSize); - REQUIRE(params.Get("output_probs").n_cols == testSize); + REQUIRE(params.Get>("predictions").n_cols == testSize); + REQUIRE(params.Get("probabilities").n_cols == testSize); // Check output have only single row. - REQUIRE(params.Get>("output").n_rows == 1); - REQUIRE(params.Get("output_probs").n_rows == 2); + REQUIRE(params.Get>("predictions").n_rows == 1); + REQUIRE(params.Get("probabilities").n_rows == 2); // Store outputs. arma::Row output; arma::mat output_probs; - output = std::move(params.Get>("output")); - output_probs = std::move(params.Get("output_probs")); + output = std::move(params.Get>("predictions")); + output_probs = std::move(params.Get("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>("output").n_cols == testSize); - REQUIRE(params.Get("output_probs").n_cols == testSize); + REQUIRE(params.Get>("predictions").n_cols == testSize); + REQUIRE(params.Get("probabilities").n_cols == testSize); // Check output have only single row. - REQUIRE(params.Get>("output").n_rows == 1); - REQUIRE(params.Get("output_probs").n_rows == 2); + REQUIRE(params.Get>("predictions").n_rows == 1); + REQUIRE(params.Get("probabilities").n_rows == 2); // Check that initial output and final output matrix // from two models are same. - CheckMatrices(output, params.Get>("output")); - CheckMatrices(output_probs, params.Get("output_probs")); + CheckMatrices(output, params.Get>("predictions")); + CheckMatrices(output_probs, params.Get("probabilities")); } /** @@ -178,8 +178,8 @@ TEST_CASE_METHOD(NBCTestFixture, "NBCModelReuseTest", arma::Row output; arma::mat output_probs; - output = std::move(params.Get>("output")); - output_probs = std::move(params.Get("output_probs")); + output = std::move(params.Get>("predictions")); + output_probs = std::move(params.Get("probabilities")); // Reset passed parameters. NBCModel* m = params.Get("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>("output").n_cols == testSize); - REQUIRE(params.Get("output_probs").n_cols == testSize); + REQUIRE(params.Get>("predictions").n_cols == testSize); + REQUIRE(params.Get("probabilities").n_cols == testSize); // Check output have only single row. - REQUIRE(params.Get>("output").n_rows == 1); - REQUIRE(params.Get("output_probs").n_rows == 2); + REQUIRE(params.Get>("predictions").n_rows == 1); + REQUIRE(params.Get("probabilities").n_rows == 2); // Check that initial output and final output // matrix using saved model are same. - CheckMatrices(output, params.Get>("output")); - CheckMatrices(output_probs, params.Get("output_probs")); + CheckMatrices(output, params.Get>("predictions")); + CheckMatrices(output_probs, params.Get("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>("output").n_cols == testSize); - REQUIRE(params.Get("output_probs").n_cols == testSize); + REQUIRE(params.Get>("predictions").n_cols == testSize); + REQUIRE(params.Get("probabilities").n_cols == testSize); // Check output have only single row. - REQUIRE(params.Get>("output").n_rows == 1); - REQUIRE(params.Get("output_probs").n_rows == 2); + REQUIRE(params.Get>("predictions").n_rows == 1); + REQUIRE(params.Get("probabilities").n_rows == 2); // Store outputs. arma::Row output; arma::mat output_probs; - output = std::move(params.Get>("output")); - output_probs = std::move(params.Get("output_probs")); + output = std::move(params.Get>("predictions")); + output_probs = std::move(params.Get("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>("output").n_cols == testSize); - REQUIRE(params.Get("output_probs").n_cols == testSize); + REQUIRE(params.Get>("predictions").n_cols == testSize); + REQUIRE(params.Get("probabilities").n_cols == testSize); // Check output have only single row. - REQUIRE(params.Get>("output").n_rows == 1); - REQUIRE(params.Get("output_probs").n_rows == 2); + REQUIRE(params.Get>("predictions").n_rows == 1); + REQUIRE(params.Get("probabilities").n_rows == 2); // Check that initial output and final output matrix // from two models are same. - CheckMatrices(output, params.Get>("output")); - CheckMatrices(output_probs, params.Get("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 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 testY1 = - std::move(params.Get>("output")); - - // Get output from 'predictions' parameter. - const arma::Row testY2 = - params.Get>("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 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("output_probs")); - - // Get probabilities from 'predictions' parameter. - const arma::mat testY2 = - params.Get("probabilities"); - - // Both solutions must be equal. - CheckMatrices(testY1, testY2); + CheckMatrices(output, params.Get>("predictions")); + CheckMatrices(output_probs, params.Get("probabilities")); } diff --git a/src/mlpack/tests/main_tests/perceptron_test.cpp b/src/mlpack/tests/main_tests/perceptron_test.cpp index dca34f14a9..edc7b0e24e 100644 --- a/src/mlpack/tests/main_tests/perceptron_test.cpp +++ b/src/mlpack/tests/main_tests/perceptron_test.cpp @@ -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>("output").n_cols == testSize); + REQUIRE(params.Get>("predictions").n_cols == testSize); // Check output have only single row. - REQUIRE(params.Get>("output").n_rows == 1); + REQUIRE(params.Get>("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>("output").n_cols == testSize); + REQUIRE(params.Get>("predictions").n_cols == testSize); // Check output have only single row. - REQUIRE(params.Get>("output").n_rows == 1); + REQUIRE(params.Get>("predictions").n_rows == 1); inputData.shed_row(inputData.n_rows - 1); // Store outputs. arma::Row output; - output = std::move(params.Get>("output")); + output = std::move(params.Get>("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>("output").n_cols == testSize); + REQUIRE(params.Get>("predictions").n_cols == testSize); // Check output have only single row. - REQUIRE(params.Get>("output").n_rows == 1); + REQUIRE(params.Get>("predictions").n_rows == 1); // Check that initial output and final output matrix // from two models are same. - CheckMatrices(output, params.Get>("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 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>("output"), - params.Get>("predictions")); + CheckMatrices(output, params.Get>("predictions")); } /** @@ -202,7 +168,7 @@ TEST_CASE_METHOD(PerceptronTestFixture, "PerceptronModelReuseTest", RUN_BINDING(); arma::Row output; - output = std::move(params.Get>("output")); + output = std::move(params.Get>("predictions")); // Reset passed parameters. PerceptronModel* m = params.Get("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>("output").n_cols == testSize); + REQUIRE(params.Get>("predictions").n_cols == testSize); // Check output have only single row. - REQUIRE(params.Get>("output").n_rows == 1); + REQUIRE(params.Get>("predictions").n_rows == 1); // Check that initial output and final output matrix // using saved model are same. - CheckMatrices(output, params.Get>("output")); + CheckMatrices(output, params.Get>("predictions")); } /** diff --git a/src/mlpack/tests/sparse_coding_test.cpp b/src/mlpack/tests/sparse_coding_test.cpp index 661b946d6d..582ddad38e 100644 --- a/src/mlpack/tests/sparse_coding_test.cpp +++ b/src/mlpack/tests/sparse_coding_test.cpp @@ -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 #include