66
edits
Changes
m
Editing Code Examples
=== Loading Data in Intel DAAL ===
Setting Training and Testing Files:
<source lang=c++>
string trainDatasetFileName = "digits_tra.csv";
string trainGroundTruthFileName = "digits_tra_labels.csv";
string testDatasetFileName = "digits_tes.csv";
string testGroundTruthFileName = "digits_tes_labels.csv";
</source>
The object trainDataSource is a CSVFeatureManager that can load the data from a CSV file into memory.
<source lang=c++>
FileDataSource<CSVFeatureManager> trainDataSource(trainDatasetFileName,
DataSource::doAllocateNumericTable, DataSource::doDictionaryFromContext);
</source>
The data in memory would be stored as a numerical table. With the CSVFeatureManager, the table is automatically created.
Load data from the CSV file by calling the member function loadDataBlock().
<source lang=c++>
trainDataSource.loadDataBlock(nTrainObservations);
</source>
=== Training Data in Intel DAAL ===
With the training data in memory, DAAL can start use that data to train by passing it to an algorithm by the training data numeric table trainDataSource.getNumericTable()
First the creation of a training model must occur
<source lang=c++>
services::SharedPtr<svm::training::Batch<> > training(new
svm::training::Batch<>());
</source>
Create algorithm object for multi-class SVM training
<source lang=c++>
multi_class_classifier::training::Batch<> algorithm;
algorithm.parameter.nClasses = nClasses;
algorithm.parameter.training = training;
</source>
Pass training dataset and dependent values to the algorithm
<source lang=c++>
algorithm.input.set(classifier::training::data,
trainDataSource.getNumericTable());
</source>
Build multi-class SVM model by calling the SVM computation on algorithm
<source lang=c++>
algorithm.compute();
</source>
Retrieve algorithm results and place within trainingResult object
<source lang=c++>
trainingResult = algorithm.getResult();
</source>
Serialize the learned model into a disk file. The training data from trainingResult is written to the model.
<source lang=c++>
ModelFileWriter writer("./model");
writer.serializeToFile(trainingResult->get(classifier::training::model));
</source>
=== Testing The Trained Model ===
The prediction model is created
<source lang=c++>
services::SharedPtr<svm::prediction::Batch<> > prediction(new
svm::prediction::Batch<>());
<source>
Initialize testDataSource and load data from
/* Initialize FileDataSource<CSVFeatureManager> to retrieve the test data from .csv file */
FileDataSource<CSVFeatureManager> testDataSource(testDatasetFileName,
DataSource::doAllocateNumericTable,
DataSource::doDictionaryFromContext);
testDataSource.loadDataBlock(nTestObservations);
/* Create algorithm object for prediction of multi-class SVM values */
multi_class_classifier::prediction::Batch<> algorithm;
algorithm.parameter.prediction = prediction;
/* Pass testing dataset and trained model to the algorithm */
algorithm.input.set(classifier::prediction::data,
testDataSource.getNumericTable());
algorithm.input.set(classifier::prediction::model,
trainingResult->get(classifier::training::model));
/* Predict multi-class SVM values */
algorithm.compute();
/* Retrieve algorithm results */
predictionResult = algorithm.getResult();