Merge pull request #2650 from UtR491/issue-2590

Added Weight Size to concat and fast_lstm layers and
This commit is contained in:
Ryan Curtin
2020-10-31 15:59:54 -04:00
committed by GitHub
9 changed files with 93 additions and 20 deletions
+1 -1
View File
@@ -23,7 +23,7 @@ template<typename InputDataType, typename OutputDataType>
Add<InputDataType, OutputDataType>::Add(const size_t outSize) :
outSize(outSize)
{
weights.set_size(outSize, 1);
weights.set_size(WeightSize(), 1);
}
template<typename InputDataType, typename OutputDataType>
@@ -122,8 +122,7 @@ AtrousConvolution<
dilationWidth(dilationWidth),
dilationHeight(dilationHeight)
{
weights.set_size((outSize * inSize * kernelWidth * kernelHeight) + outSize,
1);
weights.set_size(WeightSize(), 1);
// Transform paddingType to lowercase.
std::string paddingTypeLow = paddingType;
+7 -4
View File
@@ -165,9 +165,9 @@ class Concat
}
//! Return the initial point for the optimization.
const arma::mat& Parameters() const { return parameters; }
const arma::mat& Parameters() const { return weights; }
//! Modify the initial point for the optimization.
arma::mat& Parameters() { return parameters; }
arma::mat& Parameters() { return weights; }
//! Get the value of run parameter.
bool Run() const { return run; }
@@ -196,6 +196,9 @@ class Concat
//! Get the axis of concatenation.
size_t const& ConcatAxis() const { return axis; }
//! Get the size of the weight matrix.
size_t WeightSize() const { return 0; }
/**
* Serialize the layer
*/
@@ -225,8 +228,8 @@ class Concat
//! Locally-stored network modules.
std::vector<LayerTypes<CustomLayers...> > network;
//! Locally-stored model parameters.
arma::mat parameters;
//! Locally-stored model weights.
OutputDataType weights;
//! Locally-stored delta visitor.
DeltaVisitor deltaVisitor;
+2 -2
View File
@@ -33,7 +33,7 @@ Concat<InputDataType, OutputDataType, CustomLayers...>::Concat(
run(run),
channels(1)
{
parameters.set_size(0, 0);
weights.set_size(0, 0);
}
template<typename InputDataType, typename OutputDataType,
@@ -49,7 +49,7 @@ Concat<InputDataType, OutputDataType, CustomLayers...>::Concat(
model(model),
run(run)
{
parameters.set_size(0, 0);
weights.set_size(0, 0);
// Parameters to help calculate the number of channels.
size_t oldColSize = 1, newColSize = 1;
+6 -3
View File
@@ -115,9 +115,9 @@ class DropConnect
std::vector<LayerTypes<> >& Model() { return network; }
//! Get the parameters.
OutputDataType const& Parameters() const { return parameters; }
OutputDataType const& Parameters() const { return weights; }
//! Modify the parameters.
OutputDataType& Parameters() { return parameters; }
OutputDataType& Parameters() { return weights; }
//! Get the output parameter.
OutputDataType const& OutputParameter() const { return outputParameter; }
@@ -150,6 +150,9 @@ class DropConnect
scale = 1.0 / (1.0 - ratio);
}
//! Return the size of the weight matrix.
size_t WeightSize() const { return 0; }
/**
* Serialize the layer.
*/
@@ -164,7 +167,7 @@ class DropConnect
double scale;
//! Locally-stored weight object.
OutputDataType parameters;
OutputDataType weights;
//! Locally-stored delta object.
OutputDataType delta;
@@ -164,6 +164,12 @@ class FastLSTM
//! Get the number of output units.
size_t OutSize() const { return outSize; }
//! Get the size of the weight matrix.
size_t WeightSize() const
{
return 4 * outSize * inSize + 4 * outSize + 4 * outSize * outSize;
}
/**
* Serialize the layer
*/
@@ -42,8 +42,7 @@ FastLSTM<InputDataType, OutputDataType>::FastLSTM(
{
// Weights for: input to gate layer (4 * outsize * inSize + 4 * outsize)
// and output to gate (4 * outSize).
weights.set_size(
4 * outSize * inSize + 4 * outSize + 4 * outSize * outSize, 1);
weights.set_size(WeightSize(), 1);
}
template<typename InputDataType, typename OutputDataType>
+1 -1
View File
@@ -38,7 +38,7 @@ Linear<InputDataType, OutputDataType, RegularizerType>::Linear(
outSize(outSize),
regularizer(regularizer)
{
weights.set_size(outSize * inSize + outSize, 1);
weights.set_size(WeightSize(), 1);
}
template<typename InputDataType, typename OutputDataType,
+68 -5
View File
@@ -86,15 +86,78 @@ TEST_CASE("WeightSetVisitorTest", "[ANNVisitorTest]")
}
/**
* Test that WeightSizeVisitor works properly.
* Test that WeightSizeVisitor works properly for linear layer.
*/
TEST_CASE("WeightSizeVisitorTest", "[ANNVisitorTest]")
TEST_CASE("WeightSizeVisitorTestForLinearLayer", "[ANNVisitorTest]")
{
size_t randomSize = arma::randi(arma::distr_param(1, 100));
size_t randomInSize = arma::randi(arma::distr_param(1, 100));
size_t randomOutSize = arma::randi(arma::distr_param(1, 100));
LayerTypes<> linear = new Linear<>(randomSize, randomSize);
LayerTypes<> linearLayer = new Linear<>(randomInSize, randomOutSize);
CheckCorrectnessOfWeightSize(linear);
size_t weightSize = boost::apply_visitor(WeightSizeVisitor(), linearLayer);
CheckCorrectnessOfWeightSize(linearLayer);
}
/**
* Test that WeightSizeVisitor works properly for concat layer.
*/
TEST_CASE("WeightSizeVisitorTestForConcatLayer", "[ANNVisitorTest]")
{
LayerTypes<> concatLayer = new Concat<>();
size_t weightSize = boost::apply_visitor(WeightSizeVisitor(), concatLayer);
CheckCorrectnessOfWeightSize(concatLayer);
}
/**
* Test that WeightSizeVisitor works properly for fast lstm layer.
*/
TEST_CASE("WeightSizeVisitorTestForFastLSTMLayer", "[ANNVisitorTest]")
{
size_t randomInSize = arma::randi(arma::distr_param(1, 100));
size_t randomOutSize = arma::randi(arma::distr_param(1, 100));
LayerTypes<> fastLSTMLayer = new FastLSTM<>(randomInSize, randomOutSize);
size_t weightSize = boost::apply_visitor(WeightSizeVisitor(), fastLSTMLayer);
CheckCorrectnessOfWeightSize(fastLSTMLayer);
}
/**
* Test that WeightSizeVisitor works properly for Add layer.
*/
TEST_CASE("WeightSizeVisitorTestForAddLayer", "[ANNVisitorTest]")
{
size_t randomOutSize = arma::randi(arma::distr_param(1, 100));
LayerTypes<> addLayer = new Add<>(randomOutSize);
size_t weightSize = boost::apply_visitor(WeightSizeVisitor(), addLayer);
CheckCorrectnessOfWeightSize(addLayer);
}
/**
* Test that WeightSizeVisitor works properly for Atrous Convolution Layer.
*/
TEST_CASE("WeightSizeVisitorTestForAtrousConvolutionLayer", "[ANNVisitorTest]")
{
size_t randomInSize = arma::randi(arma::distr_param(1, 100));
size_t randomOutSize = arma::randi(arma::distr_param(1, 100));
size_t randomKernelWidth = arma::randi(arma::distr_param(1, 100));
size_t randomKernelHeight = arma::randi(arma::distr_param(1, 100));
LayerTypes<> atrousConvLayer = new AtrousConvolution<>(randomInSize, randomOutSize,
randomKernelWidth, randomKernelHeight);
size_t weightSize = boost::apply_visitor(WeightSizeVisitor(),
atrousConvLayer);
CheckCorrectnessOfWeightSize(atrousConvLayer);
}