Fix style issues (hopefully).

This commit is contained in:
Ryan Curtin
2021-06-02 18:08:49 -04:00
parent 5853547f94
commit bfde132ca7
30 changed files with 145 additions and 126 deletions
+2 -1
View File
@@ -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;
+2 -2
View File
@@ -108,8 +108,8 @@ void CVBase<MLAlgorithm,
WeightsType>::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<typename MLAlgorithm,
+2 -2
View File
@@ -230,8 +230,8 @@ PARAM_FLAG("copy_all_inputs", "If specified, all input parameters will be deep"
" copied before the method is run. This is useful for debugging problems "
"where the input parameters are being modified by the algorithm, but can "
"slow down the code.", "");
PARAM_FLAG("check_input_matrices", "If specified, the input matrix is checked for"
" NaN and inf values; an exception is thrown if any are found.", "");
PARAM_FLAG("check_input_matrices", "If specified, the input matrix is checked "
"for NaN and inf values; an exception is thrown if any are found.", "");
// Nothing else needs to be defined---the binding will use mlpackMain() as-is.
+2 -2
View File
@@ -37,8 +37,8 @@ inline void CheckSameSizes(const DataType& data,
{
std::ostringstream oss;
oss << callerDescription << ": number of points (" << data.n_cols << ") "
<< "does not match number of " << addInfo << " (" << label.n_elem << ")!"
<< std::endl;
<< "does not match number of " << addInfo << " (" << label.n_elem
<< ")!" << std::endl;
throw std::invalid_argument(oss.str());
}
}
@@ -40,7 +40,7 @@ class SimpleResidueTermination
* @param maxIterations Maximum number of iterations.
*/
SimpleResidueTermination(const double minResidue = 1e-5,
const size_t maxIterations = 10000) :
const size_t maxIterations = 10000) :
minResidue(minResidue),
maxIterations(maxIterations),
residue(0.0),
@@ -2,13 +2,15 @@
* @file methods/ann/activation_functions/silu_function.hpp
* @author Fawwaz Mayda
*
* Definition and implementation of the Sigmoid Weighted Linear Unit function (SILU).
* Definition and implementation of the Sigmoid Weighted Linear Unit function
* (SILU).
*
* For more information see the following paper
*
* @code
* @misc{elfwing2017sigmoidweighted ,
* title = {Sigmoid-Weighted Linear Units for Neural Network Function Approximation in Reinforcement Learning},
* title = {Sigmoid-Weighted Linear Units for Neural Network Function
* Approximation in Reinforcement Learning},
* author = {Stefan Elfwing and Eiji Uchibe and Kenji Doya},
* year = {2017},
* url = {https://arxiv.org/pdf/1702.03118.pdf},
@@ -38,7 +40,7 @@ namespace ann /* Artificial Neural Network */ {
* f'(x) &=& \frac{1}{1 + e^{-x}} * (1 + x * (1-\frac{1}{1 + e^{-x}}))\\
* @f}
*/
class SILUFunction
class SILUFunction
{
public:
/**
@@ -47,11 +49,11 @@ class SILUFunction
* @param x Input data.
* @return f(x).
*/
static double Fn(const double x)
static double Fn(const double x)
{
return x / (1.0 + std::exp(-x));
}
/**
* Computes the SILU function.
*
@@ -59,9 +61,9 @@ class SILUFunction
* @param y The resulting output activation.
*/
template<typename InputVecType, typename OutputVecType>
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<typename InputVecType, typename OutputVecType>
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
#endif
@@ -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:
/**
+8 -10
View File
@@ -111,8 +111,8 @@ double FFN<OutputLayerType, InitializationRuleType, CustomLayers...>::Train(
OptimizerType& optimizer,
CallbackTypes&&... callbacks)
{
CheckInputShape<std::vector<LayerTypes<CustomLayers...> > >(network,
predictors.n_rows,
CheckInputShape<std::vector<LayerTypes<CustomLayers...> > >(network,
predictors.n_rows,
"FFN<>::Train()");
ResetData(std::move(predictors), std::move(responses));
@@ -137,8 +137,8 @@ double FFN<OutputLayerType, InitializationRuleType, CustomLayers...>::Train(
arma::mat responses,
CallbackTypes&&... callbacks)
{
CheckInputShape<std::vector<LayerTypes<CustomLayers...> > >(network,
predictors.n_rows,
CheckInputShape<std::vector<LayerTypes<CustomLayers...> > >(network,
predictors.n_rows,
"FFN<>::Train()");
ResetData(std::move(predictors), std::move(responses));
@@ -227,9 +227,8 @@ template<typename OutputLayerType, typename InitializationRuleType,
void FFN<OutputLayerType, InitializationRuleType, CustomLayers...>::Predict(
arma::mat predictors, arma::mat& results)
{
CheckInputShape<std::vector<LayerTypes<CustomLayers...> > >(network,
predictors.n_rows,
"FFN<>::Predict()");
CheckInputShape<std::vector<LayerTypes<CustomLayers...> > >(
network, predictors.n_rows, "FFN<>::Predict()");
if (parameter.is_empty())
ResetParameters();
@@ -264,9 +263,8 @@ template<typename PredictorsType, typename ResponsesType>
double FFN<OutputLayerType, InitializationRuleType, CustomLayers...>::Evaluate(
const PredictorsType& predictors, const ResponsesType& responses)
{
CheckInputShape<std::vector<LayerTypes<CustomLayers...> > >(network,
predictors.n_rows,
"FFN<>::Evaluate()");
CheckInputShape<std::vector<LayerTypes<CustomLayers...> > >(
network, predictors.n_rows, "FFN<>::Evaluate()");
if (parameter.is_empty())
ResetParameters();
@@ -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.
+2 -2
View File
@@ -29,7 +29,7 @@
#include <mlpack/methods/ann/activation_functions/gaussian_function.hpp>
#include <mlpack/methods/ann/activation_functions/hard_swish_function.hpp>
#include <mlpack/methods/ann/activation_functions/tanh_exponential_function.hpp>
#include <mlpack/methods/ann/activation_functions/silu_function.hpp>
#include <mlpack/methods/ann/activation_functions/silu_function.hpp>
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
@@ -27,7 +27,7 @@ Concatenate<InputDataType, OutputDataType>::Concatenate() :
}
template<typename InputDataType, typename OutputDataType>
Concatenate<InputDataType, OutputDataType>::Concatenate(const Concatenate& layer) :
Concatenate<InputDataType, OutputDataType>::Concatenate(const Concatenate& layer) :
inRows(layer.inRows),
weights(layer.weights),
delta(layer.delta),
@@ -37,7 +37,7 @@ Concatenate<InputDataType, OutputDataType>::Concatenate(const Concatenate& layer
}
template<typename InputDataType, typename OutputDataType>
Concatenate<InputDataType, OutputDataType>::Concatenate(Concatenate&& layer) :
Concatenate<InputDataType, OutputDataType>::Concatenate(Concatenate&& layer) :
inRows(layer.inRows),
weights(std::move(layer.weights)),
delta(std::move(layer.delta)),
@@ -51,7 +51,7 @@ Concatenate<InputDataType, OutputDataType>&
Concatenate<InputDataType, OutputDataType>::
operator=(const Concatenate& layer)
{
if (this != &layer)
if (this != &layer)
{
inRows = layer.inRows;
weights = layer.weights;
@@ -67,7 +67,7 @@ Concatenate<InputDataType, OutputDataType>&
Concatenate<InputDataType, OutputDataType>::
operator=(Concatenate&& layer)
{
if (this != &layer)
if (this != &layer)
{
inRows = layer.inRows;
weights = std::move(layer.weights);
@@ -48,9 +48,9 @@ void FlattenTSwish<InputDataType, OutputDataType>::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<InputDataType, OutputDataType>::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<InputDataType, OutputDataType>::serialize(
} // namespace ann
} // namespace mlpack
#endif
#endif
+1 -1
View File
@@ -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;
}
-1
View File
@@ -126,7 +126,6 @@ class ISRLU
//! ISRLU Hyperparameter (alpha > 0).
double alpha;
}; // class ISRLU
} // namespace ann
+1 -1
View File
@@ -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;
+6 -4
View File
@@ -196,11 +196,12 @@ class LpPooling
const arma::Mat<eT>& error,
arma::Mat<eT>& output)
{
arma::Mat<eT> 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);
+4 -1
View File
@@ -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
+6 -6
View File
@@ -26,7 +26,7 @@ LSTM<InputDataType, OutputDataType>::LSTM()
template<typename InputDataType, typename OutputDataType>
LSTM<InputDataType, OutputDataType>::LSTM(
const LSTM& layer) :
const LSTM& layer) :
inSize(layer.inSize),
outSize(layer.outSize),
rho(layer.rho),
@@ -45,7 +45,7 @@ LSTM<InputDataType, OutputDataType>::LSTM(
template<typename InputDataType, typename OutputDataType>
LSTM<InputDataType, OutputDataType>::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<InputDataType, OutputDataType>::LSTM(
}
template <typename InputDataType, typename OutputDataType>
LSTM<InputDataType, OutputDataType>&
LSTM<InputDataType, OutputDataType>&
LSTM<InputDataType, OutputDataType> :: operator=(const LSTM& layer)
{
if (this != &layer)
@@ -82,11 +82,11 @@ LSTM<InputDataType, OutputDataType> :: operator=(const LSTM& layer)
rhoSize = layer.rho;
bpttSteps = layer.bpttSteps;
}
return *this;
return *this;
}
template <typename InputDataType, typename OutputDataType>
LSTM<InputDataType, OutputDataType>&
LSTM<InputDataType, OutputDataType>&
LSTM<InputDataType, OutputDataType> :: operator=(LSTM&& layer)
{
if (this != &layer)
@@ -105,7 +105,7 @@ LSTM<InputDataType, OutputDataType> :: operator=(LSTM&& layer)
rhoSize = std::move(layer.rho);
bpttSteps = std::move(layer.bpttSteps);
}
return *this;
return *this;
}
template <typename InputDataType, typename OutputDataType>
@@ -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<eT>& error,
arma::Mat<eT>& output)
{
arma::Mat<eT> 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<eT>(InputArea.n_rows, InputArea.n_cols);
unpooledError.fill(error(rowidx, colidx) / InputArea.n_elem);
@@ -77,12 +77,11 @@ void PixelShuffle<InputDataType, OutputDataType>::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<InputDataType, OutputDataType>::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);
}
}
}
}
}
+16 -13
View File
@@ -128,9 +128,12 @@ Recurrent<InputDataType, OutputDataType, CustomLayers...>::Recurrent(
template<typename InputDataType, typename OutputDataType,
typename... CustomLayers>
size_t Recurrent<InputDataType, OutputDataType, CustomLayers...>::InputShape() const
size_t
Recurrent<InputDataType, OutputDataType, CustomLayers...>::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<InputDataType, OutputDataType, CustomLayers...>::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;
}
}
}
}
@@ -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);
@@ -46,7 +46,7 @@ Reparametrization<InputDataType, OutputDataType>::Reparametrization(
<< "included." << std::endl;
}
}
template <typename InputDataType, typename OutputDataType>
Reparametrization<InputDataType, OutputDataType>::Reparametrization(
const Reparametrization& layer) :
@@ -55,7 +55,7 @@ Reparametrization<InputDataType, OutputDataType>::Reparametrization(
includeKl(layer.includeKl),
beta(layer.beta)
{
// Nothing to do here.
// Nothing to do here.
}
template <typename InputDataType, typename OutputDataType>
@@ -66,13 +66,13 @@ Reparametrization<InputDataType, OutputDataType>::Reparametrization(
includeKl(std::move(layer.includeKl)),
beta(std::move(layer.beta))
{
// Nothing to do here.
// Nothing to do here.
}
template <typename InputDataType, typename OutputDataType>
Reparametrization<InputDataType, OutputDataType>&
Reparametrization<InputDataType, OutputDataType>::
operator=(const Reparametrization& layer)
operator=(const Reparametrization& layer)
{
if (this != &layer)
{
@@ -83,11 +83,11 @@ operator=(const Reparametrization& layer)
}
return *this;
}
template <typename InputDataType, typename OutputDataType>
Reparametrization<InputDataType, OutputDataType>&
Reparametrization<InputDataType, OutputDataType>::
operator=(Reparametrization&& layer)
operator=(Reparametrization&& layer)
{
if (this != &layer)
{
@@ -98,8 +98,8 @@ operator=(Reparametrization&& layer)
}
return *this;
}
template<typename InputDataType, typename OutputDataType>
template<typename eT>
void Reparametrization<InputDataType, OutputDataType>::Forward(
@@ -34,7 +34,7 @@ BCELoss<InputDataType, OutputDataType>::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;
@@ -31,16 +31,17 @@ HuberLoss<InputDataType, OutputDataType>::HuberLoss(
template<typename InputDataType, typename OutputDataType>
template<typename PredictionType, typename TargetType>
typename PredictionType::elem_type
HuberLoss<InputDataType, OutputDataType>::Forward(const PredictionType& prediction,
const TargetType& target)
HuberLoss<InputDataType, OutputDataType>::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<InputDataType, OutputDataType>::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;
}
@@ -29,8 +29,9 @@ KLDivergence<InputDataType, OutputDataType>::KLDivergence(const bool takeMean) :
template<typename InputDataType, typename OutputDataType>
template<typename PredictionType, typename TargetType>
typename PredictionType::elem_type
KLDivergence<InputDataType, OutputDataType>::Forward(const PredictionType& prediction,
const TargetType& target)
KLDivergence<InputDataType, OutputDataType>::Forward(
const PredictionType& prediction,
const TargetType& target)
{
if (takeMean)
{
@@ -52,7 +53,8 @@ void KLDivergence<InputDataType, OutputDataType>::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
{
@@ -65,8 +65,10 @@ class SigmoidCrossEntropyError
* @param target The target vector.
*/
template<typename PredictionType, typename TargetType>
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.
*
@@ -108,4 +108,4 @@ class TripletMarginLoss
// include implementation.
#include "triplet_margin_loss_impl.hpp"
#endif
#endif
@@ -33,8 +33,10 @@ TripletMarginLoss<InputDataType, OutputDataType>::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<InputDataType, OutputDataType>::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;
}
+6 -9
View File
@@ -149,9 +149,8 @@ double RNN<OutputLayerType, InitializationRuleType, CustomLayers...>::Train(
OptimizerType& optimizer,
CallbackTypes&&... callbacks)
{
CheckInputShape<std::vector<LayerTypes<CustomLayers...> > >(network,
predictors.n_rows,
"RNN<>::Train()");
CheckInputShape<std::vector<LayerTypes<CustomLayers...> > >(
network, predictors.n_rows, "RNN<>::Train()");
numFunctions = responses.n_cols;
@@ -197,9 +196,8 @@ double RNN<OutputLayerType, InitializationRuleType, CustomLayers...>::Train(
arma::cube responses,
CallbackTypes&&... callbacks)
{
CheckInputShape<std::vector<LayerTypes<CustomLayers...> > >(network,
predictors.n_rows,
"RNN<>::Train()");
CheckInputShape<std::vector<LayerTypes<CustomLayers...> > >(
network, predictors.n_rows, "RNN<>::Train()");
numFunctions = responses.n_cols;
@@ -233,9 +231,8 @@ template<typename OutputLayerType, typename InitializationRuleType,
void RNN<OutputLayerType, InitializationRuleType, CustomLayers...>::Predict(
arma::cube predictors, arma::cube& results, const size_t batchSize)
{
CheckInputShape<std::vector<LayerTypes<CustomLayers...> > >(network,
predictors.n_rows,
"RNN<>::Predict()");
CheckInputShape<std::vector<LayerTypes<CustomLayers...> > >(
network, predictors.n_rows, "RNN<>::Predict()");
ResetCells();