reverted 9 previous commits

This commit is contained in:
nishantkr18
2020-06-03 17:23:53 +05:30
parent 1cb9290014
commit 1dddedfbe2
75 changed files with 141 additions and 1097 deletions
-2
View File
@@ -27,8 +27,6 @@
* Fix `no_intercept` and probability computation for linear SVM bindings
(#2419).
* Add copy constructor in all layers of ANN (#2325).
### mlpack 3.3.1
###### 2020-04-29
* Minor Julia and Python documentation fixes (#2373).
+1 -9
View File
@@ -79,14 +79,6 @@ class BRNN
MergeOutputType* mergeOutput = new MergeOutputType(),
InitializationRuleType initializeRule = InitializationRuleType());
/**
* Copy the BRNN object.
*
* Warning: Copying BRNN is a memory-intensive task: two RNNs need to
* be copied.
*/
BRNN(const BRNN&);
~BRNN();
/**
@@ -401,7 +393,7 @@ class BRNN
//! Locally-stored delete visitor.
DeleteVisitor deleteVisitor;
//! Locally-stored copy visitor.
//! Locally-stored delete visitor.
CopyVisitor<CustomLayers...> copyVisitor;
//! The current evaluation mode (training or testing).
-51
View File
@@ -60,57 +60,6 @@ BRNN<OutputLayerType, MergeLayerType, MergeOutputType,
/* Nothing to do here. */
}
template<typename OutputLayerType, typename MergeLayerType,
typename MergeOutputType, typename InitializationRuleType,
typename... CustomLayers>
BRNN<OutputLayerType, MergeLayerType, MergeOutputType,
InitializationRuleType, CustomLayers...>::BRNN(
const BRNN& network) :
rho(network.rho),
outputLayer(network.outputLayer),
initializeRule(network.initializeRule),
inputSize(network.inputSize),
outputSize(network.outputSize),
targetSize(network.targetSize),
reset(network.reset),
single(network.single),
numFunctions(network.numFunctions),
deterministic(network.deterministic),
parameter(network.parameter),
predictors(network.predictors),
responses(network.responses),
forwardRNN(network.rho, network.single, network.outputLayer,
network.initializeRule),
backwardRNN(network.rho, network.single, network.outputLayer,
network.initializeRule)
{
mergeLayer = boost::apply_visitor(copyVisitor, network.mergeLayer);
mergeOutput = boost::apply_visitor(copyVisitor, network.mergeOutput);
// Build new layers according to source network.
for (size_t i = 0; i < network.forwardRNN.network.size(); ++i)
{
this->forwardRNN.network.push_back(boost::apply_visitor(copyVisitor,
network.forwardRNN.network[i]));
}
// Build new layers according to source network.
for (size_t i = 0; i < network.backwardRNN.network.size(); ++i)
{
this->backwardRNN.network.push_back(boost::apply_visitor(copyVisitor,
network.backwardRNN.network[i]));
}
boost::apply_visitor(AddVisitor<CustomLayers...>(
forwardRNN.network.back()), mergeLayer);
boost::apply_visitor(AddVisitor<CustomLayers...>(
backwardRNN.network.back()), mergeLayer);
boost::apply_visitor(RunSetVisitor(false), mergeLayer);
forwardRNN.Parameters() = network.forwardRNN.Parameters();
backwardRNN.Parameters() = network.backwardRNN.Parameters();
}
template<typename OutputLayerType, typename MergeLayerType,
typename MergeOutputType, typename InitializationRuleType,
typename... CustomLayers>
+1 -6
View File
@@ -71,12 +71,7 @@ class FFN
FFN(OutputLayerType outputLayer = OutputLayerType(),
InitializationRuleType initializeRule = InitializationRuleType());
/**
* Copy the FFN object.
*
* Warning: Copying FFN is a memory-intensive task: multiple layers as well
* as parameters needed to be copied.
*/
//! Copy constructor.
FFN(const FFN&);
//! Move constructor.
+1 -6
View File
@@ -92,12 +92,7 @@ class GAN
const double clippingParameter = 0.01,
const double lambda = 10.0);
/**
* Copy the GAN object.
*
* Warning: Copying a GAN is a memory-intensive task: the Generator and Discriminator
* networks will be copied.
*/
//! Copy constructor.
GAN(const GAN&);
//! Move constructor.
@@ -17,7 +17,6 @@
#include "../visitor/delete_visitor.hpp"
#include "../visitor/delta_visitor.hpp"
#include "../visitor/copy_visitor.hpp"
#include "../visitor/output_parameter_visitor.hpp"
#include "layer_types.hpp"
@@ -60,9 +59,6 @@ class AddMerge
*/
AddMerge(const bool model, const bool run, const bool ownsLayers);
//! Copy constructor.
AddMerge(const AddMerge&);
//! Destructor to release allocated memory.
~AddMerge();
@@ -41,17 +41,6 @@ AddMerge<InputDataType, OutputDataType, CustomLayers...>::AddMerge(
// Nothing to do here.
}
template<typename InputDataType, typename OutputDataType,
typename... CustomLayers>
AddMerge<InputDataType, OutputDataType, CustomLayers...>::AddMerge(
const AddMerge& layer) :
model(layer.model),
run(layer.run),
ownsLayers(layer.ownsLayers)
{
// Nothing to do here.
}
template<typename InputDataType, typename OutputDataType,
typename... CustomLayers>
AddMerge<InputDataType, OutputDataType, CustomLayers...>::~AddMerge()
@@ -125,9 +125,6 @@ class AtrousConvolution
const size_t dilationHeight = 1,
const std::string& paddingType = "None");
//! Copy constructor.
AtrousConvolution(const AtrousConvolution&);
/*
* Set the weight and bias term.
*/
@@ -37,39 +37,6 @@ AtrousConvolution<
// Nothing to do here.
}
template<
typename ForwardConvolutionRule,
typename BackwardConvolutionRule,
typename GradientConvolutionRule,
typename InputDataType,
typename OutputDataType
>
AtrousConvolution<
ForwardConvolutionRule,
BackwardConvolutionRule,
GradientConvolutionRule,
InputDataType,
OutputDataType
>::AtrousConvolution(
const AtrousConvolution& layer) :
inSize(layer.inSize),
outSize(layer.outSize),
kernelWidth(layer.kernelWidth),
kernelHeight(layer.kernelHeight),
weights(layer.weights),
strideWidth(layer.strideWidth),
strideHeight(layer.strideHeight),
inputWidth(layer.inputWidth),
inputHeight(layer.inputHeight),
outputWidth(layer.outputWidth),
outputHeight(layer.outputHeight),
dilationWidth(layer.dilationWidth),
dilationHeight(layer.dilationHeight),
padding(layer.padding)
{
Reset();
}
template<
typename ForwardConvolutionRule,
typename BackwardConvolutionRule,
@@ -67,9 +67,6 @@ class BatchNorm
*/
BatchNorm(const size_t size, const double eps = 1e-8);
//! Copy constructor.
BatchNorm(const BatchNorm&);
/**
* Reset the layer parameters
*/
@@ -30,22 +30,6 @@ BatchNorm<InputDataType, OutputDataType>::BatchNorm() :
{
// Nothing to do here.
}
template<typename InputDataType, typename OutputDataType>
BatchNorm<InputDataType, OutputDataType>::BatchNorm(
const BatchNorm& layer) :
size(layer.size),
eps(layer.eps),
gamma(layer.gamma),
beta(layer.beta),
weights(layer.weights),
count(layer.count),
runningMean(layer.runningMean),
runningVariance(layer.runningVariance)
{
Reset();
}
template <typename InputDataType, typename OutputDataType>
BatchNorm<InputDataType, OutputDataType>::BatchNorm(
const size_t size, const double eps) :
@@ -57,9 +57,6 @@ class BilinearInterpolation
const size_t outColSize,
const size_t depth);
//! Copy constructor.
BilinearInterpolation(const BilinearInterpolation&);
/**
* Forward pass through the layer. The layer interpolates
* the matrix using the given Bilinear Interpolation method.
@@ -51,19 +51,6 @@ BilinearInterpolation(
// Nothing to do here.
}
template<typename InputDataType, typename OutputDataType>
BilinearInterpolation<InputDataType, OutputDataType>::
BilinearInterpolation(const BilinearInterpolation& layer):
inRowSize(layer.inRowSize),
inColSize(layer.inColSize),
outRowSize(layer.outRowSize),
outColSize(layer.outColSize),
depth(layer.depth),
batchSize(layer.batchSize)
{
// Nothing to do here.
}
template<typename InputDataType, typename OutputDataType>
template<typename eT>
void BilinearInterpolation<InputDataType, OutputDataType>::Forward(
-3
View File
@@ -67,9 +67,6 @@ class Concat
const bool model = false,
const bool run = true);
//! Copy constructor.
Concat(const Concat&);
/**
* Destroy the layers held by the model.
*/
@@ -36,53 +36,6 @@ Concat<InputDataType, OutputDataType, CustomLayers...>::Concat(
parameters.set_size(0, 0);
}
template<typename InputDataType, typename OutputDataType,
typename... CustomLayers>
Concat<InputDataType, OutputDataType, CustomLayers...>::Concat(
const Concat& layer) :
inputSize(layer.inputSize),
axis(layer.axis),
useAxis(layer.useAxis),
model(layer.model),
run(layer.run)
{
parameters.set_size(0, 0);
// Parameters to help calculate the number of channels.
size_t oldColSize = 1, newColSize = 1;
// Axis is specified and useAxis is true.
if (useAxis)
{
// Axis is specified without input dimension.
// Throw an error.
if (inputSize.n_elem > 0)
{
// Calculate rowSize, newColSize based on the axis
// of concatenation. Finally concat along cols and
// reshape to original format i.e. (input, batch_size).
size_t i = std::min(axis + 1, (size_t) inputSize.n_elem);
for (; i < inputSize.n_elem; ++i)
{
newColSize *= inputSize[i];
}
}
else
{
throw std::logic_error("Input dimensions not specified.");
}
}
else
{
channels = 1;
}
if (newColSize <= 0)
{
throw std::logic_error("Col size is zero.");
}
channels = newColSize / oldColSize;
inputSize.clear();
}
template<typename InputDataType, typename OutputDataType,
typename... CustomLayers>
Concat<InputDataType, OutputDataType, CustomLayers...>::Concat(
@@ -48,9 +48,6 @@ class ConcatPerformance
ConcatPerformance(const size_t inSize = 0,
OutputLayerType&& outputLayer = OutputLayerType());
//! Copy constructor.
ConcatPerformance(const ConcatPerformance&);
/*
* Computes the Negative log likelihood.
*
@@ -34,22 +34,6 @@ ConcatPerformance<
// Nothing to do here.
}
template<
typename OutputLayerType,
typename InputDataType,
typename OutputDataType
>
ConcatPerformance<
OutputLayerType,
InputDataType,
OutputDataType
>::ConcatPerformance(const ConcatPerformance& layer) :
inSize(layer.inSize),
outputLayer(layer.outputLayer)
{
// Nothing to do here.
}
template<
typename OutputLayerType,
typename InputDataType,
@@ -41,9 +41,6 @@ class Concatenate
*/
Concatenate();
//! Copy constructor.
Concatenate(const Concatenate&);
/**
* Ordinary feed forward pass of a neural network, evaluating the function
* f(x) by propagating the activity forward through f.
@@ -25,15 +25,6 @@ Concatenate<InputDataType, OutputDataType>::Concatenate()
// Nothing to do here.
}
template<typename InputDataType, typename OutputDataType>
Concatenate<InputDataType, OutputDataType>::Concatenate(
const Concatenate& layer) :
inRows(layer.inRows),
concat(layer.concat)
{
// Nothing to do here.
}
template<typename InputDataType, typename OutputDataType>
template<typename eT>
void Concatenate<InputDataType, OutputDataType>::Forward(
@@ -43,9 +43,6 @@ class Constant
*/
Constant(const size_t outSize = 0, const double scalar = 0.0);
//! Copy constructor.
Constant(const Constant&);
/**
* Ordinary feed forward pass of a neural network. The forward pass fills the
* output with the specified constant parameter.
@@ -30,16 +30,6 @@ Constant<InputDataType, OutputDataType>::Constant(
constantOutput.fill(scalar);
}
template<typename InputDataType, typename OutputDataType>
Constant<InputDataType, OutputDataType>::Constant(
const Constant& layer) :
inSize(layer.inSize),
outSize(layer.outSize),
constantOutput(layer.constantOutput)
{
// Nothing to do here.
}
template<typename InputDataType, typename OutputDataType>
template<typename InputType, typename OutputType>
void Constant<InputDataType, OutputDataType>::Forward(
@@ -111,9 +111,6 @@ class Convolution
const size_t inputHeight = 0,
const std::string& paddingType = "None");
//! Copy constructor.
Convolution(const Convolution&);
/*
* Set the weight and bias term.
*/
@@ -139,41 +139,6 @@ Convolution<
padding = ann::Padding<>(padWLeft, padWRight, padHTop, padHBottom);
}
template<
typename ForwardConvolutionRule,
typename BackwardConvolutionRule,
typename GradientConvolutionRule,
typename InputDataType,
typename OutputDataType
>
Convolution<
ForwardConvolutionRule,
BackwardConvolutionRule,
GradientConvolutionRule,
InputDataType,
OutputDataType
>::Convolution(
const Convolution& layer) :
inSize(layer.inSize),
outSize(layer.outSize),
kernelWidth(layer.kernelWidth),
kernelHeight(layer.kernelHeight),
strideWidth(layer.strideWidth),
strideHeight(layer.strideHeight),
padWLeft(layer.padWLeft),
padWRight(layer.padWRight),
padHBottom(layer.padHBottom),
padHTop(layer.padHTop),
weights(layer.weights),
inputWidth(layer.inputWidth),
inputHeight(layer.inputHeight),
outputWidth(layer.outputWidth),
outputHeight(layer.outputHeight),
padding(layer.padding)
{
Reset();
}
template<
typename ForwardConvolutionRule,
typename BackwardConvolutionRule,
+1 -6
View File
@@ -14,7 +14,6 @@
#ifndef MLPACK_METHODS_ANN_LAYER_DROPCONNECT_HPP
#define MLPACK_METHODS_ANN_LAYER_DROPCONNECT_HPP
#include "../visitor/copy_visitor.hpp"
#include <mlpack/prereqs.hpp>
#include "layer_types.hpp"
@@ -59,8 +58,7 @@ namespace ann /** Artificial Neural Network. */ {
*/
template<
typename InputDataType = arma::mat,
typename OutputDataType = arma::mat,
typename... CustomLayers
typename OutputDataType = arma::mat
>
class DropConnect
{
@@ -68,9 +66,6 @@ class DropConnect
//! Create the DropConnect object.
DropConnect();
//! Copy constructor.
DropConnect(const DropConnect&);
/**
* Creates the DropConnect Layer as a Linear Object that takes input size,
* output size and ratio as parameter.
@@ -18,7 +18,6 @@
#include "dropconnect.hpp"
#include "../visitor/delete_visitor.hpp"
#include "../visitor/copy_visitor.hpp"
#include "../visitor/forward_visitor.hpp"
#include "../visitor/backward_visitor.hpp"
#include "../visitor/gradient_visitor.hpp"
@@ -28,9 +27,8 @@
namespace mlpack {
namespace ann /** Artificial Neural Network. */ {
template<typename InputDataType, typename OutputDataType,
typename... CustomLayers>
DropConnect<InputDataType, OutputDataType, CustomLayers...>::DropConnect() :
template<typename InputDataType, typename OutputDataType>
DropConnect<InputDataType, OutputDataType>::DropConnect() :
ratio(0.5),
scale(2.0),
deterministic(true)
@@ -38,9 +36,8 @@ DropConnect<InputDataType, OutputDataType, CustomLayers...>::DropConnect() :
// Nothing to do here.
}
template<typename InputDataType, typename OutputDataType,
typename... CustomLayers>
DropConnect<InputDataType, OutputDataType, CustomLayers...>::DropConnect(
template<typename InputDataType, typename OutputDataType>
DropConnect<InputDataType, OutputDataType>::DropConnect(
const size_t inSize,
const size_t outSize,
const double ratio) :
@@ -51,24 +48,9 @@ DropConnect<InputDataType, OutputDataType, CustomLayers...>::DropConnect(
network.push_back(baseLayer);
}
template<typename InputDataType, typename OutputDataType,
typename... CustomLayers>
DropConnect<InputDataType, OutputDataType, CustomLayers...>::DropConnect(
const DropConnect& layer) :
ratio(layer.ratio),
scale(layer.scale),
deterministic(layer.deterministic)
{
CopyVisitor<CustomLayers...> copyVisitor;
baseLayer = boost::apply_visitor(copyVisitor, layer.baseLayer);
this->network.push_back(baseLayer);
}
template<typename InputDataType, typename OutputDataType,
typename... CustomLayers>
template<typename InputDataType, typename OutputDataType>
template<typename eT>
void DropConnect<InputDataType, OutputDataType, CustomLayers...>::Forward(
void DropConnect<InputDataType, OutputDataType>::Forward(
const arma::Mat<eT>& input,
arma::Mat<eT>& output)
{
@@ -97,10 +79,9 @@ void DropConnect<InputDataType, OutputDataType, CustomLayers...>::Forward(
}
}
template<typename InputDataType, typename OutputDataType,
typename... CustomLayers>
template<typename InputDataType, typename OutputDataType>
template<typename eT>
void DropConnect<InputDataType, OutputDataType, CustomLayers...>::Backward(
void DropConnect<InputDataType, OutputDataType>::Backward(
const arma::Mat<eT>& input,
const arma::Mat<eT>& gy,
arma::Mat<eT>& g)
@@ -108,10 +89,9 @@ void DropConnect<InputDataType, OutputDataType, CustomLayers...>::Backward(
boost::apply_visitor(BackwardVisitor(input, gy, g), baseLayer);
}
template<typename InputDataType, typename OutputDataType,
typename... CustomLayers>
template<typename InputDataType, typename OutputDataType>
template<typename eT>
void DropConnect<InputDataType, OutputDataType, CustomLayers...>::Gradient(
void DropConnect<InputDataType, OutputDataType>::Gradient(
const arma::Mat<eT>& input,
const arma::Mat<eT>& error,
arma::Mat<eT>& /* gradient */)
@@ -123,10 +103,9 @@ void DropConnect<InputDataType, OutputDataType, CustomLayers...>::Gradient(
boost::apply_visitor(ParametersSetVisitor(denoise), baseLayer);
}
template<typename InputDataType, typename OutputDataType,
typename... CustomLayers>
template<typename InputDataType, typename OutputDataType>
template<typename Archive>
void DropConnect<InputDataType, OutputDataType, CustomLayers...>::serialize(
void DropConnect<InputDataType, OutputDataType>::serialize(
Archive& ar,
const unsigned int /* version */)
{
-3
View File
@@ -60,9 +60,6 @@ class Dropout
*/
Dropout(const double ratio = 0.5);
//! Copy constructor.
Dropout(const Dropout&);
/**
* Ordinary feed forward pass of the dropout layer.
*
@@ -29,16 +29,6 @@ Dropout<InputDataType, OutputDataType>::Dropout(
// Nothing to do here.
}
template<typename InputDataType, typename OutputDataType>
Dropout<InputDataType, OutputDataType>::Dropout(
const Dropout& layer) :
ratio(layer.ratio),
scale(layer.scale),
deterministic(layer.deterministic)
{
// Nothing to do here.
}
template<typename InputDataType, typename OutputDataType>
template<typename eT>
void Dropout<InputDataType, OutputDataType>::Forward(
+1 -4
View File
@@ -73,9 +73,6 @@ class FastLSTM
//! Create the Fast LSTM object.
FastLSTM();
//! Copy constructor.
FastLSTM(const FastLSTM&);
/**
* Create the Fast LSTM layer object using the specified parameters.
*
@@ -290,7 +287,7 @@ class FastLSTM
//! Locally-stored cell activation error.
OutputDataType cellActivation;
//! Locally-stored forget gate error.
//! Locally-stored foget gate error.
OutputDataType forgetGateError;
//! Locally-stored previous error.
@@ -46,26 +46,6 @@ FastLSTM<InputDataType, OutputDataType>::FastLSTM(
4 * outSize * inSize + 4 * outSize + 4 * outSize * outSize, 1);
}
template <typename InputDataType, typename OutputDataType>
FastLSTM<InputDataType, OutputDataType>::FastLSTM(
const FastLSTM& layer) :
inSize(layer.inSize),
outSize(layer.outSize),
rho(layer.rho),
weights(layer.weights),
forwardStep(layer.forwardStep),
backwardStep(layer.backwardStep),
gradientStep(layer.gradientStep),
grad(layer.grad),
batchSize(layer.batchSize),
batchStep(layer.batchStep),
gradientStepIdx(layer.gradientStepIdx),
rhoSize(layer.rho),
bpttSteps(layer.bpttSteps)
{
Reset();
}
template<typename InputDataType, typename OutputDataType>
void FastLSTM<InputDataType, OutputDataType>::Reset()
{
@@ -71,9 +71,6 @@ class FlexibleReLU
*/
FlexibleReLU(const double alpha = 0);
//! Copy constructor.
FlexibleReLU(const FlexibleReLU&);
/**
* Reset the layer parameter.
*/
@@ -31,15 +31,6 @@ FlexibleReLU<InputDataType, OutputDataType>::FlexibleReLU(
this->alpha(0) = userAlpha;
}
template<typename InputDataType, typename OutputDataType>
FlexibleReLU<InputDataType, OutputDataType>::FlexibleReLU(
const FlexibleReLU& layer) :
userAlpha(layer.userAlpha)
{
this->alpha.set_size(1, 1);
this->alpha(0) = userAlpha;
}
template<typename InputDataType, typename OutputDataType>
void FlexibleReLU<InputDataType, OutputDataType>::Reset()
{
-3
View File
@@ -88,9 +88,6 @@ template <
class Glimpse
{
public:
//! Copy constructor.
Glimpse(const Glimpse&);
/**
* Create the GlimpseLayer object using the specified ratio and rescale
* parameter.
@@ -20,23 +20,6 @@
namespace mlpack {
namespace ann /** Artificial Neural Network. */ {
template <typename InputDataType, typename OutputDataType>
Glimpse<InputDataType, OutputDataType>::Glimpse(
const Glimpse& layer) :
inSize(layer.inSize),
size(layer.size),
depth(layer.depth),
scale(layer.scale),
inputWidth(layer.inputWidth),
inputHeight(layer.inputHeight),
outputWidth(layer.outputWidth),
outputHeight(layer.outputHeight),
inputDepth(layer.inputDepth),
deterministic(layer.deterministic)
{
// Nothing to do here.
}
template <typename InputDataType, typename OutputDataType>
Glimpse<InputDataType, OutputDataType>::Glimpse(
const size_t inSize,
+1 -5
View File
@@ -53,8 +53,7 @@ namespace ann /** Artificial Neural Network. */ {
*/
template <
typename InputDataType = arma::mat,
typename OutputDataType = arma::mat,
typename... CustomLayers
typename OutputDataType = arma::mat
>
class GRU
{
@@ -73,9 +72,6 @@ class GRU
const size_t outSize,
const size_t rho = std::numeric_limits<size_t>::max());
//! Copy constructor.
GRU(const GRU&);
/**
* Ordinary feed forward pass of a neural network, evaluating the function
* f(x) by propagating the activity forward through f.
+14 -68
View File
@@ -23,62 +23,14 @@
namespace mlpack {
namespace ann /** Artificial Neural Network. */ {
template<typename InputDataType, typename OutputDataType,
typename... CustomLayers>
GRU<InputDataType, OutputDataType, CustomLayers...>::GRU()
template<typename InputDataType, typename OutputDataType>
GRU<InputDataType, OutputDataType>::GRU()
{
// Nothing to do here.
}
template<typename InputDataType, typename OutputDataType,
typename... CustomLayers>
GRU<InputDataType, OutputDataType, CustomLayers...>::GRU(
const GRU& layer) :
inSize(layer.inSize),
outSize(layer.outSize),
rho(layer.rho),
batchSize(layer.batchSize),
forwardStep(layer.forwardStep),
backwardStep(layer.backwardStep),
gradientStep(layer.gradientStep),
outParameter(layer.outParameter),
allZeros(layer.allZeros),
prevOutput(layer.prevOutput),
backIterator(layer.backIterator),
gradIterator(layer.gradIterator),
prevError(layer.prevError),
deterministic(layer.deterministic)
{
CopyVisitor<CustomLayers...> copyVisitor;
// Input specific linear layers(for zt, rt, ot).
input2GateModule = boost::apply_visitor(copyVisitor,
layer.input2GateModule);
// Previous output gates (for zt and rt).
output2GateModule = boost::apply_visitor(copyVisitor,
layer.output2GateModule);
// Previous output gate for ot.
outputHidden2GateModule = boost::apply_visitor(copyVisitor,
layer.outputHidden2GateModule);
this->network.push_back(input2GateModule);
this->network.push_back(output2GateModule);
this->network.push_back(outputHidden2GateModule);
inputGateModule = new SigmoidLayer<>();
forgetGateModule = new SigmoidLayer<>();
hiddenStateModule = new TanHLayer<>();
this->network.push_back(inputGateModule);
this->network.push_back(hiddenStateModule);
this->network.push_back(forgetGateModule);
}
template<typename InputDataType, typename OutputDataType,
typename... CustomLayers>
GRU<InputDataType, OutputDataType, CustomLayers...>::GRU(
template <typename InputDataType, typename OutputDataType>
GRU<InputDataType, OutputDataType>::GRU(
const size_t inSize,
const size_t outSize,
const size_t rho) :
@@ -124,10 +76,9 @@ GRU<InputDataType, OutputDataType, CustomLayers...>::GRU(
gradIterator = outParameter.end();
}
template<typename InputDataType, typename OutputDataType,
typename... CustomLayers>
template<typename InputDataType, typename OutputDataType>
template<typename eT>
void GRU<InputDataType, OutputDataType, CustomLayers...>::Forward(
void GRU<InputDataType, OutputDataType>::Forward(
const arma::Mat<eT>& input, arma::Mat<eT>& output)
{
if (input.n_cols != batchSize)
@@ -240,10 +191,9 @@ void GRU<InputDataType, OutputDataType, CustomLayers...>::Forward(
}
}
template<typename InputDataType, typename OutputDataType,
typename... CustomLayers>
template<typename InputDataType, typename OutputDataType>
template<typename eT>
void GRU<InputDataType, OutputDataType, CustomLayers...>::Backward(
void GRU<InputDataType, OutputDataType>::Backward(
const arma::Mat<eT>& input, const arma::Mat<eT>& gy, arma::Mat<eT>& g)
{
if (input.n_cols != batchSize)
@@ -363,10 +313,9 @@ void GRU<InputDataType, OutputDataType, CustomLayers...>::Backward(
g = boost::apply_visitor(deltaVisitor, input2GateModule);
}
template<typename InputDataType, typename OutputDataType,
typename... CustomLayers>
template<typename InputDataType, typename OutputDataType>
template<typename eT>
void GRU<InputDataType, OutputDataType, CustomLayers...>::Gradient(
void GRU<InputDataType, OutputDataType>::Gradient(
const arma::Mat<eT>& input,
const arma::Mat<eT>& /* error */,
arma::Mat<eT>& /* gradient */)
@@ -413,10 +362,8 @@ void GRU<InputDataType, OutputDataType, CustomLayers...>::Gradient(
gradIterator--;
}
template<typename InputDataType, typename OutputDataType,
typename... CustomLayers>
void GRU<InputDataType, OutputDataType, CustomLayers...>::
ResetCell(const size_t /* size */)
template<typename InputDataType, typename OutputDataType>
void GRU<InputDataType, OutputDataType>::ResetCell(const size_t /* size */)
{
outParameter.clear();
outParameter.push_back(std::move(arma::mat(allZeros.memptr(),
@@ -430,10 +377,9 @@ ResetCell(const size_t /* size */)
backwardStep = 0;
}
template<typename InputDataType, typename OutputDataType,
typename... CustomLayers>
template<typename InputDataType, typename OutputDataType>
template<typename Archive>
void GRU<InputDataType, OutputDataType, CustomLayers...>::serialize(
void GRU<InputDataType, OutputDataType>::serialize(
Archive& ar, const unsigned int /* version */)
{
// If necessary, clean memory from the old model.
-6
View File
@@ -74,9 +74,6 @@ class Highway
//! Destroy the Highway object.
~Highway();
//! Copy constructor.
Highway(const Highway&);
/**
* Reset the layer parameter.
*/
@@ -258,9 +255,6 @@ class Highway
//! Locally-stored output height visitor.
OutputHeightVisitor outputHeightVisitor;
//! Locally-stored copy visitor
CopyVisitor<CustomLayers...> copyVisitor;
}; // class Highway
} // namespace ann
@@ -37,30 +37,6 @@ Highway<InputDataType, OutputDataType, CustomLayers...>::Highway() :
// Nothing to do here.
}
template<typename InputDataType, typename OutputDataType,
typename... CustomLayers>
Highway<InputDataType, OutputDataType, CustomLayers...>::Highway(
const Highway& layer) :
inSize(layer.inSize),
networkOwnerships(layer.networkOwnerships),
model(layer.model),
weights(layer.weights),
reset(layer.reset),
width(layer.width),
height(layer.height),
networkOutput(layer.networkOutput)
{
for (size_t i = 0; i < layer.network.size(); ++i)
{
if (layer.networkOwnerships[i])
{
this->network.push_back(boost::apply_visitor(copyVisitor,
layer.network[i]));
}
}
Reset();
}
template<
typename InputDataType, typename OutputDataType, typename... CustomLayers>
Highway<InputDataType, OutputDataType, CustomLayers...>::Highway(
@@ -76,9 +76,6 @@ class LayerNorm
*/
LayerNorm(const size_t size, const double eps = 1e-8);
//! Copy constructor.
LayerNorm(const LayerNorm&);
/**
* Reset the layer parameters.
*/
@@ -29,16 +29,6 @@ LayerNorm<InputDataType, OutputDataType>::LayerNorm() :
// Nothing to do here.
}
template<typename InputDataType, typename OutputDataType>
LayerNorm<InputDataType, OutputDataType>::LayerNorm(const LayerNorm& layer) :
size(layer.size),
eps(layer.eps),
loading(layer.loading),
weights(layer.weights)
{
Reset();
}
template <typename InputDataType, typename OutputDataType>
LayerNorm<InputDataType, OutputDataType>::LayerNorm(
const size_t size, const double eps) :
+15 -24
View File
@@ -65,9 +65,11 @@ namespace mlpack {
namespace ann {
template<typename InputDataType, typename OutputDataType> class BatchNorm;
template<typename InputDataType, typename OutputDataType> class DropConnect;
template<typename InputDataType, typename OutputDataType> class Glimpse;
template<typename InputDataType, typename OutputDataType> class LayerNorm;
template<typename InputDataType, typename OutputDataType> class LSTM;
template<typename InputDataType, typename OutputDataType> class GRU;
template<typename InputDataType, typename OutputDataType> class FastLSTM;
template<typename InputDataType, typename OutputDataType> class VRClassReward;
template<typename InputDataType, typename OutputDataType> class Concatenate;
@@ -149,18 +151,6 @@ template<
>
class Convolution;
template<typename InputDataType,
typename OutputDataType,
typename... CustomLayers
>
class DropConnect;
template<typename InputDataType,
typename OutputDataType,
typename... CustomLayers
>
class GRU;
template<
typename ForwardConvolutionRule,
typename BackwardConvolutionRule,
@@ -181,21 +171,10 @@ class AtrousConvolution;
template<
typename InputDataType,
typename OutputDataType,
typename... CustomLayers
typename OutputDataType
>
class RecurrentAttention;
template <typename InputDataType,
typename OutputDataType
>
class AdaptiveMaxPooling;
template <typename InputDataType,
typename OutputDataType
>
class AdaptiveMeanPooling;
template<typename InputDataType,
typename OutputDataType,
typename... CustomLayers
@@ -208,6 +187,16 @@ template <typename InputDataType,
>
class WeightNorm;
template <typename InputDataType,
typename OutputDataType
>
class AdaptiveMaxPooling;
template <typename InputDataType,
typename OutputDataType
>
class AdaptiveMeanPooling;
using MoreTypes = boost::variant<
Recurrent<arma::mat, arma::mat>*,
RecurrentAttention<arma::mat, arma::mat>*,
@@ -223,6 +212,8 @@ using MoreTypes = boost::variant<
template <typename... CustomLayers>
using LayerTypes = boost::variant<
AdaptiveMaxPooling<arma::mat, arma::mat>*,
AdaptiveMeanPooling<arma::mat, arma::mat>*,
Add<arma::mat, arma::mat>*,
AddMerge<arma::mat, arma::mat>*,
AlphaDropout<arma::mat, arma::mat>*,
-3
View File
@@ -52,9 +52,6 @@ class Linear
const size_t outSize,
RegularizerType regularizer = RegularizerType());
//! Copy constructor.
Linear(const Linear&);
/*
* Reset the layer parameter.
*/
@@ -28,18 +28,6 @@ Linear<InputDataType, OutputDataType, RegularizerType>::Linear() :
// Nothing to do here.
}
template<typename InputDataType, typename OutputDataType,
typename RegularizerType>
Linear<InputDataType, OutputDataType, RegularizerType>::Linear(
const Linear& layer) :
inSize(layer.inSize),
outSize(layer.outSize),
weights(layer.weights),
regularizer(layer.regularizer)
{
Reset();
}
template<typename InputDataType, typename OutputDataType,
typename RegularizerType>
Linear<InputDataType, OutputDataType, RegularizerType>::Linear(
@@ -51,9 +51,6 @@ class LinearNoBias
const size_t outSize,
RegularizerType regularizer = RegularizerType());
//! Copy constructor.
LinearNoBias(const LinearNoBias&);
/*
* Reset the layer parameter.
*/
@@ -28,18 +28,6 @@ LinearNoBias<InputDataType, OutputDataType, RegularizerType>::LinearNoBias() :
// Nothing to do here.
}
template<typename InputDataType, typename OutputDataType,
typename RegularizerType>
LinearNoBias<InputDataType, OutputDataType, RegularizerType>::LinearNoBias(
const LinearNoBias& layer) :
inSize(layer.inSize),
outSize(layer.outSize),
weights(layer.weights),
regularizer(layer.regularizer)
{
Reset();
}
template<typename InputDataType, typename OutputDataType,
typename RegularizerType>
LinearNoBias<InputDataType, OutputDataType, RegularizerType>::LinearNoBias(
-3
View File
@@ -44,9 +44,6 @@ class Lookup
*/
Lookup(const size_t inSize = 0, const size_t outSize = 0);
//! Copy constructor.
Lookup(const Lookup&);
/**
* Ordinary feed forward pass of a neural network, evaluating the function
* f(x) by propagating the activity forward through f.
@@ -29,17 +29,6 @@ Lookup<InputDataType, OutputDataType>::Lookup(
weights.set_size(outSize, inSize);
}
template <typename InputDataType, typename OutputDataType>
Lookup<InputDataType, OutputDataType>::Lookup(
const Lookup& layer) :
inSize(layer.inSize),
outSize(layer.outSize),
weights(layer.weights)
{
// Nothing to do here.
}
template<typename InputDataType, typename OutputDataType>
template<typename eT>
void Lookup<InputDataType, OutputDataType>::Forward(
-3
View File
@@ -65,9 +65,6 @@ class LSTM
//! Create the LSTM object.
LSTM();
//! Copy constructor.
LSTM(const LSTM&);
/**
* Create the LSTM layer object using the specified parameters.
*
@@ -24,24 +24,6 @@ LSTM<InputDataType, OutputDataType>::LSTM()
// Nothing to do here.
}
template <typename InputDataType, typename OutputDataType>
LSTM<InputDataType, OutputDataType>::LSTM(
const LSTM& layer) :
inSize(layer.inSize),
outSize(layer.outSize),
rho(layer.rho),
weights(layer.weights),
forwardStep(layer.forwardStep),
backwardStep(layer.backwardStep),
gradientStep(layer.gradientStep),
batchSize(layer.batchSize),
batchStep(layer.batchStep),
rhoSize(layer.rhoSize),
bpttSteps(layer.bpttSteps)
{
Reset();
}
template <typename InputDataType, typename OutputDataType>
LSTM<InputDataType, OutputDataType>::LSTM(
const size_t inSize, const size_t outSize, const size_t rho) :
@@ -70,9 +70,6 @@ class MaxPooling
const size_t strideHeight = 1,
const bool floor = true);
//! Copy constructor.
MaxPooling(const MaxPooling&);
/**
* Ordinary feed forward pass of a neural network, evaluating the function
* f(x) by propagating the activity forward through f.
@@ -51,28 +51,6 @@ MaxPooling<InputDataType, OutputDataType>::MaxPooling(
// Nothing to do here.
}
template<typename InputDataType, typename OutputDataType>
MaxPooling<InputDataType, OutputDataType>::MaxPooling(
const MaxPooling& layer) :
kernelWidth(layer.kernelWidth),
kernelHeight(layer.kernelHeight),
strideWidth(layer.strideWidth),
strideHeight(layer.strideHeight),
floor(layer.floor),
inSize(layer.inSize),
outSize(layer.outSize),
reset(layer.reset),
inputWidth(layer.inputWidth),
inputHeight(layer.inputHeight),
outputWidth(layer.outputWidth),
outputHeight(layer.outputHeight),
deterministic(layer.deterministic),
offset(layer.offset),
batchSize(layer.batchSize)
{
// Nothing to do here.
}
template<typename InputDataType, typename OutputDataType>
template<typename eT>
void MaxPooling<InputDataType, OutputDataType>::Forward(
@@ -51,9 +51,6 @@ class MeanPooling
const size_t strideHeight = 1,
const bool floor = true);
//! Copy constructor.
MeanPooling(const MeanPooling&);
/**
* Ordinary feed forward pass of a neural network, evaluating the function
* f(x) by propagating the activity forward through f.
@@ -51,28 +51,6 @@ MeanPooling<InputDataType, OutputDataType>::MeanPooling(
// Nothing to do here.
}
template<typename InputDataType, typename OutputDataType>
MeanPooling<InputDataType, OutputDataType>::MeanPooling(
const MeanPooling& layer) :
kernelWidth(layer.kernelWidth),
kernelHeight(layer.kernelHeight),
strideWidth(layer.strideWidth),
strideHeight(layer.strideHeight),
floor(layer.floor),
inSize(layer.inSize),
outSize(layer.outSize),
inputWidth(layer.inputWidth),
inputHeight(layer.inputHeight),
outputWidth(layer.outputWidth),
reset(layer.reset),
outputHeight(layer.outputHeight),
deterministic(layer.deterministic),
offset(layer.offset),
batchSize(layer.batchSize)
{
// Nothing to do here.
}
template<typename InputDataType, typename OutputDataType>
template<typename eT>
void MeanPooling<InputDataType, OutputDataType>::Forward(
@@ -56,9 +56,6 @@ class MiniBatchDiscrimination
//! Create the MiniBatchDiscrimination object.
MiniBatchDiscrimination();
//! Copy constructor.
MiniBatchDiscrimination(const MiniBatchDiscrimination&);
/**
* Create the MiniBatchDiscrimination layer object using the specified
* number of units.
@@ -43,19 +43,6 @@ MiniBatchDiscrimination<InputDataType, OutputDataType
weights.set_size(A * B * C, 1);
}
template <typename InputDataType, typename OutputDataType>
MiniBatchDiscrimination<InputDataType, OutputDataType
>::MiniBatchDiscrimination(
const MiniBatchDiscrimination& layer) :
A(layer.A),
B(layer.B),
C(layer.C),
batchSize(layer.batchSize),
weights(layer.weights)
{
Reset();
}
template<typename InputDataType, typename OutputDataType>
void MiniBatchDiscrimination<InputDataType, OutputDataType>::Reset()
{
@@ -39,9 +39,6 @@ class MultiplyConstant
*/
MultiplyConstant(const double scalar = 1.0);
//! Copy constructor.
MultiplyConstant(const MultiplyConstant&);
/**
* Ordinary feed forward pass of a neural network. Multiply the input with the
* specified constant scalar value.
@@ -26,14 +26,6 @@ MultiplyConstant<InputDataType, OutputDataType>::MultiplyConstant(
// Nothing to do here.
}
template<typename InputDataType, typename OutputDataType>
MultiplyConstant<InputDataType, OutputDataType>::MultiplyConstant(
const MultiplyConstant& layer) :
scalar(layer.scalar)
{
// Nothing to do here.
}
template<typename InputDataType, typename OutputDataType>
template<typename InputType, typename OutputType>
void MultiplyConstant<InputDataType, OutputDataType>::Forward(
@@ -50,9 +50,6 @@ class MultiplyMerge
*/
MultiplyMerge(const bool model = false, const bool run = true);
//! Copy constructor.
MultiplyMerge(const MultiplyMerge&);
//! Destructor to release allocated memory.
~MultiplyMerge();
@@ -32,17 +32,6 @@ MultiplyMerge<InputDataType, OutputDataType, CustomLayers...>::MultiplyMerge(
// Nothing to do here.
}
template<typename InputDataType, typename OutputDataType,
typename... CustomLayers>
MultiplyMerge<InputDataType, OutputDataType, CustomLayers...>::MultiplyMerge(
const MultiplyMerge& layer) :
model(layer.model),
run(layer.run),
ownsLayer(layer.ownsLayer)
{
// Nothing to do here.
}
template<typename InputDataType, typename OutputDataType,
typename... CustomLayers>
MultiplyMerge<InputDataType, OutputDataType, CustomLayers...>::~MultiplyMerge()
@@ -16,7 +16,6 @@
#include <boost/ptr_container/ptr_vector.hpp>
#include "../visitor/delta_visitor.hpp"
#include "../visitor/copy_visitor.hpp"
#include "../visitor/output_parameter_visitor.hpp"
#include "../visitor/reset_visitor.hpp"
#include "../visitor/weight_size_visitor.hpp"
@@ -52,8 +51,7 @@ namespace ann /** Artificial Neural Network. */ {
*/
template <
typename InputDataType = arma::mat,
typename OutputDataType = arma::mat,
typename... CustomLayers
typename OutputDataType = arma::mat
>
class RecurrentAttention
{
@@ -64,9 +62,6 @@ class RecurrentAttention
*/
RecurrentAttention();
//! Copy constructor.
RecurrentAttention(const RecurrentAttention&);
/**
* Create the RecurrentAttention object using the specified modules.
*
@@ -214,9 +209,6 @@ class RecurrentAttention
//! Locally-stored weight size visitor.
WeightSizeVisitor weightSizeVisitor;
//! Locally-stored copy visitor
CopyVisitor<CustomLayers...> copyVisitor;
//! Locally-stored delta visitor.
DeltaVisitor deltaVisitor;
@@ -26,10 +26,8 @@
namespace mlpack {
namespace ann /** Artificial Neural Network. */ {
template<typename InputDataType, typename OutputDataType,
typename... CustomLayers>
RecurrentAttention<InputDataType, OutputDataType, CustomLayers...>::
RecurrentAttention() :
template<typename InputDataType, typename OutputDataType>
RecurrentAttention<InputDataType, OutputDataType>::RecurrentAttention() :
rho(0),
forwardStep(0),
backwardStep(0),
@@ -38,29 +36,9 @@ RecurrentAttention() :
// Nothing to do.
}
template<typename InputDataType, typename OutputDataType,
typename... CustomLayers>
RecurrentAttention<InputDataType, OutputDataType, CustomLayers...>::
RecurrentAttention(
const RecurrentAttention& layer) :
outSize(layer.outSize),
rho(layer.rho),
forwardStep(layer.forwardStep),
backwardStep(layer.backwardStep),
deterministic(layer.deterministic)
{
rnnModule = boost::apply_visitor(copyVisitor, layer.rnnModule);
actionModule = boost::apply_visitor(copyVisitor, layer.actionModule);
this->network.push_back(rnnModule);
this->network.push_back(actionModule);
}
template<typename InputDataType, typename OutputDataType,
typename... CustomLayers>
template <typename InputDataType, typename OutputDataType>
template<typename RNNModuleType, typename ActionModuleType>
RecurrentAttention<InputDataType, OutputDataType, CustomLayers...>::
RecurrentAttention(
RecurrentAttention<InputDataType, OutputDataType>::RecurrentAttention(
const size_t outSize,
const RNNModuleType& rnn,
const ActionModuleType& action,
@@ -77,11 +55,9 @@ RecurrentAttention(
network.push_back(actionModule);
}
template<typename InputDataType, typename OutputDataType,
typename... CustomLayers>
template<typename InputDataType, typename OutputDataType>
template<typename eT>
void RecurrentAttention<InputDataType, OutputDataType, CustomLayers...>::
Forward(
void RecurrentAttention<InputDataType, OutputDataType>::Forward(
const arma::Mat<eT>& input, arma::Mat<eT>& output)
{
// Initialize the action input.
@@ -134,11 +110,9 @@ Forward(
backwardStep = 0;
}
template<typename InputDataType, typename OutputDataType,
typename... CustomLayers>
template<typename InputDataType, typename OutputDataType>
template<typename eT>
void RecurrentAttention<InputDataType, OutputDataType, CustomLayers...>::
Backward(
void RecurrentAttention<InputDataType, OutputDataType>::Backward(
const arma::Mat<eT>& /* input */,
const arma::Mat<eT>& gy,
arma::Mat<eT>& g)
@@ -216,11 +190,10 @@ Backward(
IntermediateGradient();
}
}
template<typename InputDataType, typename OutputDataType,
typename... CustomLayers>
template<typename InputDataType, typename OutputDataType>
template<typename eT>
void RecurrentAttention<InputDataType, OutputDataType, CustomLayers...>::
Gradient(
void RecurrentAttention<InputDataType, OutputDataType>::Gradient(
const arma::Mat<eT>& /* input */,
const arma::Mat<eT>& /* error */,
arma::Mat<eT>& /* gradient */)
@@ -232,11 +205,9 @@ Gradient(
attentionGradient, offset), actionModule);
}
template<typename InputDataType, typename OutputDataType,
typename... CustomLayers>
template<typename InputDataType, typename OutputDataType>
template<typename Archive>
void RecurrentAttention<InputDataType, OutputDataType, CustomLayers...>::
serialize(
void RecurrentAttention<InputDataType, OutputDataType>::serialize(
Archive& ar, const unsigned int /* version */)
{
ar & BOOST_SERIALIZATION_NVP(rho);
@@ -59,9 +59,6 @@ class Reparametrization
//! Create the Reparametrization object.
Reparametrization();
//! Copy constructor.
Reparametrization(const Reparametrization&);
/**
* Create the Reparametrization layer object using the specified sample vector size.
*
@@ -29,17 +29,6 @@ Reparametrization<InputDataType, OutputDataType>::Reparametrization() :
// Nothing to do here.
}
template<typename InputDataType, typename OutputDataType>
Reparametrization<InputDataType, OutputDataType>::Reparametrization(
const Reparametrization& layer) :
latentSize(layer.latentSize),
stochastic(layer.stochastic),
includeKl(layer.includeKl),
beta(layer.beta)
{
// Nothing to do here.
}
template <typename InputDataType, typename OutputDataType>
Reparametrization<InputDataType, OutputDataType>::Reparametrization(
const size_t latentSize,
@@ -34,19 +34,6 @@ Sequential(const bool model) :
// Nothing to do here.
}
template <typename InputDataType, typename OutputDataType, bool Residual,
typename... CustomLayers>
Sequential<InputDataType, OutputDataType, Residual, CustomLayers...>::
Sequential(const Sequential& layer) :
model(layer.model),
reset(layer.reset),
width(layer.width),
height(layer.height),
ownsLayers(layer.ownsLayers)
{
// Nothing to do here.
}
template <typename InputDataType, typename OutputDataType, bool Residual,
typename... CustomLayers>
Sequential<InputDataType, OutputDataType, Residual, CustomLayers...>::
@@ -52,9 +52,6 @@ class TransposedConvolution
//! Create the Transposed Convolution object.
TransposedConvolution();
//! Copy constructor.
TransposedConvolution(const TransposedConvolution&);
/**
* Create the Transposed Convolution object using the specified number of
* input maps, output maps, filter size, stride and padding parameter.
@@ -171,42 +171,6 @@ TransposedConvolution<
}
}
template<
typename ForwardConvolutionRule,
typename BackwardConvolutionRule,
typename GradientConvolutionRule,
typename InputDataType,
typename OutputDataType
>
TransposedConvolution<
ForwardConvolutionRule,
BackwardConvolutionRule,
GradientConvolutionRule,
InputDataType,
OutputDataType
>::TransposedConvolution(
const TransposedConvolution& layer) :
inSize(layer.inSize),
outSize(layer.outSize),
weights(layer.weights),
kernelWidth(layer.kernelWidth),
kernelHeight(layer.kernelHeight),
strideWidth(layer.strideWidth),
strideHeight(layer.strideHeight),
padWLeft(layer.padWLeft),
padWRight(layer.padWRight),
padHBottom(layer.padHBottom),
padHTop(layer.padHTop),
inputWidth(layer.inputWidth),
inputHeight(layer.inputHeight),
paddingForward(layer.paddingForward),
paddingBackward(layer.paddingBackward),
outputWidth(layer.outputWidth),
outputHeight(layer.outputHeight)
{
Reset();
}
template<
typename ForwardConvolutionRule,
typename BackwardConvolutionRule,
@@ -49,9 +49,6 @@ class VirtualBatchNorm
//! Create the VirtualBatchNorm object.
VirtualBatchNorm();
//! Copy constructor.
VirtualBatchNorm(const VirtualBatchNorm&);
/**
* Create the VirtualBatchNorm layer object for a specified number of input units.
*
@@ -29,22 +29,6 @@ VirtualBatchNorm<InputDataType, OutputDataType>::VirtualBatchNorm() :
{
// Nothing to do here.
}
template<typename InputDataType, typename OutputDataType>
VirtualBatchNorm<InputDataType, OutputDataType>::VirtualBatchNorm(
const VirtualBatchNorm& layer) :
size(layer.size),
eps(layer.eps),
loading(layer.loading),
weights(layer.weights),
referenceBatchMean(layer.referenceBatchMean),
referenceBatchMeanSquared(layer.referenceBatchMeanSquared),
oldCoefficient(layer.oldCoefficient),
newCoefficient(layer.newCoefficient)
{
Reset();
}
template <typename InputDataType, typename OutputDataType>
template<typename eT>
VirtualBatchNorm<InputDataType, OutputDataType>::VirtualBatchNorm(
@@ -68,9 +68,6 @@ class WeightNorm
*/
WeightNorm(LayerTypes<CustomLayers...> layer = LayerTypes<CustomLayers...>());
//! Copy constructor.
WeightNorm(const WeightNorm&);
//! Destructor to release allocated memory.
~WeightNorm();
@@ -162,9 +159,6 @@ class WeightNorm
//! Locally-stored gradient object.
OutputDataType gradient;
//! Locally-stored copy visitor
CopyVisitor<CustomLayers...> copyVisitor;
//! Locally-stored wrapped layer.
LayerTypes<CustomLayers...> wrappedLayer;
@@ -37,18 +37,6 @@ WeightNorm<InputDataType, OutputDataType, CustomLayers...>::WeightNorm(
layerGradients.set_size(layerWeightSize, 1);
}
template<typename InputDataType, typename OutputDataType,
typename... CustomLayers>
WeightNorm<InputDataType, OutputDataType, CustomLayers...>::WeightNorm(
const WeightNorm& layer) :
layerWeightSize(layer.layerWeightSize),
layerGradients(layer.layerGradients),
weights(layer.weights),
layerWeights(layer.layerWeights)
{
Reset();
}
template<typename InputDataType, typename OutputDataType,
typename... CustomLayers>
WeightNorm<InputDataType, OutputDataType, CustomLayers...>::~WeightNorm()
-11
View File
@@ -70,14 +70,6 @@ class RNN
OutputLayerType outputLayer = OutputLayerType(),
InitializationRuleType initializeRule = InitializationRuleType());
/**
* Copy the RNN object.
*
* Warning: Copying RNN is a memory-intensive task multiple layers as well
* as parameters needed to be copied.
*/
RNN(const RNN&);
//! Destructor to release allocated memory.
~RNN();
@@ -414,9 +406,6 @@ class RNN
//! Locally-stored output parameter visitor.
OutputParameterVisitor outputParameterVisitor;
//! Locally-stored copy visitor
CopyVisitor<CustomLayers...> copyVisitor;
//! List of all module parameters for the backward pass (BBTT).
std::vector<arma::mat> moduleOutputParameter;
-31
View File
@@ -51,37 +51,6 @@ RNN<OutputLayerType, InitializationRuleType, CustomLayers...>::RNN(
/* Nothing to do here */
}
template<typename OutputLayerType, typename InitializationRuleType,
typename... CustomLayers>
RNN<OutputLayerType, InitializationRuleType, CustomLayers...>::RNN(
const RNN& network) :
rho(network.rho),
initializeRule(network.initializeRule),
inputSize(network.inputSize),
outputLayer(network.outputLayer),
outputSize(network.outputSize),
targetSize(network.targetSize),
reset(network.reset),
single(network.single),
numFunctions(network.numFunctions),
deterministic(network.deterministic),
parameter(network.parameter)
{
for (size_t i = 0; i < network.network.size(); ++i)
{
this->network.push_back(boost::apply_visitor(copyVisitor,
network.network[i]));
}
ResetCells();
if (parameter.is_empty())
{
ResetParameters();
}
}
template<typename OutputLayerType, typename InitializationRuleType,
typename... CustomLayers>
RNN<OutputLayerType, InitializationRuleType, CustomLayers...>::~RNN()
+15 -23
View File
@@ -81,33 +81,31 @@ BOOST_AUTO_TEST_CASE(VanillaNetworkTest)
bool success = false;
for (size_t trial = 0; trial < 5; ++trial)
{
FFN<NegativeLogLikelihood<>, RandomInitialization> *model = new FFN<
NegativeLogLikelihood<>,
RandomInitialization>();
FFN<NegativeLogLikelihood<>, RandomInitialization> model;
model->Add<Convolution<> >(1, 8, 5, 5, 1, 1, 0, 0, 28, 28);
model->Add<ReLULayer<> >();
model->Add<MaxPooling<> >(8, 8, 2, 2);
model->Add<Convolution<> >(8, 12, 2, 2);
model->Add<ReLULayer<> >();
model->Add<MaxPooling<> >(2, 2, 2, 2);
model->Add<Linear<> >(192, 20);
model->Add<ReLULayer<> >();
model->Add<Linear<> >(20, 10);
model->Add<ReLULayer<> >();
model->Add<Linear<> >(10, 2);
model->Add<LogSoftMax<> >();
model.Add<Convolution<> >(1, 8, 5, 5, 1, 1, 0, 0, 28, 28);
model.Add<ReLULayer<> >();
model.Add<MaxPooling<> >(8, 8, 2, 2);
model.Add<Convolution<> >(8, 12, 2, 2);
model.Add<ReLULayer<> >();
model.Add<MaxPooling<> >(2, 2, 2, 2);
model.Add<Linear<> >(192, 20);
model.Add<ReLULayer<> >();
model.Add<Linear<> >(20, 10);
model.Add<ReLULayer<> >();
model.Add<Linear<> >(10, 2);
model.Add<LogSoftMax<> >();
// Train for only 8 epochs.
ens::RMSProp opt(0.001, 1, 0.88, 1e-8, 8 * nPoints, -1);
double objVal = model->Train(X, Y, opt);
double objVal = model.Train(X, Y, opt);
// Test that objective value returned by FFN::Train() is finite.
BOOST_REQUIRE_EQUAL(std::isfinite(objVal), true);
arma::mat predictionTemp;
model->Predict(X, predictionTemp);
model.Predict(X, predictionTemp);
arma::mat prediction = arma::zeros<arma::mat>(1, predictionTemp.n_cols);
for (size_t i = 0; i < predictionTemp.n_cols; ++i)
@@ -123,12 +121,6 @@ BOOST_AUTO_TEST_CASE(VanillaNetworkTest)
success = true;
break;
}
// Test for copy constructor.
FFN<NegativeLogLikelihood<>, RandomInitialization> model1(*model);
arma::mat prediction1;
delete model;
model1.Predict(X, prediction1);
CheckMatrices(prediction1, predictionTemp);
}
BOOST_REQUIRE_EQUAL(success, true);
+40 -65
View File
@@ -32,7 +32,7 @@ BOOST_AUTO_TEST_SUITE(FeedForwardNetworkTest);
* Train and evaluate a model with the specified structure.
*/
template<typename MatType = arma::mat, typename ModelType>
void TestNetwork(ModelType* model,
void TestNetwork(ModelType& model,
MatType& trainData,
MatType& trainLabels,
MatType& testData,
@@ -41,10 +41,10 @@ void TestNetwork(ModelType* model,
const double classificationErrorThreshold)
{
ens::RMSProp opt(0.01, 32, 0.88, 1e-8, maxEpochs * trainData.n_cols, -1);
model->Train(trainData, trainLabels, opt);
model.Train(trainData, trainLabels, opt);
MatType predictionTemp;
model->Predict(testData, predictionTemp);
model.Predict(testData, predictionTemp);
MatType prediction = arma::zeros<MatType>(1, predictionTemp.n_cols);
for (size_t i = 0; i < predictionTemp.n_cols; ++i)
@@ -58,22 +58,6 @@ void TestNetwork(ModelType* model,
BOOST_REQUIRE_LE(classificationError, classificationErrorThreshold);
}
// network1 should be allocated with `new`, and trained on some data.
template<typename MatType = arma::mat, typename ModelType>
void CheckCopyFunction(ModelType* network1, MatType& inputs)
{
FFN<> network2(*network1);
arma::mat predictions1;
network1->Predict(inputs, predictions1);
delete network1;
// Deallocating all of network1's memory, so that
// if network2 is trying to use any of that memory.
arma::mat predictions2;
network2.Predict(inputs, predictions2);
CheckMatrices(predictions1, predictions2);
}
/**
* Train the vanilla network on a larger dataset.
*/
@@ -114,17 +98,16 @@ BOOST_AUTO_TEST_CASE(VanillaNetworkTest)
* +-----+ +-----+
*/
FFN<NegativeLogLikelihood<> > *model = new FFN<NegativeLogLikelihood<> >();
model->Add<Linear<> >(trainData.n_rows, 8);
model->Add<SigmoidLayer<> >();
model->Add<Linear<> >(8, 3);
model->Add<LogSoftMax<> >();
FFN<NegativeLogLikelihood<> > model;
model.Add<Linear<> >(trainData.n_rows, 8);
model.Add<SigmoidLayer<> >();
model.Add<Linear<> >(8, 3);
model.Add<LogSoftMax<> >();
// Vanilla neural net with logistic activation function.
// Because 92% of the patients are not hyperthyroid the neural
// network must be significant better than 92%.
TestNetwork<>(model, trainData, trainLabels, testData, testLabels, 10, 0.1);
CheckCopyFunction(model, testData);
arma::mat dataset;
dataset.load("mnist_first250_training_4s_and_9s.arm");
@@ -137,14 +120,13 @@ BOOST_AUTO_TEST_CASE(VanillaNetworkTest)
labels.submat(0, labels.n_cols / 2, 0, labels.n_cols - 1).fill(1);
labels += 1;
FFN<NegativeLogLikelihood<> > *model1 = new FFN<NegativeLogLikelihood<> >();
model1->Add<Linear<> >(dataset.n_rows, 10);
model1->Add<SigmoidLayer<> >();
model1->Add<Linear<> >(10, 2);
model1->Add<LogSoftMax<> >();
FFN<NegativeLogLikelihood<> > model1;
model1.Add<Linear<> >(dataset.n_rows, 10);
model1.Add<SigmoidLayer<> >();
model1.Add<Linear<> >(10, 2);
model1.Add<LogSoftMax<> >();
// Vanilla neural net with logistic activation function.
TestNetwork<>(model1, dataset, labels, dataset, labels, 10, 0.2);
CheckCopyFunction(model1, dataset);
}
BOOST_AUTO_TEST_CASE(ForwardBackwardTest)
@@ -263,19 +245,17 @@ BOOST_AUTO_TEST_CASE(DropoutNetworkTest)
* +-----+
*/
FFN<NegativeLogLikelihood<> > *model = new FFN<NegativeLogLikelihood<> >();
model->Add<Linear<> >(trainData.n_rows, 8);
model->Add<SigmoidLayer<> >();
model->Add<Dropout<> >();
model->Add<Linear<> >(8, 3);
model->Add<LogSoftMax<> >();
FFN<NegativeLogLikelihood<> > model;
model.Add<Linear<> >(trainData.n_rows, 8);
model.Add<SigmoidLayer<> >();
model.Add<Dropout<> >();
model.Add<Linear<> >(8, 3);
model.Add<LogSoftMax<> >();
// Vanilla neural net with logistic activation function.
// Because 92% of the patients are not hyperthyroid the neural
// network must be significant better than 92%.
TestNetwork<>(model, trainData, trainLabels, testData, testLabels, 10, 0.1);
CheckCopyFunction(model, testData);
arma::mat dataset;
dataset.load("mnist_first250_training_4s_and_9s.arm");
@@ -289,20 +269,18 @@ BOOST_AUTO_TEST_CASE(DropoutNetworkTest)
labels.submat(0, labels.n_cols / 2, 0, labels.n_cols - 1).fill(1);
labels += 1;
FFN<NegativeLogLikelihood<> > *model1 = new FFN<NegativeLogLikelihood<> >();
model1->Add<Linear<> >(dataset.n_rows, 10);
model1->Add<SigmoidLayer<> >();
model1->Add<Dropout<> >();
model1->Add<Linear<> >(10, 2);
model1->Add<LogSoftMax<> >();
FFN<NegativeLogLikelihood<> > model1;
model1.Add<Linear<> >(dataset.n_rows, 10);
model1.Add<SigmoidLayer<> >();
model.Add<Dropout<> >();
model1.Add<Linear<> >(10, 2);
model1.Add<LogSoftMax<> >();
// Vanilla neural net with logistic activation function.
TestNetwork<>(model1, dataset, labels, dataset, labels, 10, 0.2);
CheckCopyFunction(model1, dataset);
}
/**
* Train the highway network on a larger dataset.
*
*/
BOOST_AUTO_TEST_CASE(HighwayNetworkTest)
{
@@ -317,16 +295,15 @@ BOOST_AUTO_TEST_CASE(HighwayNetworkTest)
labels.submat(0, labels.n_cols / 2, 0, labels.n_cols - 1).fill(1);
labels += 1;
FFN<NegativeLogLikelihood<> > *model = new FFN<NegativeLogLikelihood<> >();
model->Add<Linear<> >(dataset.n_rows, 10);
FFN<NegativeLogLikelihood<> > model;
model.Add<Linear<> >(dataset.n_rows, 10);
Highway<>* highway = new Highway<>(10, true);
highway->Add<Linear<> >(10, 10);
highway->Add<SigmoidLayer<> >();
model->Add(highway); // This takes ownership of the memory.
model->Add<Linear<> >(10, 2);
model->Add<LogSoftMax<> >();
model.Add(highway); // This takes ownership of the memory.
model.Add<Linear<> >(10, 2);
model.Add<LogSoftMax<> >();
TestNetwork<>(model, dataset, labels, dataset, labels, 10, 0.2);
CheckCopyFunction(model, dataset);
}
/**
@@ -371,17 +348,16 @@ BOOST_AUTO_TEST_CASE(DropConnectNetworkTest)
*
*/
FFN<NegativeLogLikelihood<> > *model = new FFN<NegativeLogLikelihood<> >();
model->Add<Linear<> >(trainData.n_rows, 8);
model->Add<SigmoidLayer<> >();
model->Add<DropConnect<> >(8, 3);
model->Add<LogSoftMax<> >();
FFN<NegativeLogLikelihood<> > model;
model.Add<Linear<> >(trainData.n_rows, 8);
model.Add<SigmoidLayer<> >();
model.Add<DropConnect<> >(8, 3);
model.Add<LogSoftMax<> >();
// Vanilla neural net with logistic activation function.
// Because 92% of the patients are not hyperthyroid the neural
// network must be significant better than 92%.
TestNetwork<>(model, trainData, trainLabels, testData, testLabels, 10, 0.1);
CheckCopyFunction(model, testData);
arma::mat dataset;
dataset.load("mnist_first250_training_4s_and_9s.arm");
@@ -394,14 +370,13 @@ BOOST_AUTO_TEST_CASE(DropConnectNetworkTest)
labels.submat(0, labels.n_cols / 2, 0, labels.n_cols - 1).fill(1);
labels += 1;
FFN<NegativeLogLikelihood<> > *model1 = new FFN<NegativeLogLikelihood<> >();
model1->Add<Linear<> >(dataset.n_rows, 10);
model1->Add<SigmoidLayer<> >();
model1->Add<DropConnect<> >(10, 2);
model1->Add<LogSoftMax<> >();
FFN<NegativeLogLikelihood<> > model1;
model1.Add<Linear<> >(dataset.n_rows, 10);
model1.Add<SigmoidLayer<> >();
model1.Add<DropConnect<> >(10, 2);
model1.Add<LogSoftMax<> >();
// Vanilla neural net with logistic activation function.
TestNetwork<>(model1, dataset, labels, dataset, labels, 10, 0.2);
CheckCopyFunction(model1, dataset);
}
/**
-71
View File
@@ -348,75 +348,4 @@ BOOST_AUTO_TEST_CASE(GANMemorySharingTest)
trainData);
}
/*
* Create GAN network and copy of that GAN and
* check whether predictions are same or not.
*/
BOOST_AUTO_TEST_CASE(GANCopyTest)
{
size_t generatorHiddenLayerSize = 8;
size_t discriminatorHiddenLayerSize = 8;
size_t generatorOutputSize = 1;
size_t discriminatorOutputSize = 1;
size_t discriminatorPreTrain = 0;
size_t batchSize = 8;
size_t noiseDim = 1;
size_t generatorUpdateStep = 1;
double multiplier = 1;
double eps = 1e-8;
double stepSize = 0.0003;
size_t numIterations = 8;
double tolerance = 1e-5;
bool shuffle = true;
arma::mat trainData(1, 10000);
trainData.imbue( [&]() { return arma::as_scalar(RandNormal(4, 0.5));});
trainData = arma::sort(trainData);
// Create the Discriminator network.
FFN<SigmoidCrossEntropyError<> > discriminator;
discriminator.Add<Linear<> > (
generatorOutputSize, discriminatorHiddenLayerSize * 2);
discriminator.Add<ReLULayer<> >();
discriminator.Add<Linear<> > (
discriminatorHiddenLayerSize * 2, discriminatorHiddenLayerSize * 2);
discriminator.Add<ReLULayer<> >();
discriminator.Add<Linear<> > (
discriminatorHiddenLayerSize * 2, discriminatorHiddenLayerSize * 2);
discriminator.Add<ReLULayer<> >();
discriminator.Add<Linear<> > (
discriminatorHiddenLayerSize * 2, discriminatorOutputSize);
// Create the Generator network.
FFN<SigmoidCrossEntropyError<> > generator;
generator.Add<Linear<> >(noiseDim, generatorHiddenLayerSize);
generator.Add<SoftPlusLayer<> >();
generator.Add<Linear<> >(generatorHiddenLayerSize, generatorOutputSize);
// Create GAN.
GaussianInitialization gaussian(0, 0.1);
ens::Adam optimizer(stepSize, batchSize, 0.9, 0.999, eps, numIterations,
tolerance, shuffle);
std::function<double ()> noiseFunction = [](){ return math::Random(-8, 8) +
math::RandNormal(0, 1) * 0.01;};
GAN<FFN<SigmoidCrossEntropyError<> >,
GaussianInitialization,
std::function<double()> >* gan = new GAN<FFN<SigmoidCrossEntropyError<> >,
GaussianInitialization, std::function<double()> >(generator,
discriminator, gaussian, noiseFunction, noiseDim, batchSize,
generatorUpdateStep, discriminatorPreTrain, multiplier);
gan->Train(trainData, optimizer);
arma::mat predictions;
gan->Predict(trainData, predictions);
GAN<FFN<SigmoidCrossEntropyError<> >,
GaussianInitialization,
std::function<double()> >gan2(*gan);
delete gan;
arma::mat predictions1;
gan2.Predict(trainData, predictions1);
CheckMatrices(predictions, predictions1);
}
BOOST_AUTO_TEST_SUITE_END();
+25 -46
View File
@@ -516,27 +516,11 @@ arma::Mat<char> GenerateReberGrammarData(
return transitions;
}
// network1 should be allocated with `new`, and trained on some data.
template<typename MatType = arma::mat, typename ModelType>
void CheckCopyFunction(ModelType* network1, MatType& inputs)
{
ModelType network2(*network1);
arma::cube predictions1;
network1->Predict(inputs, predictions1);
delete network1;
// Deallocating all of network1's memory, so that
// network2 is not trying to use any of that memory.
arma::cube predictions2;
network2.Predict(inputs, predictions2);
CheckMatrices(predictions1, predictions2);
}
/**
* Train the specified network and the construct a Reber grammar dataset.
*/
template<typename ModelType>
void ReberGrammarTestNetwork(ModelType* model,
void ReberGrammarTestNetwork(ModelType& model,
const bool recursive = false,
const size_t averageRecursion = 3,
const size_t maxRecursion = 5,
@@ -583,11 +567,10 @@ void ReberGrammarTestNetwork(ModelType* model,
size_t successes = 0;
size_t offset = 0;
const size_t inputSize = 7;
arma::cube input1;
for (size_t trial = 0; trial < trials; ++trial)
{
// Reset model before using for next trial.
model->Reset();
model.Reset();
MomentumSGD opt(0.06, 50, 2, -50000);
arma::cube inputTemp, labelsTemp;
@@ -603,8 +586,8 @@ void ReberGrammarTestNetwork(ModelType* model,
labelsTemp = arma::cube(trainLabels.at(0, j).memptr(), inputSize, 1,
trainInput.at(0, j).n_elem / inputSize, false, true);
model->Rho() = inputTemp.n_elem / inputSize;
model->Train(inputTemp, labelsTemp, opt);
model.Rho() = inputTemp.n_elem / inputSize;
model.Train(inputTemp, labelsTemp, opt);
opt.ResetPolicy() = false;
}
}
@@ -617,10 +600,9 @@ void ReberGrammarTestNetwork(ModelType* model,
arma::cube prediction;
arma::cube input(testInput.at(0, i).memptr(), inputSize, 1,
testInput.at(0, i).n_elem / inputSize, false, true);
input1 = input;
model->Rho() = input.n_elem / inputSize;
model->Predict(input, prediction);
model.Rho() = input.n_elem / inputSize;
model.Predict(input, prediction);
const size_t reberGrammerSize = 7;
std::string inputReber = "";
@@ -662,7 +644,6 @@ void ReberGrammarTestNetwork(ModelType* model,
offset += 3;
}
CheckCopyFunction(model, input1);
BOOST_REQUIRE_GE(successes, 1);
}
@@ -672,11 +653,11 @@ void ReberGrammarTestNetwork(ModelType* model,
*/
BOOST_AUTO_TEST_CASE(LSTMReberGrammarTest)
{
RNN<MeanSquaredError<> > *model = new RNN<MeanSquaredError<> >(5);
model->Add<Linear<> >(7, 10);
model->Add<LSTM<> >(10, 10);
model->Add<Linear<> >(10, 7);
model->Add<SigmoidLayer<> >();
RNN<MeanSquaredError<> > model(5);
model.Add<Linear<> >(7, 10);
model.Add<LSTM<> >(10, 10);
model.Add<Linear<> >(10, 7);
model.Add<SigmoidLayer<> >();
ReberGrammarTestNetwork(model, false);
}
@@ -685,11 +666,11 @@ BOOST_AUTO_TEST_CASE(LSTMReberGrammarTest)
*/
BOOST_AUTO_TEST_CASE(FastLSTMReberGrammarTest)
{
RNN<MeanSquaredError<> > *model = new RNN<MeanSquaredError<> >(5);
model->Add<Linear<> >(7, 8);
model->Add<FastLSTM<> >(8, 8);
model->Add<Linear<> >(8, 7);
model->Add<SigmoidLayer<> >();
RNN<MeanSquaredError<> > model(5);
model.Add<Linear<> >(7, 8);
model.Add<FastLSTM<> >(8, 8);
model.Add<Linear<> >(8, 7);
model.Add<SigmoidLayer<> >();
ReberGrammarTestNetwork(model, false);
}
@@ -698,11 +679,11 @@ BOOST_AUTO_TEST_CASE(FastLSTMReberGrammarTest)
*/
BOOST_AUTO_TEST_CASE(GRURecursiveReberGrammarTest)
{
RNN<MeanSquaredError<> > *model = new RNN<MeanSquaredError<> >(5);
model->Add<Linear<> >(7, 16);
model->Add<GRU<> >(16, 16);
model->Add<Linear<> >(16, 7);
model->Add<SigmoidLayer<> >();
RNN<MeanSquaredError<> > model(5);
model.Add<Linear<> >(7, 16);
model.Add<GRU<> >(16, 16);
model.Add<Linear<> >(16, 7);
model.Add<SigmoidLayer<> >();
ReberGrammarTestNetwork(model, true, 3, 5, 10, 7);
}
@@ -711,12 +692,10 @@ BOOST_AUTO_TEST_CASE(GRURecursiveReberGrammarTest)
*/
BOOST_AUTO_TEST_CASE(BRNNReberGrammarTest)
{
BRNN<MeanSquaredError<>,
AddMerge<>, SigmoidLayer<> > *model = new
BRNN<MeanSquaredError<>, AddMerge<>, SigmoidLayer<> >(5);
model->Add<Linear<> >(7, 10);
model->Add<LSTM<> >(10, 10);
model->Add<Linear<> >(10, 7);
BRNN<MeanSquaredError<>, AddMerge<>, SigmoidLayer<> > model(5);
model.Add<Linear<> >(7, 10);
model.Add<LSTM<> >(10, 10);
model.Add<Linear<> >(10, 7);
ReberGrammarTestNetwork(model, false, 3, 5, 1);
}