#include "daal.h"
#include "service.h"
#include "neural_net_dense_batch.h"
using namespace std;
using namespace daal;
using namespace daal::algorithms;
using namespace daal::algorithms::neural_networks;
using namespace daal::services;
string trainDatasetFile = "../data/batch/neural_network_train.csv";
string trainGroundTruthFile = "../data/batch/neural_network_train_ground_truth.csv";
string testDatasetFile = "../data/batch/neural_network_test.csv";
string testGroundTruthFile = "../data/batch/neural_network_test_ground_truth.csv";
prediction::ModelPtr predictionModel;
prediction::ResultPtr predictionResult;
void trainModel();
void testModel();
void printResults();
const size_t batchSize = 10;
int main()
{
trainModel();
testModel();
printResults();
return 0;
}
void trainModel()
{
TensorPtr trainingData = readTensorFromCSV(trainDatasetFile);
TensorPtr trainingGroundTruth = readTensorFromCSV(trainGroundTruthFile, true);
SharedPtr<optimization_solver::sgd::Batch<> > sgdAlgorithm(new optimization_solver::sgd::Batch<>());
float learningRate = 0.001f;
sgdAlgorithm->parameter.learningRateSequence = NumericTablePtr(new HomogenNumericTable<>(1, 1, NumericTable::doAllocate, learningRate));
sgdAlgorithm->parameter.batchSize = batchSize;
sgdAlgorithm->parameter.nIterations = trainingData->getDimensionSize(0) / sgdAlgorithm->parameter.batchSize;
training::Batch<> net(sgdAlgorithm);
training::TopologyPtr topology = configureNet();
services::Collection<size_t> oneBatchDimensions = trainingData->getDimensions();
oneBatchDimensions[0] = batchSize;
net.initialize(oneBatchDimensions, *topology);
net.input.set(training::data, trainingData);
net.input.set(training::groundTruth, trainingGroundTruth);
net.compute();
training::ModelPtr trainingModel = net.getResult()->get(training::model);
predictionModel = trainingModel->getPredictionModel<float>();
}
void testModel()
{
TensorPtr predictionData = readTensorFromCSV(testDatasetFile);
prediction::Batch<> net;
net.parameter.batchSize = predictionData->getDimensionSize(0);
net.input.set(prediction::model, predictionModel);
net.input.set(prediction::data, predictionData);
net.compute();
predictionResult = net.getResult();
}
void printResults()
{
TensorPtr predictionGroundTruth = readTensorFromCSV(testGroundTruthFile);
printTensors<int, float>(predictionGroundTruth, predictionResult->get(prediction::prediction),
"Ground truth", "Neural network predictions: each class probability",
"Neural network classification results (first 20 observations):", 20);
}