[mlpack-git] [mlpack] ANN Saving the network and reloading (#531)
sudarshan
notifications at github.com
Tue Mar 1 07:43:17 EST 2016
Hey,
I'm working with the ANN module and have started with the feed_forward test. I'm trying to return the built network back to where I was calling it from so that I can use it there (and I was running into a lot of problems because of the template programming part more details [here] (http://stackoverflow.com/questions/35703764/c-return-from-template), which I was able to solve by compiling with -std=c++1y and return auto). However, when I tried to train the returned built network, it threw a matrix multiplication exception, which I'm assuming meant that something got corrupted while returning. So, I thought I could just train the network and save it, so that I could reload it later for prediction. The below does that and saves the network parameters in "test" file. The problem is while loading now I have to specify the type of the network (which was determined by decltype(modules) and decltype(classOutputLayer)). Furthermore, the XML file that was saved is just parameters, whic
h I'm gu
essing is the values of the weight matrix and does not indicate the type of the FFN. Is there a way to fix this problem?
```
#include <iostream>
#include <fstream>
#include <mlpack/core.hpp>
#include <mlpack/methods/ann/activation_functions/logistic_function.hpp>
#include <mlpack/methods/ann/activation_functions/tanh_function.hpp>
#include <mlpack/methods/ann/init_rules/random_init.hpp>
#include <mlpack/methods/ann/layer/bias_layer.hpp>
#include <mlpack/methods/ann/layer/linear_layer.hpp>
#include <mlpack/methods/ann/layer/base_layer.hpp>
#include <mlpack/methods/ann/layer/dropout_layer.hpp>
#include <mlpack/methods/ann/layer/binary_classification_layer.hpp>
#include <mlpack/methods/ann/ffn.hpp>
#include <mlpack/methods/ann/performance_functions/mse_function.hpp>
using namespace mlpack;
template <typename PerformanceFunction,
typename OutputLayerType,
typename PerformanceFunctionType,
typename MatType = arma::mat
>
void BuildFFN(MatType& trainData, MatType& trainLabels, MatType& testData, MatType& testLabels, const size_t hiddenLayerSize)
{
// input layer
ann::LinearLayer<> inputLayer(trainData.n_rows, hiddenLayerSize);
ann::BiasLayer<> inputBiasLayer(hiddenLayerSize);
ann::BaseLayer<PerformanceFunction> inputBaseLayer;
// hidden layer
ann::LinearLayer<> hiddenLayer1(hiddenLayerSize, trainLabels.n_rows);
ann::BiasLayer<> hiddenBiasLayer1(trainLabels.n_rows);
ann::BaseLayer<PerformanceFunction> outputLayer;
// output layer
OutputLayerType classOutputLayer;
auto modules = std::tie(inputLayer, inputBiasLayer, inputBaseLayer, hiddenLayer1, hiddenBiasLayer1, outputLayer);
ann::FFN<decltype(modules), decltype(classOutputLayer), ann::RandomInitialization, PerformanceFunctionType> net(modules, classOutputLayer);
net.Train(trainData, trainLabels);
//arma::mat prediction;
//net.Predict(testData, prediction);
std::ofstream ofs("test", std::ios::binary);
boost::archive::xml_oarchive o(ofs);
net.Serialize(o, 1);
//o << data::CreateNVP(net, "N");
//ofs.close();
//return net;
}
int main(int argc, char** argv)
{
arma::mat dataset;
data::Load("../data/thyroid_train.csv", dataset, true);
arma::mat trainData = dataset.submat(0, 0, dataset.n_rows - 4, dataset.n_cols - 1);
arma::mat trainLabels = dataset.submat(dataset.n_rows - 3, 0, dataset.n_rows - 1, dataset.n_cols - 1);
data::Load("../data/thyroid_test.csv", dataset, true);
arma::mat testData = dataset.submat(0, 0, dataset.n_rows - 4, dataset.n_cols - 1);
arma::mat testLabels = dataset.submat(dataset.n_rows - 3, 0, dataset.n_rows - 1, dataset.n_cols - 1);
std::cout << "Loaded the training and testing datasets" << std::endl;
const size_t hiddenLayerSize = 8;
//std::ifstream ifs("test2", std::ios::binary);
//boost::archive::xml_iarchive i(ifs);
//auto net = BuildFFN<ann::LogisticFunction, ann::BinaryClassificationLayer, ann::MeanSquaredErrorFunction>
//(trainData, trainLabels, testData, testLabels, hiddenLayerSize);
BuildFFN<ann::LogisticFunction, ann::BinaryClassificationLayer, ann::MeanSquaredErrorFunction>
(trainData, trainLabels, testData, testLabels, hiddenLayerSize);
//double classificationError;
//for (size_t i = 0; i < testData.n_cols; i++)
//{
//if (arma::sum(arma::sum(arma::abs(prediction.col(i) - testLabels.col(i)))) != 0)
//{
//classificationError++;
//}
//}
//std::cout << "Classification Error = " << (double(classificationError) / testData.n_cols) * 100 << "%" << std::endl;
return 0;
}
```
Thanks.
---
Reply to this email directly or view it on GitHub:
https://github.com/mlpack/mlpack/issues/531
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.cc.gatech.edu/pipermail/mlpack-git/attachments/20160301/cd7807f5/attachment-0001.html>
More information about the mlpack-git
mailing list