diff --git a/src/mlpack/bindings/python/print_pyx.cpp b/src/mlpack/bindings/python/print_pyx.cpp index 6853c969da..d2f03a8f4c 100644 --- a/src/mlpack/bindings/python/print_pyx.cpp +++ b/src/mlpack/bindings/python/print_pyx.cpp @@ -230,7 +230,8 @@ void PrintPYX(const util::BindingDetails& doc, << "\'bool'!\")" << endl; cout << endl; - // Before calling mlpackMain(), we check input matrices for NaN values if needed. + // Before calling mlpackMain(), we check input matrices for NaN values if + // needed. cout << " if check_input_matrices:" << endl; cout << " IO.CheckInputMatrices()" << endl; diff --git a/src/mlpack/core/cv/cv_base_impl.hpp b/src/mlpack/core/cv/cv_base_impl.hpp index 0da9f8f4fa..0a3d01e511 100644 --- a/src/mlpack/core/cv/cv_base_impl.hpp +++ b/src/mlpack/core/cv/cv_base_impl.hpp @@ -108,8 +108,8 @@ void CVBase::AssertDataConsistency(const MatType& xs, const PredictionsType& ys) { - util::CheckSameSizes(xs, (size_t) ys.n_cols, "CVBase::AssertDataConsistency()", - "predictions"); + util::CheckSameSizes(xs, (size_t) ys.n_cols, + "CVBase::AssertDataConsistency()", "predictions"); } template - static void Fn(const InputVecType &x, OutputVecType &y) + static void Fn(const InputVecType &x, OutputVecType &y) { - y = x / (1.0 + arma::exp(-x)); + y = x / (1.0 + arma::exp(-x)); } /** @@ -70,10 +72,10 @@ class SILUFunction * @param y Input activation. * @return f'(x) */ - static double Deriv(const double x) + static double Deriv(const double x) { - double sigmoid = 1.0 / (1.0 + std::exp(-x)); - return sigmoid * (1.0 + x * (1.0 - sigmoid)); + double sigmoid = 1.0 / (1.0 + std::exp(-x)); + return sigmoid * (1.0 + x * (1.0 - sigmoid)); } /** @@ -83,14 +85,14 @@ class SILUFunction * @param x The resulting derivatives. */ template - static void Deriv(const InputVecType &x, OutputVecType &y) + static void Deriv(const InputVecType &x, OutputVecType &y) { - OutputVecType sigmoid = 1.0 / (1.0 + arma::exp(-x)); - y = sigmoid % (1.0 + x % (1.0 - sigmoid)); + OutputVecType sigmoid = 1.0 / (1.0 + arma::exp(-x)); + y = sigmoid % (1.0 + x % (1.0 - sigmoid)); } }; // class SILUFunction } // namespace ann } // namespace mlpack -#endif \ No newline at end of file +#endif diff --git a/src/mlpack/methods/ann/activation_functions/tanh_exponential_function.hpp b/src/mlpack/methods/ann/activation_functions/tanh_exponential_function.hpp index cabe427a88..536e42f94c 100644 --- a/src/mlpack/methods/ann/activation_functions/tanh_exponential_function.hpp +++ b/src/mlpack/methods/ann/activation_functions/tanh_exponential_function.hpp @@ -8,7 +8,8 @@ * * @code * @misc{The Institution of Engineering and Technology 2015 , - * title = {TanhExp: A Smooth Activation Function with High Convergence Speed for Lightweight Neural Networks}, + * title = {TanhExp: A Smooth Activation Function with High Convergence Speed + * for Lightweight Neural Networks}, * author = {Xinyu Liu and Xiaoguang Di}, * year = {2020}, * url = {https://arxiv.org/pdf/2003.09855v2.pdf}, @@ -38,7 +39,7 @@ namespace ann /** Artificial Neural Network. */ { * f'(x) = tanh(e^x) - x*e^x*(tanh(e^x)^2 - 1)\\ * @f} */ - class TanhExpFunction +class TanhExpFunction { public: /** diff --git a/src/mlpack/methods/ann/ffn_impl.hpp b/src/mlpack/methods/ann/ffn_impl.hpp index 401c094ca6..921ded0bd7 100644 --- a/src/mlpack/methods/ann/ffn_impl.hpp +++ b/src/mlpack/methods/ann/ffn_impl.hpp @@ -111,8 +111,8 @@ double FFN::Train( OptimizerType& optimizer, CallbackTypes&&... callbacks) { - CheckInputShape > >(network, - predictors.n_rows, + CheckInputShape > >(network, + predictors.n_rows, "FFN<>::Train()"); ResetData(std::move(predictors), std::move(responses)); @@ -137,8 +137,8 @@ double FFN::Train( arma::mat responses, CallbackTypes&&... callbacks) { - CheckInputShape > >(network, - predictors.n_rows, + CheckInputShape > >(network, + predictors.n_rows, "FFN<>::Train()"); ResetData(std::move(predictors), std::move(responses)); @@ -227,9 +227,8 @@ template::Predict( arma::mat predictors, arma::mat& results) { - CheckInputShape > >(network, - predictors.n_rows, - "FFN<>::Predict()"); + CheckInputShape > >( + network, predictors.n_rows, "FFN<>::Predict()"); if (parameter.is_empty()) ResetParameters(); @@ -264,9 +263,8 @@ template double FFN::Evaluate( const PredictorsType& predictors, const ResponsesType& responses) { - CheckInputShape > >(network, - predictors.n_rows, - "FFN<>::Evaluate()"); + CheckInputShape > >( + network, predictors.n_rows, "FFN<>::Evaluate()"); if (parameter.is_empty()) ResetParameters(); diff --git a/src/mlpack/methods/ann/layer/atrous_convolution.hpp b/src/mlpack/methods/ann/layer/atrous_convolution.hpp index daddab76f2..d6086de86f 100644 --- a/src/mlpack/methods/ann/layer/atrous_convolution.hpp +++ b/src/mlpack/methods/ann/layer/atrous_convolution.hpp @@ -71,7 +71,8 @@ class AtrousConvolution * @param inputWidth The widht of the input data. * @param inputHeight The height of the input data. * @param dilationWidth The space between the cells of filters in x direction. - * @param dilationHeight The space between the cells of filters in y direction. + * @param dilationHeight The space between the cells of filters in y + * direction. * @param paddingType The type of padding (Valid or Same). Defaults to None. */ AtrousConvolution(const size_t inSize, @@ -108,7 +109,8 @@ class AtrousConvolution * @param inputWidth The widht of the input data. * @param inputHeight The height of the input data. * @param dilationWidth The space between the cells of filters in x direction. - * @param dilationHeight The space between the cells of filters in y direction. + * @param dilationHeight The space between the cells of filters in y + * direction. * @param paddingType The type of padding (Valid/Same/None). Defaults to None. */ AtrousConvolution(const size_t inSize, @@ -266,8 +268,8 @@ class AtrousConvolution //! Get the shape of the input. size_t InputShape() const { - return inputHeight * inputWidth * inSize; - } + return inputHeight * inputWidth * inSize; + } /** * Serialize the layer. diff --git a/src/mlpack/methods/ann/layer/base_layer.hpp b/src/mlpack/methods/ann/layer/base_layer.hpp index 9c6bd19478..e2d7aaf809 100644 --- a/src/mlpack/methods/ann/layer/base_layer.hpp +++ b/src/mlpack/methods/ann/layer/base_layer.hpp @@ -29,7 +29,7 @@ #include #include #include -#include +#include namespace mlpack { namespace ann /** Artificial Neural Network. */ { @@ -314,7 +314,7 @@ template < typename OutputDataType = arma::mat > using SILUFunctionLayer = BaseLayer< - ActivationFunction, InputDataType,OutputDataType + ActivationFunction, InputDataType, OutputDataType >; } // namespace ann diff --git a/src/mlpack/methods/ann/layer/concatenate_impl.hpp b/src/mlpack/methods/ann/layer/concatenate_impl.hpp index bfede6c162..cf85443cf6 100644 --- a/src/mlpack/methods/ann/layer/concatenate_impl.hpp +++ b/src/mlpack/methods/ann/layer/concatenate_impl.hpp @@ -27,7 +27,7 @@ Concatenate::Concatenate() : } template -Concatenate::Concatenate(const Concatenate& layer) : +Concatenate::Concatenate(const Concatenate& layer) : inRows(layer.inRows), weights(layer.weights), delta(layer.delta), @@ -37,7 +37,7 @@ Concatenate::Concatenate(const Concatenate& layer } template -Concatenate::Concatenate(Concatenate&& layer) : +Concatenate::Concatenate(Concatenate&& layer) : inRows(layer.inRows), weights(std::move(layer.weights)), delta(std::move(layer.delta)), @@ -51,7 +51,7 @@ Concatenate& Concatenate:: operator=(const Concatenate& layer) { - if (this != &layer) + if (this != &layer) { inRows = layer.inRows; weights = layer.weights; @@ -67,7 +67,7 @@ Concatenate& Concatenate:: operator=(Concatenate&& layer) { - if (this != &layer) + if (this != &layer) { inRows = layer.inRows; weights = std::move(layer.weights); diff --git a/src/mlpack/methods/ann/layer/flatten_t_swish_impl.hpp b/src/mlpack/methods/ann/layer/flatten_t_swish_impl.hpp index 1ce5364da8..edc616ac3c 100644 --- a/src/mlpack/methods/ann/layer/flatten_t_swish_impl.hpp +++ b/src/mlpack/methods/ann/layer/flatten_t_swish_impl.hpp @@ -48,9 +48,9 @@ void FlattenTSwish::Backward( const DataType& input, const DataType& gy, DataType& g) { DataType derivate, sigmoid; - LogisticFunction::Fn(input,sigmoid); + LogisticFunction::Fn(input, sigmoid); derivate.set_size(arma::size(input)); - for(size_t i = 0; i < input.n_elem; ++i) + for(size_t i = 0; i < input.n_elem; ++i) { if (input(i) >= 0) { @@ -58,9 +58,11 @@ void FlattenTSwish::Backward( // We don't put '+ t' here because this is a derivate. derivate(i) = input(i) * sigmoid(i); derivate(i) = sigmoid(i) * (1.0 - derivate(i)) + derivate(i); - } - else + } + else + { derivate(i) = 0; + } } g = gy % derivate; } @@ -77,4 +79,4 @@ void FlattenTSwish::serialize( } // namespace ann } // namespace mlpack -#endif \ No newline at end of file +#endif diff --git a/src/mlpack/methods/ann/layer/gru.hpp b/src/mlpack/methods/ann/layer/gru.hpp index 3d98a712d8..c895ee7f81 100644 --- a/src/mlpack/methods/ann/layer/gru.hpp +++ b/src/mlpack/methods/ann/layer/gru.hpp @@ -156,7 +156,7 @@ class GRU size_t OutSize() const { return outSize; } //! Get the shape of the input. - size_t InputShape() const + size_t InputShape() const { return inSize; } diff --git a/src/mlpack/methods/ann/layer/isrlu.hpp b/src/mlpack/methods/ann/layer/isrlu.hpp index b0a786c6ba..36722b91c3 100644 --- a/src/mlpack/methods/ann/layer/isrlu.hpp +++ b/src/mlpack/methods/ann/layer/isrlu.hpp @@ -126,7 +126,6 @@ class ISRLU //! ISRLU Hyperparameter (alpha > 0). double alpha; - }; // class ISRLU } // namespace ann diff --git a/src/mlpack/methods/ann/layer/linear.hpp b/src/mlpack/methods/ann/layer/linear.hpp index cc31117c53..5b9d23bd87 100644 --- a/src/mlpack/methods/ann/layer/linear.hpp +++ b/src/mlpack/methods/ann/layer/linear.hpp @@ -152,7 +152,7 @@ class Linear return (inSize * outSize) + outSize; } - //! Get the shape of the input. + //! Get the shape of the input. size_t InputShape() const { return inSize; diff --git a/src/mlpack/methods/ann/layer/lp_pooling.hpp b/src/mlpack/methods/ann/layer/lp_pooling.hpp index 03ef9f540e..1c2b841e24 100644 --- a/src/mlpack/methods/ann/layer/lp_pooling.hpp +++ b/src/mlpack/methods/ann/layer/lp_pooling.hpp @@ -196,11 +196,12 @@ class LpPooling const arma::Mat& error, arma::Mat& output) { - arma::Mat unpooledError; - for (size_t j = 0, colidx = 0; j < input.n_cols; j += strideHeight, colidx++) + for (size_t j = 0, colidx = 0; j < input.n_cols; j += strideHeight, + colidx++) { - for (size_t i = 0, rowidx = 0; i < input.n_rows; i += strideWidth, rowidx++) + for (size_t i = 0, rowidx = 0; i < input.n_rows; i += strideWidth, + rowidx++) { size_t rowEnd = i + kernelWidth - 1; size_t colEnd = j + kernelHeight - 1; @@ -219,7 +220,8 @@ class LpPooling colEnd = input.n_cols - 1; } - arma::mat InputArea = input(arma::span(i, rowEnd), arma::span(j, colEnd)); + arma::mat InputArea = input(arma::span(i, rowEnd), + arma::span(j, colEnd)); size_t sum = pow(arma::accu(arma::pow(InputArea, normType)), (normType - 1) / normType); diff --git a/src/mlpack/methods/ann/layer/lstm.hpp b/src/mlpack/methods/ann/layer/lstm.hpp index 98be4b500f..effca7328e 100644 --- a/src/mlpack/methods/ann/layer/lstm.hpp +++ b/src/mlpack/methods/ann/layer/lstm.hpp @@ -184,7 +184,10 @@ class LSTM size_t OutSize() const { return outSize; } //! Get the size of the weights. - size_t WeightSize() const { return (4 * outSize * inSize + 7 * outSize + 4 * outSize * outSize); } + size_t WeightSize() const + { + return (4 * outSize * inSize + 7 * outSize + 4 * outSize * outSize); + } //! Get the shape of the input. size_t InputShape() const diff --git a/src/mlpack/methods/ann/layer/lstm_impl.hpp b/src/mlpack/methods/ann/layer/lstm_impl.hpp index 9d720732c4..2b298d18aa 100644 --- a/src/mlpack/methods/ann/layer/lstm_impl.hpp +++ b/src/mlpack/methods/ann/layer/lstm_impl.hpp @@ -26,7 +26,7 @@ LSTM::LSTM() template LSTM::LSTM( - const LSTM& layer) : + const LSTM& layer) : inSize(layer.inSize), outSize(layer.outSize), rho(layer.rho), @@ -45,7 +45,7 @@ LSTM::LSTM( template LSTM::LSTM( - LSTM&& layer) : + LSTM&& layer) : inSize(std::move(layer.inSize)), outSize(std::move(layer.outSize)), rho(std::move(layer.rho)), @@ -63,7 +63,7 @@ LSTM::LSTM( } template -LSTM& +LSTM& LSTM :: operator=(const LSTM& layer) { if (this != &layer) @@ -82,11 +82,11 @@ LSTM :: operator=(const LSTM& layer) rhoSize = layer.rho; bpttSteps = layer.bpttSteps; } - return *this; + return *this; } template -LSTM& +LSTM& LSTM :: operator=(LSTM&& layer) { if (this != &layer) @@ -105,7 +105,7 @@ LSTM :: operator=(LSTM&& layer) rhoSize = std::move(layer.rho); bpttSteps = std::move(layer.bpttSteps); } - return *this; + return *this; } template diff --git a/src/mlpack/methods/ann/layer/mean_pooling.hpp b/src/mlpack/methods/ann/layer/mean_pooling.hpp index 2480a9525f..afba6470c7 100644 --- a/src/mlpack/methods/ann/layer/mean_pooling.hpp +++ b/src/mlpack/methods/ann/layer/mean_pooling.hpp @@ -164,7 +164,7 @@ class MeanPooling for (size_t i = 1; i < input.n_cols; ++i) inputPre.col(i) += inputPre.col(i - 1); - + for (size_t i = 1; i < input.n_rows; ++i) inputPre.row(i) += inputPre.row(i - 1); @@ -210,12 +210,13 @@ class MeanPooling const arma::Mat& error, arma::Mat& output) { - arma::Mat unpooledError; - for (size_t j = 0, colidx = 0; j < input.n_cols; j += strideHeight, colidx++) + for (size_t j = 0, colidx = 0; j < input.n_cols; j += strideHeight, + colidx++) { - for (size_t i = 0, rowidx = 0; i < input.n_rows; i += strideWidth, rowidx++) - { + for (size_t i = 0, rowidx = 0; i < input.n_rows; i += strideWidth, + rowidx++) + { size_t rowEnd = i + kernelWidth - 1; size_t colEnd = j + kernelHeight - 1; @@ -233,7 +234,8 @@ class MeanPooling colEnd = input.n_cols - 1; } - arma::mat InputArea = input(arma::span(i, rowEnd), arma::span(j, colEnd)); + arma::mat InputArea = input(arma::span(i, rowEnd), + arma::span(j, colEnd)); unpooledError = arma::Mat(InputArea.n_rows, InputArea.n_cols); unpooledError.fill(error(rowidx, colidx) / InputArea.n_elem); diff --git a/src/mlpack/methods/ann/layer/pixel_shuffle_impl.hpp b/src/mlpack/methods/ann/layer/pixel_shuffle_impl.hpp index f56f708981..4de0816990 100644 --- a/src/mlpack/methods/ann/layer/pixel_shuffle_impl.hpp +++ b/src/mlpack/methods/ann/layer/pixel_shuffle_impl.hpp @@ -77,12 +77,11 @@ void PixelShuffle::Forward( size_t width_index = w / upscaleFactor; size_t channel_index = (upscaleFactor * (h % upscaleFactor)) + (w % upscaleFactor) + (c * std::pow(upscaleFactor, 2)); - outputTemp(w, h, c + n * sizeOut) = inputTemp(width_index, height_index, - channel_index + n * size); + outputTemp(w, h, c + n * sizeOut) = inputTemp(width_index, + height_index, channel_index + n * size); } } } - } } @@ -109,12 +108,11 @@ void PixelShuffle::Backward( size_t width_index = w / upscaleFactor; size_t channel_index = (upscaleFactor * (h % upscaleFactor)) + (w % upscaleFactor) + (c * std::pow(upscaleFactor, 2)); - gTemp(width_index, height_index, channel_index + n * size) = gyTemp(w, h, - c + n * sizeOut); + gTemp(width_index, height_index, channel_index + n * size) = + gyTemp(w, h, c + n * sizeOut); } } } - } } diff --git a/src/mlpack/methods/ann/layer/recurrent_impl.hpp b/src/mlpack/methods/ann/layer/recurrent_impl.hpp index e6b933bd20..046c48fa0f 100644 --- a/src/mlpack/methods/ann/layer/recurrent_impl.hpp +++ b/src/mlpack/methods/ann/layer/recurrent_impl.hpp @@ -128,9 +128,12 @@ Recurrent::Recurrent( template -size_t Recurrent::InputShape() const +size_t +Recurrent::InputShape() const { - const size_t inputShapeStartModule = boost::apply_visitor(InShapeVisitor(), startModule); + const size_t inputShapeStartModule = boost::apply_visitor(InShapeVisitor(), + startModule); + // Return the input shape of the first module that we have. if (inputShapeStartModule != 0) { @@ -140,34 +143,34 @@ size_t Recurrent::InputShape() c else { // Return input shape of the second module that we have. - const size_t inputShapeInputModule = boost::apply_visitor(InShapeVisitor(), inputModule); + const size_t inputShapeInputModule = boost::apply_visitor(InShapeVisitor(), + inputModule); if (inputShapeInputModule != 0) { return inputShapeInputModule; - // If the input shape of second module is 0. } - else + else // If the input shape of second module is 0. { // Return input shape of the third module that we have. - const size_t inputShapeFeedbackModule = boost::apply_visitor(InShapeVisitor(), - feedbackModule); + const size_t inputShapeFeedbackModule = boost::apply_visitor( + InShapeVisitor(), feedbackModule); if (inputShapeFeedbackModule != 0) { return inputShapeFeedbackModule; - // If the input shape of the third module is 0. } - else + else // If the input shape of the third module is 0. { // Return the shape of the fourth module that we have. - const size_t inputShapeTransferModule = boost::apply_visitor(InShapeVisitor(), - transferModule); + const size_t inputShapeTransferModule = boost::apply_visitor( + InShapeVisitor(), transferModule); if (inputShapeTransferModule != 0) { return inputShapeTransferModule; } - // If the input shape of the fourth module is 0. - else + else // If the input shape of the fourth module is 0. + { return 0; + } } } } diff --git a/src/mlpack/methods/ann/layer/reparametrization.hpp b/src/mlpack/methods/ann/layer/reparametrization.hpp index a526d746bf..d3a183b9fd 100644 --- a/src/mlpack/methods/ann/layer/reparametrization.hpp +++ b/src/mlpack/methods/ann/layer/reparametrization.hpp @@ -71,16 +71,16 @@ class Reparametrization const bool stochastic = true, const bool includeKl = true, const double beta = 1); - + //! Copy Constructor. Reparametrization(const Reparametrization& layer); - + //! Move Constructor. Reparametrization(Reparametrization&& layer); - + //! Copy assignment operator. Reparametrization& operator=(const Reparametrization& layer); - + //! Move assignment operator. Reparametrization& operator=(Reparametrization&& layer); diff --git a/src/mlpack/methods/ann/layer/reparametrization_impl.hpp b/src/mlpack/methods/ann/layer/reparametrization_impl.hpp index cef6a32b0d..117e67a620 100644 --- a/src/mlpack/methods/ann/layer/reparametrization_impl.hpp +++ b/src/mlpack/methods/ann/layer/reparametrization_impl.hpp @@ -46,7 +46,7 @@ Reparametrization::Reparametrization( << "included." << std::endl; } } - + template Reparametrization::Reparametrization( const Reparametrization& layer) : @@ -55,7 +55,7 @@ Reparametrization::Reparametrization( includeKl(layer.includeKl), beta(layer.beta) { - // Nothing to do here. + // Nothing to do here. } template @@ -66,13 +66,13 @@ Reparametrization::Reparametrization( includeKl(std::move(layer.includeKl)), beta(std::move(layer.beta)) { - // Nothing to do here. + // Nothing to do here. } - + template Reparametrization& Reparametrization:: -operator=(const Reparametrization& layer) +operator=(const Reparametrization& layer) { if (this != &layer) { @@ -83,11 +83,11 @@ operator=(const Reparametrization& layer) } return *this; } - + template Reparametrization& Reparametrization:: -operator=(Reparametrization&& layer) +operator=(Reparametrization&& layer) { if (this != &layer) { @@ -98,8 +98,8 @@ operator=(Reparametrization&& layer) } return *this; } - - + + template template void Reparametrization::Forward( diff --git a/src/mlpack/methods/ann/loss_functions/binary_cross_entropy_loss_impl.hpp b/src/mlpack/methods/ann/loss_functions/binary_cross_entropy_loss_impl.hpp index 89e7aaf1c2..4555240b88 100644 --- a/src/mlpack/methods/ann/loss_functions/binary_cross_entropy_loss_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/binary_cross_entropy_loss_impl.hpp @@ -34,7 +34,7 @@ BCELoss::Forward( { typedef typename PredictionType::elem_type ElemType; - ElemType loss = -arma::accu(target % arma::log(prediction + eps) + + ElemType loss = -arma::accu(target % arma::log(prediction + eps) + (1. - target) % arma::log(1. - prediction + eps)); if (reduction) loss /= prediction.n_elem; diff --git a/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp b/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp index d692734754..50b2c61858 100644 --- a/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/huber_loss_impl.hpp @@ -31,16 +31,17 @@ HuberLoss::HuberLoss( template template typename PredictionType::elem_type -HuberLoss::Forward(const PredictionType& prediction, - const TargetType& target) +HuberLoss::Forward( + const PredictionType& prediction, + const TargetType& target) { typedef typename PredictionType::elem_type ElemType; ElemType loss = 0; for (size_t i = 0; i < prediction.n_elem; ++i) { const ElemType absError = std::abs(target[i] - prediction[i]); - loss += absError > delta - ? delta * (absError - 0.5 * delta) : 0.5 * std::pow(absError, 2); + loss += absError > delta ? + delta * (absError - 0.5 * delta) : 0.5 * std::pow(absError, 2); } return mean ? loss / prediction.n_elem : loss; } @@ -58,8 +59,9 @@ void HuberLoss::Backward( for (size_t i = 0; i < loss.n_elem; ++i) { const ElemType absError = std::abs(target[i] - prediction[i]); - loss[i] = absError > delta - ? - delta * (target[i] - prediction[i]) / absError : prediction[i] - target[i]; + loss[i] = absError > delta ? + -delta * (target[i] - prediction[i]) / absError : + prediction[i] - target[i]; if (mean) loss[i] /= loss.n_elem; } diff --git a/src/mlpack/methods/ann/loss_functions/kl_divergence_impl.hpp b/src/mlpack/methods/ann/loss_functions/kl_divergence_impl.hpp index aa1a5c1b62..9c74453a21 100644 --- a/src/mlpack/methods/ann/loss_functions/kl_divergence_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/kl_divergence_impl.hpp @@ -29,8 +29,9 @@ KLDivergence::KLDivergence(const bool takeMean) : template template typename PredictionType::elem_type -KLDivergence::Forward(const PredictionType& prediction, - const TargetType& target) +KLDivergence::Forward( + const PredictionType& prediction, + const TargetType& target) { if (takeMean) { @@ -52,7 +53,8 @@ void KLDivergence::Backward( { if (takeMean) { - loss = arma::mean(arma::mean(arma::log(prediction) - arma::log(target) + 1)); + loss = arma::mean(arma::mean( + arma::log(prediction) - arma::log(target) + 1)); } else { diff --git a/src/mlpack/methods/ann/loss_functions/sigmoid_cross_entropy_error.hpp b/src/mlpack/methods/ann/loss_functions/sigmoid_cross_entropy_error.hpp index 2d0bed9721..a1f4384e7f 100644 --- a/src/mlpack/methods/ann/loss_functions/sigmoid_cross_entropy_error.hpp +++ b/src/mlpack/methods/ann/loss_functions/sigmoid_cross_entropy_error.hpp @@ -65,8 +65,10 @@ class SigmoidCrossEntropyError * @param target The target vector. */ template - inline typename PredictionType::elem_type Forward(const PredictionType& prediction, - const TargetType& target); + inline typename PredictionType::elem_type Forward( + const PredictionType& prediction, + const TargetType& target); + /** * Ordinary feed backward pass of a neural network. * diff --git a/src/mlpack/methods/ann/loss_functions/triplet_margin_loss.hpp b/src/mlpack/methods/ann/loss_functions/triplet_margin_loss.hpp index fba54973f0..980d863d17 100644 --- a/src/mlpack/methods/ann/loss_functions/triplet_margin_loss.hpp +++ b/src/mlpack/methods/ann/loss_functions/triplet_margin_loss.hpp @@ -108,4 +108,4 @@ class TripletMarginLoss // include implementation. #include "triplet_margin_loss_impl.hpp" -#endif \ No newline at end of file +#endif diff --git a/src/mlpack/methods/ann/loss_functions/triplet_margin_loss_impl.hpp b/src/mlpack/methods/ann/loss_functions/triplet_margin_loss_impl.hpp index a007490be0..2a43bc4ac4 100644 --- a/src/mlpack/methods/ann/loss_functions/triplet_margin_loss_impl.hpp +++ b/src/mlpack/methods/ann/loss_functions/triplet_margin_loss_impl.hpp @@ -33,8 +33,10 @@ TripletMarginLoss::Forward( const PredictionType& prediction, const TargetType& target) { - PredictionType anchor = prediction.submat(0, 0, prediction.n_rows / 2 - 1, prediction.n_cols - 1); - PredictionType positive = prediction.submat(prediction.n_rows / 2, 0, prediction.n_rows - 1, + PredictionType anchor = + prediction.submat(0, 0, prediction.n_rows / 2 - 1, prediction.n_cols - 1); + PredictionType positive = + prediction.submat(prediction.n_rows / 2, 0, prediction.n_rows - 1, prediction.n_cols - 1); return std::max(0.0, arma::accu(arma::pow(anchor - positive, 2)) - arma::accu(arma::pow(anchor - target, 2)) + margin) / anchor.n_cols; @@ -51,7 +53,8 @@ void TripletMarginLoss::Backward( const TargetType& target, LossType& loss) { - PredictionType positive = prediction.submat(prediction.n_rows / 2, 0, prediction.n_rows - 1, + PredictionType positive = + prediction.submat(prediction.n_rows / 2, 0, prediction.n_rows - 1, prediction.n_cols - 1); loss = 2 * (target - positive) / target.n_cols; } diff --git a/src/mlpack/methods/ann/rnn_impl.hpp b/src/mlpack/methods/ann/rnn_impl.hpp index 5077eb9896..3e7d6d0323 100644 --- a/src/mlpack/methods/ann/rnn_impl.hpp +++ b/src/mlpack/methods/ann/rnn_impl.hpp @@ -149,9 +149,8 @@ double RNN::Train( OptimizerType& optimizer, CallbackTypes&&... callbacks) { - CheckInputShape > >(network, - predictors.n_rows, - "RNN<>::Train()"); + CheckInputShape > >( + network, predictors.n_rows, "RNN<>::Train()"); numFunctions = responses.n_cols; @@ -197,9 +196,8 @@ double RNN::Train( arma::cube responses, CallbackTypes&&... callbacks) { - CheckInputShape > >(network, - predictors.n_rows, - "RNN<>::Train()"); + CheckInputShape > >( + network, predictors.n_rows, "RNN<>::Train()"); numFunctions = responses.n_cols; @@ -233,9 +231,8 @@ template::Predict( arma::cube predictors, arma::cube& results, const size_t batchSize) { - CheckInputShape > >(network, - predictors.n_rows, - "RNN<>::Predict()"); + CheckInputShape > >( + network, predictors.n_rows, "RNN<>::Predict()"); ResetCells();