Changes

Jump to: navigation, search

Alpha Centauri

1,936 bytes added, 17:59, 21 December 2017
Done with code example - next is image setting
</source>
The object trainDataSource is a CSVFeatureManager that can load the data from a CSV file into memory.Setting Training and Prediction Models
<source lang=c++>
FileDataSourceservices::SharedPtr<svm::training::Batch<CSVFeatureManager> trainDataSource> training(trainDatasetFileName,new svm::training::Batch<>()); DataSourceservices::doAllocateNumericTable, DataSourceSharedPtr<svm::prediction::Batch<> > prediction(new svm::prediction:doDictionaryFromContext:Batch<>());
</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().Setting Training and Prediction Algorithm Models
<source lang=c++>
trainDataSource.loadDataBlockservices::SharedPtr<multi_class_classifier::training::Result> trainingResult;services::SharedPtr<classifier::prediction::Result> predictionResult;</source> Setting up Kernel Function Parameters for Multi-Class Classifier<source lang=c++>kernel_function::rbf::Batch<> *rbfKernel = new kernel_function::rbf::Batch<>(nTrainObservations);services::SharedPtr<kernel_function::KernelIface> kernel(rbfKernel);services::SharedPtr<multi_class_classifier::quality_metric_set::ResultCollection> qualityMetricSetResult;</source> Initializing Numeric Tables for Predicted and Ground Truth<source lang=c++>services::SharedPtr<NumericTable> predictedLabels;services::SharedPtr<NumericTable> groundTruthLabels;
</source>
 
=== Training Data in Intel DAAL ===
With the training data in memory, DAAL can start use that data to train by passing it Initialize FileDataSource<CSVFeatureManager> to an algorithm by the training retrieve input data numeric table from .csv file <source lang=c++>FileDataSource<CSVFeatureManager> trainDataSource.getNumericTable(trainDatasetFileName, DataSource::doAllocateNumericTable, DataSource::doDictionaryFromContext);FileDataSource<CSVFeatureManager> trainGroundTruthSource(trainGroundTruthFileName, DataSource::doAllocateNumericTable, DataSource::doDictionaryFromContext);</source>
First the creation of a training model must occurLoad data from files
<source lang=c++>
services::SharedPtr<svm::training::Batch<> > trainingtrainDataSource.loadDataBlock(newnTrainObservations); svm::training::Batch<>trainGroundTruthSource.loadDataBlock()nTrainObservations);
</source>
Create Initialize 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 Setting algorithmparameters
<source lang=c++>
algorithm.inputparameter.nClasses = nClasses;algorithm.parameter.set(classifier::training::data,= training; trainDataSourcealgorithm.parameter.getNumericTable())prediction = prediction;
</source>
Build multi-class SVM model by calling Pass dependent parameters and training data to the SVM computation on algorithm
<source lang=c++>
algorithm.computeinput.set(classifier::training::data, trainDataSource.getNumericTable());algorithm.input.set(classifier::training::labels, trainGroundTruthSource.getNumericTable());
</source>
Retrieve Retrieving results from 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 createdInitialize testDataSource to retrieve test data from a .csv file
<source lang=c++>
services::SharedPtr<svm::prediction::BatchFileDataSource<CSVFeatureManager> > predictiontestDataSource(newtestDatasetFileName, svm DataSource::predictiondoAllocateNumericTable, DataSource::Batch<>doDictionaryFromContext);testDataSource.loadDataBlock()nTestObservations);</source>
Initialize testDataSource and load data from algorithm object for prediction of SVM values/* Initialize FileDataSource<CSVFeatureManager> to retrieve the test data from .csv file */ FileDataSource<CSVFeatureManagersource lang=c++> testDataSource(testDatasetFileName, DataSourcemulti_class_classifier::doAllocateNumericTable, DataSourceprediction::doDictionaryFromContext)Batch<> algorithm; testDataSource.loadDataBlock(nTestObservations);</source>
/* Create Setting algorithm object for prediction of multi-class SVM values */parameters multi_class_classifier::prediction::Batch<source lang=c++> algorithm.parameter.nClasses = nClasses;algorithm.parameter.training = training;algorithm.parameter.prediction = prediction;</source>
Pass into the algorithm the testing data and trained model<source lang=c++>algorithm.parameterinput.set(classifier::prediction = ::data, testDataSource.getNumericTable());algorithm.input.set(classifier::prediction::model,trainingResult->get(classifier::training::model));</source>
/* Pass testing dataset and trained model to the Retrieve results from algorithm */ <source lang=c++>predictionResult = algorithm.inputgetResult();</source>  === Testing the Quality of the Model ===Initialize testGroundTruth to retrieve ground truth test data from .setcsv file<source lang=c++>FileDataSource<CSVFeatureManager> testGroundTruth(classifiertestGroundTruthFileName, DataSource::predictiondoAllocateNumericTable, DataSource::data,doDictionaryFromContext); testDataSourcetestGroundTruth.getNumericTableloadDataBlock()nTestObservations); algorithm.input.set(classifier::prediction::model,</source> Retrieve label for ground truth trainingResult-<source lang=c++>getgroundTruthLabels = testGroundTruth.getNumericTable(classifier::training::model));</source>
/* Predict multiRetrieve prediction label<source lang=c++>predictedLabels = predictionResult-class SVM values */ algorithm.compute>get(classifier::prediction::prediction);</source>
/* Retrieve Create quality metric object to quantitate quality metrics of the classifier algorithm results */ predictionResult <source lang=c++>multi_class_classifier::quality_metric_set::Batch qualityMetricSet(nClasses);services::SharedPtr<multiclass_confusion_matrix::Input> input = algorithm qualityMetricSet.getResultgetInputDataCollection()->getInput(multi_class_classifier::quality_metric_set::confusionMatrix);input->set(multiclass_confusion_matrix::predictedLabels, predictedLabels);input->set(multiclass_confusion_matrix::groundTruthLabels, groundTruthLabels);</source>
Compute quality
<source lang=c++>
qualityMetricSet.compute();
</source>
 
Retrieve quality results
<source lang=c++>
qualityMetricSetResult = qualityMetricSet.getResultCollection();
</source>
=== SVM Digit Recognition Code Example Output ===
== Sources ==
66
edits

Navigation menu