Merge branch 'zoq-ann_model'

This commit is contained in:
Ryan Curtin
2019-10-05 09:47:42 -04:00
3 changed files with 46 additions and 0 deletions
+1
View File
@@ -1,5 +1,6 @@
### mlpack ?.?.?
###### ????-??-??
* Add Model() to the FFN class to access individual layers (#2043).
### mlpack 3.2.1
###### 2019-10-01
+8
View File
@@ -257,6 +257,14 @@ class FFN
*/
void Add(LayerTypes<CustomLayers...> layer) { network.push_back(layer); }
//! Get the network model.
const std::vector<LayerTypes<CustomLayers...> >& Model() const
{
return network;
}
//! Modify the network model.
std::vector<LayerTypes<CustomLayers...> >& Model() { return network; }
//! Return the number of separable functions (the number of predictor points).
size_t NumFunctions() const { return numFunctions; }
@@ -575,4 +575,41 @@ BOOST_AUTO_TEST_CASE(FFNTrainReturnObjective)
BOOST_REQUIRE_EQUAL(std::isfinite(objVal), true);
}
/**
* Test that FFN::Model() allows us to access the instantiated network.
*/
BOOST_AUTO_TEST_CASE(FFNReturnModel)
{
// Create dummy network.
FFN<NegativeLogLikelihood<> > model;
Linear<>* linearA = new Linear<>(3, 3);
model.Add(linearA);
Linear<>* linearB = new Linear<>(3, 4);
model.Add(linearB);
// Initialize network parameter.
model.ResetParameters();
// Set all network parameter to one.
model.Parameters().ones();
// Zero the second layer parameter.
linearB->Parameters().zeros();
// Get the layer parameter from layer A and layer B and store them in
// parameterA and parameterB.
arma::mat parameterA, parameterB;
boost::apply_visitor(ParametersVisitor(std::move(parameterA)),
model.Model()[0]);
boost::apply_visitor(ParametersVisitor(std::move(parameterB)),
model.Model()[1]);
CheckMatrices(parameterA, arma::ones(3 * 3 + 3, 1));
CheckMatrices(parameterB, arma::zeros(3 * 4 + 4, 1));
CheckMatrices(linearA->Parameters(), arma::ones(3 * 3 + 3, 1));
CheckMatrices(linearB->Parameters(), arma::zeros(3 * 4 + 4, 1));
}
BOOST_AUTO_TEST_SUITE_END();