#include "daal.h"
#include "service.h"
using namespace std;
using namespace daal;
using namespace daal::algorithms;
string trainedModelsFileName = "../data/batch/svm_two_class_trained_model.csv";
string testDatasetFileName = "../data/batch/svm_two_class_test_dense.csv";
const size_t nFeatures = 20;
const float bias = -0.562F;
kernel_function::KernelIfacePtr kernel(new kernel_function::linear::Batch<>());
NumericTablePtr testGroundTruth;
void testModel(svm::ModelPtr&);
svm::ModelPtr buildModelFromTraining();
int main(int argc, char *argv[])
{
checkArguments(argc, argv, 2, &trainedModelsFileName, &testDatasetFileName);
svm::ModelPtr builtModel = buildModelFromTraining();
testModel(builtModel);
return 0;
}
svm::ModelPtr buildModelFromTraining()
{
FileDataSource<CSVFeatureManager> modelSource(trainedModelsFileName,
DataSource::notAllocateNumericTable,
DataSource::doDictionaryFromContext);
NumericTablePtr supportVectors(new HomogenNumericTable<>(nFeatures, 0, NumericTable::doNotAllocate));
NumericTablePtr classificationCoefficients(new HomogenNumericTable<>(1, 0, NumericTable::doNotAllocate));
NumericTablePtr mergedModel(new MergedNumericTable(supportVectors, classificationCoefficients));
modelSource.loadDataBlock(mergedModel.get());
size_t nSV = supportVectors->getNumberOfRows();
svm::ModelBuilder<> modelBuilder(nFeatures, nSV);
BlockDescriptor<> blockResult;
supportVectors->getBlockOfRows(0, nSV, readOnly, blockResult);
float* first = blockResult.getBlockPtr();
float* last = first + nSV*nFeatures;
modelBuilder.setSupportVectors(first,last);
supportVectors->releaseBlockOfRows(blockResult);
classificationCoefficients->getBlockOfRows(0, nSV, readOnly, blockResult);
first = blockResult.getBlockPtr();
last = first + nSV;
modelBuilder.setClassificationCoefficients(first,last);
classificationCoefficients->releaseBlockOfRows(blockResult);
modelBuilder.setBias(bias);
return modelBuilder.getModel();
}
void testModel(svm::ModelPtr& inputModel)
{
FileDataSource<CSVFeatureManager> testDataSource(testDatasetFileName,
DataSource::notAllocateNumericTable,
DataSource::doDictionaryFromContext);
NumericTablePtr testData(new HomogenNumericTable<>(nFeatures, 0, NumericTable::doNotAllocate));
testGroundTruth = NumericTablePtr(new HomogenNumericTable<>(1, 0, NumericTable::doNotAllocate));
NumericTablePtr mergedData(new MergedNumericTable(testData, testGroundTruth));
testDataSource.loadDataBlock(mergedData.get());
svm::prediction::Batch<float> algorithm;
algorithm.parameter.kernel = kernel;
algorithm.input.set(classifier::prediction::data, testData);
algorithm.input.set(classifier::prediction::model, inputModel);
algorithm.compute();
printNumericTables<int, float>(testGroundTruth,
algorithm.getResult()->get(classifier::prediction::prediction),
"Ground truth", "Classification results",
"SVM classification sample program results (first 20 observations):", 20);
}