Merge pull request #3105 from shubham1206agra/cp-mv-cons

linear no bias copy and move constructor created
This commit is contained in:
Ryan Curtin
2022-01-18 14:01:47 -05:00
committed by GitHub
3 changed files with 151 additions and 0 deletions
@@ -51,6 +51,18 @@ class LinearNoBias
const size_t outSize,
RegularizerType regularizer = RegularizerType());
//! Copy constructor.
LinearNoBias(const LinearNoBias& layer);
//! Move constructor.
LinearNoBias(LinearNoBias&&);
//! Copy assignment operator.
LinearNoBias& operator=(const LinearNoBias& layer);
//! Move assignment operator.
LinearNoBias& operator=(LinearNoBias&& layer);
/*
* Reset the layer parameter.
*/
@@ -41,6 +41,62 @@ LinearNoBias<InputDataType, OutputDataType, RegularizerType>::LinearNoBias(
weights.set_size(WeightSize(), 1);
}
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)
{
// Nothing to do here.
}
template<typename InputDataType, typename OutputDataType,
typename RegularizerType>
LinearNoBias<InputDataType, OutputDataType, RegularizerType>::LinearNoBias(
LinearNoBias&& layer) :
inSize(0),
outSize(0),
weights(std::move(layer.weights)),
regularizer(std::move(layer.regularizer))
{
// Nothing to do here.
}
template<typename InputDataType, typename OutputDataType,
typename RegularizerType>
LinearNoBias<InputDataType, OutputDataType, RegularizerType>&
LinearNoBias<InputDataType, OutputDataType, RegularizerType>::
operator=(const LinearNoBias& layer)
{
if (this != &layer)
{
inSize = layer.inSize;
outSize = layer.outSize;
weights = layer.weights;
regularizer = layer.regularizer;
}
return *this;
}
template<typename InputDataType, typename OutputDataType,
typename RegularizerType>
LinearNoBias<InputDataType, OutputDataType, RegularizerType>&
LinearNoBias<InputDataType, OutputDataType, RegularizerType>::
operator=(LinearNoBias&& layer)
{
if (this != &layer)
{
inSize = layer.inSize;
outSize = layer.outSize;
weights = std::move(layer.weights);
regularizer = std::move(layer.regularizer);
}
return *this;
}
template<typename InputDataType, typename OutputDataType,
typename RegularizerType>
void LinearNoBias<InputDataType, OutputDataType, RegularizerType>::Reset()
@@ -386,6 +386,89 @@ TEST_CASE("CheckCopyMovingDropoutNetworkTest", "[FeedForwardNetworkTest]")
CheckMoveFunction<>(model1, trainData, trainLabels, 1);
}
/**
* Check whether copying and moving Vanila network is working or not.
*/
TEST_CASE("CheckCopyMovingVanillaNetworkTestNoBias", "[FeedForwardNetworkTest]")
{
// Load the dataset.
arma::mat trainData;
if (!data::Load("thyroid_train.csv", trainData))
FAIL("Cannot open thyroid_train.csv");
// Normalize labels to [0, 2].
arma::mat trainLabels = trainData.row(trainData.n_rows - 1) - 1;
trainData.shed_row(trainData.n_rows - 1);
/*
* Construct a feed forward network with trainData.n_rows input nodes,
* hiddenLayerSize hidden nodes and trainLabels.n_rows output nodes. The
* network structure looks like:
*
* Input Hidden Output
* Layer Layer Layer
* +-----+ +-----+ +-----+
* | | | | | |
* | +------>| +------>| |
* | | | | | |
* +-----+ +--+--+ +-----+
*/
FFN<NegativeLogLikelihood<> > *model = new FFN<NegativeLogLikelihood<> >;
model->Add<LinearNoBias<> >(trainData.n_rows, 8);
model->Add<SigmoidLayer<> >();
model->Add<LinearNoBias<> >(8, 3);
model->Add<LogSoftMax<> >();
FFN<NegativeLogLikelihood<> > *model1 = new FFN<NegativeLogLikelihood<> >;
model1->Add<LinearNoBias<> >(trainData.n_rows, 8);
model1->Add<SigmoidLayer<> >();
model1->Add<LinearNoBias<> >(8, 3);
model1->Add<LogSoftMax<> >();
// Check whether copy constructor is working or not.
CheckCopyFunction<>(model, trainData, trainLabels, 1);
// Check whether move constructor is working or not.
CheckMoveFunction<>(model1, trainData, trainLabels, 1);
}
/**
* Check whether copying and moving network with Reparametrization is working or not.
*/
TEST_CASE("CheckCopyMovingReparametrizationNetworkTestNoBias",
"[FeedForwardNetworkTest]")
{
// Load the dataset.
arma::mat trainData;
data::Load("thyroid_train.csv", trainData, true);
// Normalize labels to [0, 2].
arma::mat trainLabels = trainData.row(trainData.n_rows - 1) - 1;
trainData.shed_row(trainData.n_rows - 1);
/*
* Construct a feed forward network with trainData.n_rows input nodes,
* followed by a linear layer and then a reparametrization layer.
*/
FFN<NegativeLogLikelihood<> > *model = new FFN<NegativeLogLikelihood<> >;
model->Add<LinearNoBias<> >(trainData.n_rows, 8);
model->Add<Reparametrization<> >(4, false, true, 1);
model->Add<LogSoftMax<> >();
FFN<NegativeLogLikelihood<> > *model1 = new FFN<NegativeLogLikelihood<> >;
model1->Add<LinearNoBias<> >(trainData.n_rows, 8);
model1->Add<Reparametrization<> >(4, false, true, 1);
model1->Add<LogSoftMax<> >();
// Check whether copy constructor is working or not.
CheckCopyFunction<>(model, trainData, trainLabels, 1);
// Check whether move constructor is working or not.
CheckMoveFunction<>(model1, trainData, trainLabels, 1);
}
/**
* Train the vanilla network on a larger dataset.
*/