Add Clone() function which handles polymorphism correctly.

This commit is contained in:
Marcus Edel
2021-01-30 04:42:02 +01:00
parent 0c4db57106
commit eafac8609d
43 changed files with 154 additions and 6 deletions
+1 -1
View File
@@ -296,7 +296,7 @@ class FFN
* network or parameters for layers, its state may become invalid, so be sure
* to call ResetParameters() afterwards.
*/
std::vector<Layer<InputType, OutputType>* >& Model() { return network; }
std::vector<Layer<InputType, OutputType>*>& Model() { return network; }
//! Return the number of separable functions (the number of predictor points).
size_t NumFunctions() const { return numFunctions; }
+4 -3
View File
@@ -48,6 +48,7 @@ template<typename OutputLayerType,
typename OutputType>
FFN<OutputLayerType, InitializationRuleType, InputType, OutputType>::~FFN()
{
//network.clear();
for (size_t i = 0; i < network.size(); ++i)
delete network[i];
}
@@ -234,8 +235,8 @@ void FFN<OutputLayerType, InitializationRuleType, InputType, OutputType>::
OutputType resultsTemp;
Forward(arma::mat(predictors.colptr(0), predictors.n_rows, 1, false, true));
resultsTemp = network.back()->OutputParameter().col(0);
resultsTemp = network.back()->OutputParameter().col(0);
results = arma::mat(resultsTemp.n_elem, predictors.n_cols);
results.col(0) = resultsTemp.col(0);
@@ -629,7 +630,7 @@ FFN<OutputLayerType, InitializationRuleType, InputType, OutputType>::FFN(
// Build new layers according to source network
for (size_t i = 0; i < network.network.size(); ++i)
{
this->network.push_back(network.network[i]);
this->network.push_back(network.network[i]->Clone());
ResetUpdate(this->network.back());
}
};
@@ -665,7 +666,7 @@ template<typename OutputLayerType,
typename OutputType>
FFN<OutputLayerType, InitializationRuleType, InputType, OutputType>&
FFN<OutputLayerType, InitializationRuleType, InputType, OutputType>::
operator = (FFN network)
operator =(FFN network)
{
Swap(network);
return *this;
@@ -73,6 +73,9 @@ class BaseLayer : public Layer<InputType, OutputType>
// Nothing to do here.
}
//! Clone the BaseLayer object. This handles polymorphism correctly.
BaseLayer* Clone() const { return new BaseLayer(*this); }
/**
* Ordinary feed forward pass of a neural network, evaluating the function
* f(x) by propagating the activity forward through f.
@@ -141,6 +144,7 @@ class BaseLayer : public Layer<InputType, OutputType>
// typename OutputType = arma::mat
// >
// using SigmoidLayer = BaseLayer<ActivationFunction, InputType, OutputType>;
typedef BaseLayer<LogisticFunction, arma::mat, arma::mat> Sigmoid;
typedef BaseLayer<LogisticFunction, arma::mat, arma::mat> SigmoidLayer;
/**
+3
View File
@@ -53,6 +53,9 @@ class CReLUType : public Layer<InputType, OutputType>
//! Create the CReLU object.
CReLUType();
//! Clone the CReLUType object. This handles polymorphism correctly.
CReLUType* Clone() const { return new CReLUType(*this); }
/**
* Ordinary feed forward pass of a neural network, evaluating the function
* f(x) by propagating the activity forward through f.
+3
View File
@@ -69,6 +69,9 @@ class CELUType : public Layer<InputType, OutputType>
*/
CELUType(const double alpha = 1.0);
//! Clone the CELUType object. This handles polymorphism correctly.
CELUType* Clone() const { return new CELUType(*this); }
/**
* Ordinary feed forward pass of a neural network, evaluating the function
* f(x) by propagating the activity forward through f.
+3
View File
@@ -64,6 +64,9 @@ class ConcatType : public Layer<InputType, OutputType>
*/
~ConcatType();
//! Clone the ConcatType object. This handles polymorphism correctly.
ConcatType* Clone() const { return new ConcatType(*this); }
/**
* Ordinary feed forward pass of a neural network, evaluating the function
* f(x) by propagating the activity forward through f.
@@ -53,6 +53,9 @@ class ConcatenateType : public Layer<InputType, OutputType>
//! Operator= move constructor.
ConcatenateType& operator=(ConcatenateType&& layer);
//! Clone the ConcatenateType object. This handles polymorphism correctly.
ConcatenateType* Clone() const { return new ConcatenateType(*this); }
/**
* Ordinary feed forward pass of a neural network, evaluating the function
* f(x) by propagating the activity forward through f.
@@ -43,6 +43,8 @@ class ConstantType : public Layer<InputType, OutputType>
*/
ConstantType(const size_t outSize, const double scalar = 0);
//! Clone the ConstantType object. This handles polymorphism correctly.
ConstantType* Clone() const { return new ConstantType(*this); }
/**
* Ordinary feed forward pass of a neural network. The forward pass fills the
* output with the specified constant parameter.
@@ -73,6 +73,9 @@ class DropConnectType : public Layer<InputType, OutputType>
const size_t outSize,
const double ratio = 0.5);
//! Clone the DropConnectType object. This handles polymorphism correctly.
DropConnectType* Clone() const { return new DropConnectType(*this); }
/**
* Ordinary feed forward pass of the DropConnect layer.
*
+3
View File
@@ -73,6 +73,9 @@ class DropoutType : public Layer<InputType, OutputType>
//! Move assignment operator.
DropoutType& operator=(DropoutType&& layer);
//! Clone the DropoutType object. This handles polymorphism correctly.
DropoutType* Clone() const { return new DropoutType(*this); }
/**
* Ordinary feed forward pass of the dropout layer.
*
+2
View File
@@ -127,6 +127,8 @@ class ELUType : public Layer<InputType, OutputType>
*/
ELUType(const double alpha);
//! Clone the ELUType object. This handles polymorphism correctly.
ELUType* Clone() const { return new ELUType(*this); }
/**
* Ordinary feed forward pass of a neural network, evaluating the function
* f(x) by propagating the activity forward through f.
@@ -97,6 +97,9 @@ class FastLSTMType : public Layer<InputType, OutputType>
const size_t outSize,
const size_t rho = std::numeric_limits<size_t>::max());
//! Clone the FastLSTMType object. This handles polymorphism correctly.
FastLSTMType* Clone() const { return new FastLSTMType(*this); }
/**
* Ordinary feed forward pass of a neural network, evaluating the function
* f(x) by propagating the activity forward through f.
@@ -67,6 +67,9 @@ class FlexibleReLUType : public Layer<InputType, OutputType>
*/
FlexibleReLUType(const double alpha = 0);
//! Clone the FlexibleReLUType object. This handles polymorphism correctly.
FlexibleReLUType* Clone() const { return new FlexibleReLUType(*this); }
/**
* Reset the layer parameter (alpha). The method is called to
* assign the allocated memory to the learnable layer parameter.
@@ -59,6 +59,9 @@ class HardTanHType : public Layer<InputType, OutputType>
*/
HardTanHType(const double maxValue = 1, const double minValue = -1);
//! Clone the HardTanHType object. This handles polymorphism correctly.
HardTanHType* Clone() const { return new HardTanHType(*this); }
/**
* Ordinary feed forward pass of a neural network, evaluating the function
* f(x) by propagating the activity forward through f.
@@ -59,6 +59,9 @@ class HardShrinkType : public Layer<InputType, OutputType>
*/
HardShrinkType(const double lambda = 0.5);
//! Clone the HardShrinkType object. This handles polymorphism correctly.
HardShrinkType* Clone() const { return new HardShrinkType(*this); }
/**
* Ordinary feed forward pass of a neural network, evaluating the function
* f(x) by propagating the activity forward through f.
+3
View File
@@ -65,6 +65,9 @@ class HighwayType : public Layer<InputType, OutputType>
//! Destroy the Highway object.
~HighwayType();
//! Clone the HighwayType object. This handles polymorphism correctly.
HighwayType* Clone() const { return new HighwayType(*this); }
/**
* Reset the layer parameter.
*/
+3
View File
@@ -36,6 +36,9 @@ class JoinType : public Layer<InputType, OutputType>
//! Create the JoinType object.
JoinType();
//! Clone the JoinType object. This handles polymorphism correctly.
JoinType* Clone() const { return new JoinType(*this); }
/**
* Ordinary feed forward pass of a neural network, evaluating the function
* f(x) by propagating the activity forward through f.
+16 -2
View File
@@ -57,12 +57,26 @@ class Layer
{
public:
//! Default constructor.
Layer() : outputWidth(0), outputHeight(0)
{ /* Nothing to do here */ }
Layer() : outputWidth(0), outputHeight(0) { /* Nothing to do here */ }
//! Default deconstructor.
virtual ~Layer() = default;
//! Copy constructor.
Layer(const Layer& /* layer */) { /* Nothing to do here */ }
//! Make a copy of the object.
virtual Layer* Clone() const = 0;
//! Move constructor.
Layer(Layer&& /* layer */) { /* Nothing to do here */ }
//! Copy assignment operator.
virtual Layer& operator=(const Layer& /* layer */) { return *this; }
//! Move assignment operator.
virtual Layer& operator=(Layer&& /* layer */) { return *this; }
/**
* Takes an input object, and computes the corresponding output of the layer.
* In general input and output are matrices. However, some special layers like
@@ -76,6 +76,9 @@ class LayerNormType : public Layer<InputType, OutputType>
*/
LayerNormType(const size_t size, const double eps = 1e-8);
//! Clone the LayerNormType object. This handles polymorphism correctly.
LayerNormType* Clone() const { return new LayerNormType(*this); }
/**
* Reset the layer parameters.
*/
@@ -53,6 +53,9 @@ class LeakyReLUType : public Layer<InputType, OutputType>
*/
LeakyReLUType(const double alpha = 0.03);
//! Clone the LeakyReLUType object. This handles polymorphism correctly.
LeakyReLUType* Clone() const { return new LeakyReLUType(*this); }
/**
* Ordinary feed forward pass of a neural network, evaluating the function
* f(x) by propagating the activity forward through f.
+3
View File
@@ -73,6 +73,9 @@ class LinearType: public Layer<InputType, OutputType>
//! Move assignment operator.
LinearType& operator=(LinearType&& layer);
//! Clone the LinearType object. This handles polymorphism correctly.
LinearType* Clone() const { return new LinearType(*this); }
/**
* Reset the layer parameter (weights and bias). The method is called to
* assign the allocated memory to the internal learnable parameters.
@@ -67,6 +67,9 @@ class Linear3DType : public Layer<InputType, OutputType>
//! Move assignment operator.
Linear3DType& operator=(Linear3DType&& layer);
//! Clone the Linear3DType object. This handles polymorphism correctly.
Linear3DType* Clone() const { return new Linear3DType(*this); }
/*
* Reset the layer parameter.
*/
@@ -55,6 +55,9 @@ class LinearNoBiasType : public Layer<InputType, OutputType>
const size_t outSize,
RegularizerType regularizer = RegularizerType());
//! Clone the LinearNoBiasType object. This handles polymorphism correctly.
LinearNoBiasType* Clone() const { return new LinearNoBiasType(*this); }
//! Reset the layer parameter.
void Reset();
@@ -41,6 +41,9 @@ class LogSoftMaxType : public Layer<InputType, OutputType>
*/
LogSoftMaxType();
//! Clone the LogSoftMaxType object. This handles polymorphism correctly.
LogSoftMaxType* Clone() const { return new LogSoftMaxType(*this); }
/**
* Ordinary feed forward pass of a neural network, evaluating the function
* f(x) by propagating the activity forward through f.
+3
View File
@@ -49,6 +49,9 @@ class LookupType : public Layer<InputType, OutputType>
*/
LookupType(const size_t vocabSize = 0, const size_t embeddingSize = 0);
//! Clone the LookupType object. This handles polymorphism correctly.
LookupType* Clone() const { return new LookupType(*this); }
/**
* Ordinary feed forward pass of a neural network, evaluating the function
* f(x) by propagating the activity forward through f.
@@ -84,6 +84,12 @@ class MultiheadAttentionType : public Layer<InputType, OutputType>
const InputType& attnmask = InputType(),
const InputType& keyPaddingMask = InputType());
//! Clone the MultiheadAttentionType object. This handles polymorphism correctly.
MultiheadAttentionType* Clone() const
{
return new MultiheadAttentionType(*this);
}
/**
* Reset the layer parameters.
*/
@@ -37,6 +37,12 @@ class MultiplyConstantType : public Layer<InputType, OutputType>
//! Create the MultiplyConstant object.
MultiplyConstantType(const double scalar = 1.0);
//! Clone the MultiplyConstantType object. This handles polymorphism correctly.
MultiplyConstantType* Clone() const
{
return new MultiplyConstantType(*this);
}
/**
* Ordinary feed forward pass of a neural network. Multiply the input with the
* specified constant scalar value.
@@ -51,6 +51,9 @@ class MultiplyMergeType : public Layer<InputType, OutputType>
//! Destructor to release allocated memory.
~MultiplyMergeType();
//! Clone the MultiplyMergeType object. This handles polymorphism correctly.
MultiplyMergeType* Clone() const { return new MultiplyMergeType(*this); }
/**
* Ordinary feed forward pass of a neural network, evaluating the function
* f(x) by propagating the activity forward through f.
@@ -57,6 +57,9 @@ class NoisyLinearType : public Layer<InputType, OutputType>
//! Operator= move constructor.
NoisyLinearType& operator=(NoisyLinearType&& layer);
//! Clone the NoisyLinearType object. This handles polymorphism correctly.
NoisyLinearType* Clone() const { return new NoisyLinearType(*this); }
//! Reset the layer parameter.
void Reset();
+3
View File
@@ -47,6 +47,9 @@ class PaddingType : public Layer<InputType, OutputType>
const size_t padHTop = 0,
const size_t padHBottom = 0);
//! Clone the PaddingType object. This handles polymorphism correctly.
PaddingType* Clone() const { return new PaddingType(*this); }
/**
* Ordinary feed forward pass of a neural network, evaluating the function
* f(x) by propagating the activity forward through f.
@@ -55,6 +55,9 @@ class PReLUType : public Layer<InputType, OutputType>
*/
PReLUType(const double userAlpha = 0.03);
//! Clone the PReLUType object. This handles polymorphism correctly.
PReLUType* Clone() const { return new PReLUType(*this); }
//! Reset the layer parameter.
void Reset();
@@ -51,6 +51,8 @@ class PositionalEncodingType : public Layer<InputType, OutputType>
PositionalEncodingType(const size_t embedDim,
const size_t maxSequenceLength);
//! Clone the PositionalEncodingType object. This handles polymorphism correctly.
PositionalEncodingType* Clone() const { return new PositionalEncodingType(*this); }
/**
* Ordinary feed forward pass of a neural network, evaluating the function
* f(x) by propagating the activity forward through f.
@@ -42,6 +42,9 @@ class ReinforceNormalType : public Layer<InputType, OutputType>
*/
ReinforceNormalType(const double stdev = 1.0);
//! Clone the ReinforceNormalType object. This handles polymorphism correctly.
ReinforceNormalType* Clone() const { return new ReinforceNormalType(*this); }
/**
* Ordinary feed forward pass of a neural network, evaluating the function
* f(x) by propagating the activity forward through f.
@@ -72,6 +72,15 @@ class ReparametrizationType : public Layer<InputType, OutputType>
const bool includeKl = true,
const double beta = 1);
/**
* Clone the ReparametrizationType object. This handles polymorphism
* correctly.
*/
ReparametrizationType* Clone() const
{
return new ReparametrizationType(*this);
}
/**
* Ordinary feed forward pass of a neural network, evaluating the function
* f(x) by propagating the activity forward through f.
+3
View File
@@ -42,6 +42,9 @@ class SelectType : public Layer<InputType, OutputType>
*/
SelectType(const size_t index = 0, const size_t elements = 0);
//! Clone the SelectType object. This handles polymorphism correctly.
SelectType* Clone() const { return new SelectType(*this); }
/**
* Ordinary feed forward pass of a neural network, evaluating the function
* f(x) by propagating the activity forward through f.
@@ -88,6 +88,9 @@ class SequentialType : public Layer<InputType, OutputType>
//! Destroy the Sequential object.
~SequentialType();
//! Clone the SequentialType object. This handles polymorphism correctly.
SequentialType* Clone() const { return new SequentialType(*this); }
/**
* Ordinary feed forward pass of a neural network, evaluating the function
* f(x) by propagating the activity forward through f.
+3
View File
@@ -41,6 +41,9 @@ class SoftmaxType : public Layer<InputType, OutputType>
//! Create the Softmax object.
SoftmaxType();
//! Clone the SoftmaxType object. This handles polymorphism correctly.
SoftmaxType* Clone() const { return new SoftmaxType(*this); }
/**
* Ordinary feed forward pass of a neural network, evaluating the function
* f(x) by propagating the activity forward through f.
+3
View File
@@ -38,6 +38,9 @@ class SoftminType : public Layer<InputType, OutputType>
//! Create the Softmin object.
SoftminType();
//! Clone the SoftminType object. This handles polymorphism correctly.
SoftminType* Clone() const { return new SoftminType(*this); }
/**
* Ordinary feed forward pass of a neural network, evaluating the function
* f(x) by propagating the activity forward through f.
@@ -62,6 +62,9 @@ class SoftShrinkType : public Layer<InputType, OutputType>
*/
SoftShrinkType(const double lambda = 0.5);
//! Clone the SoftShrinkType object. This handles polymorphism correctly.
SoftShrinkType* Clone() const { return new SoftShrinkType(*this); }
/**
* Ordinary feed forward pass of a neural network, evaluating the function
* f(x) by propagating the activity forward through f.
@@ -58,6 +58,9 @@ class SpatialDropoutType : public Layer<InputType, OutputType>
*/
SpatialDropoutType(const size_t size, const double ratio = 0.5);
//! Clone the SpatialDropoutType object. This handles polymorphism correctly.
SpatialDropoutType* Clone() const { return new SpatialDropoutType(*this); }
/**
* Ordinary feed forward pass of the SpatialDropout layer.
*
+3
View File
@@ -59,6 +59,9 @@ class SubviewType : public Layer<InputType, OutputType>
/* Nothing to do here */
}
//! Clone the SubviewType object. This handles polymorphism correctly.
SubviewType* Clone() const { return new SubviewType(*this); }
/**
* Ordinary feed forward pass of a neural network, evaluating the function
* f(x) by propagating the activity forward through f.
@@ -62,6 +62,12 @@ class VirtualBatchNormType : public Layer<InputType, OutputType>
const size_t size,
const double eps = 1e-8);
//! Clone the VirtualBatchNormType object. This handles polymorphism correctly.
VirtualBatchNormType* Clone() const
{
return new VirtualBatchNormType(*this);
}
/**
* Reset the layer parameters.
*/
@@ -62,6 +62,9 @@ class WeightNormType : public Layer<InputType, OutputType>
//! Destructor to release allocated memory.
~WeightNormType();
//! Clone the WeightNormType object. This handles polymorphism correctly.
WeightNormType* Clone() const { return new WeightNormType(*this); }
/**
* Reset the layer parameters.
*/