/// <summary> /// Runs the module. /// </summary> /// <param name="args">The command line arguments for the module.</param> /// <param name="usagePrefix">The prefix to print before the usage string.</param> /// <returns>True if the run was successful, false otherwise.</returns> public override bool Run(string[] args, string usagePrefix) { string testSetFile = string.Empty; string modelFile = string.Empty; string predictionsFile = string.Empty; var parser = new CommandLineParser(); parser.RegisterParameterHandler("--test-set", "FILE", "File with test data", v => testSetFile = v, CommandLineParameterType.Required); parser.RegisterParameterHandler("--model", "FILE", "File with a trained binary Bayes point machine model", v => modelFile = v, CommandLineParameterType.Required); parser.RegisterParameterHandler("--predictions", "FILE", "File to store predictions for the test data", v => predictionsFile = v, CommandLineParameterType.Required); if (!parser.TryParse(args, usagePrefix)) { return(false); } var testSet = ClassifierPersistenceUtils.LoadLabeledFeatureValues(testSetFile); BayesPointMachineClassifierModuleUtilities.WriteDataSetInfo(testSet); var classifier = BayesPointMachineClassifier.LoadBinaryClassifier <IList <LabeledFeatureValues>, LabeledFeatureValues, IList <LabelDistribution>, string, IDictionary <string, double> >(modelFile); // Predict labels var predictions = classifier.PredictDistribution(testSet); // Write labels to file ClassifierPersistenceUtils.SaveLabelDistributions(predictionsFile, predictions); return(true); }
public override void Evaluate( string inputModelFileName, string reportFileName, string positiveClassLabel, string groundTruthFileName, string predictionsFileName, string weightsFileName, string calibrationCurveFileName, string precisionRecallCurveFileName, string rocCurveFileName) { IBayesPointMachineClassifier < IList <Vector>, int, IList <string>, string, IDictionary <string, double>, BayesPointMachineClassifierTrainingSettings, BinaryBayesPointMachineClassifierPredictionSettings <string> > classifier = null; // Validate _validate.Evaluate( inputModelFileName: inputModelFileName, reportFileName: reportFileName, groundTruthFileName: groundTruthFileName, predictionsFileName: predictionsFileName, weightsFileName: weightsFileName, calibrationCurveFileName: calibrationCurveFileName, precisionRecallCurveFileName: precisionRecallCurveFileName, rocCurveFileName: rocCurveFileName); // Load model if (string.IsNullOrEmpty(inputModelFileName)) { classifier = BayesPointMachineClassifier.LoadBinaryClassifier < IList <Vector>, int, IList <string>, string, IDictionary <string, double> > (inputModelFileName); } else { classifier = _classifier; } EvaluationReportsMapped evaluationReports = new EvaluationReportsMapped( classifier, _evaluator, _x, _y, _yPredicDistrib, _yPredicLabel, reportFileName, positiveClassLabel, groundTruthFileName, predictionsFileName, weightsFileName, calibrationCurveFileName, precisionRecallCurveFileName, rocCurveFileName); }
public override IDictionary <string, double> PredictInstance( string inputModelFileName, DistributionName distributionName, InferenceAlgorithm inferenceEngineAlgorithm, int instance, double noise) { TraceListeners.Log(TraceEventType.Warning, 0, "Advanced setting will not be used: " + "distributionName, inferenceEngineAlgorithm & noise.", false, true); // Validate _validate.PredictInstance( inputModelFileName: inputModelFileName, instance: instance, numObservations: _numObservations); IBayesPointMachineClassifier < IList <Vector>, int, IList <string>, string, IDictionary <string, double>, BayesPointMachineClassifierTrainingSettings, BinaryBayesPointMachineClassifierPredictionSettings <string> > classifier = null; // Load model if (string.IsNullOrEmpty(inputModelFileName)) { classifier = BayesPointMachineClassifier.LoadBinaryClassifier < IList <Vector>, int, IList <string>, string, IDictionary <string, double> > (inputModelFileName); } else { classifier = _classifier; } IDictionary <string, double> yPredicted = classifier.PredictDistribution(instance, _x); // string yPredicLabel = classifier.Predict(instance, _x); return(yPredicted); }
/// <summary> /// Runs the module. /// </summary> /// <param name="args">The command line arguments for the module.</param> /// <param name="usagePrefix">The prefix to print before the usage string.</param> /// <returns>True if the run was successful, false otherwise.</returns> public override bool Run(string[] args, string usagePrefix) { string modelFile = string.Empty; string samplesFile = string.Empty; var parser = new CommandLineParser(); parser.RegisterParameterHandler("--model", "FILE", "File with a trained binary Bayes point machine model", v => modelFile = v, CommandLineParameterType.Required); parser.RegisterParameterHandler("--samples", "FILE", "File to store samples of the weights", v => samplesFile = v, CommandLineParameterType.Required); if (!parser.TryParse(args, usagePrefix)) { return(false); } var classifier = BayesPointMachineClassifier.LoadBinaryClassifier <IList <LabeledFeatureValues>, LabeledFeatureValues, IList <LabelDistribution>, string, IDictionary <string, double> >(modelFile); BayesPointMachineClassifierModuleUtilities.SampleWeights(classifier, samplesFile); return(true); }
public override void TrainIncremental( string inputModelFileName, string outputModelFileName, int iterationCount, bool computeModelEvidence, int batchCount, DistributionName distributionName, InferenceAlgorithm inferenceEngineAlgorithm, double noise) { TraceListeners.Log(TraceEventType.Warning, 0, "Advanced setting will not be used: " + "distributionName, inferenceEngineAlgorithm & noise.", false, true); // Validate _validate.TrainIncremental( inputModelFileName: inputModelFileName, outputModelFileName: outputModelFileName, iterationCount: iterationCount, batchCount: batchCount); // Load model IBayesPointMachineClassifier < IList <Vector>, int, IList <string>, string, IDictionary <string, double>, BayesPointMachineClassifierTrainingSettings, BinaryBayesPointMachineClassifierPredictionSettings <string> > classifier = BayesPointMachineClassifier.LoadBinaryClassifier < IList <Vector>, int, IList <string>, string, IDictionary <string, double> > (inputModelFileName); // Set settings classifier.Settings.Training.ComputeModelEvidence = computeModelEvidence; classifier.Settings.Training.IterationCount = iterationCount; classifier.Settings.Training.BatchCount = batchCount; // train classifier.TrainIncremental(_x, _y); classifier.Save(outputModelFileName); }
public override void Predict( string inputModelFileName, DistributionName distributionName, InferenceAlgorithm inferenceEngineAlgorithm, double noise) { TraceListeners.Log(TraceEventType.Warning, 0, "Advanced setting will not be used: " + "distributionName, inferenceEngineAlgorithm & noise.", false, true); // Validate _validate.Predict(inputModelFileName); // Define the classifier IBayesPointMachineClassifier < IList <Vector>, int, IList <string>, string, IDictionary <string, double>, BayesPointMachineClassifierTrainingSettings, BinaryBayesPointMachineClassifierPredictionSettings <string> > classifier = null; // Load model if (string.IsNullOrEmpty(inputModelFileName)) { classifier = BayesPointMachineClassifier.LoadBinaryClassifier < IList <Vector>, int, IList <string>, string, IDictionary <string, double> > (inputModelFileName); } else { classifier = _classifier; } _validate.ValidatePredict(_x, _x); _yPredicDistrib = classifier.PredictDistribution(_x); _yPredicLabel = classifier.Predict(_x); }
/// <summary> /// Runs the module. /// </summary> /// <param name="args">The command line arguments for the module.</param> /// <param name="usagePrefix">The prefix to print before the usage string.</param> /// <returns>True if the run was successful, false otherwise.</returns> public override bool Run(string[] args, string usagePrefix) { string trainingSetFile = string.Empty; string inputModelFile = string.Empty; string outputModelFile = string.Empty; int iterationCount = BayesPointMachineClassifierTrainingSettings.IterationCountDefault; int batchCount = BayesPointMachineClassifierTrainingSettings.BatchCountDefault; var parser = new CommandLineParser(); parser.RegisterParameterHandler("--training-set", "FILE", "File with training data", v => trainingSetFile = v, CommandLineParameterType.Required); parser.RegisterParameterHandler("--input-model", "FILE", "File with the trained binary Bayes point machine model", v => inputModelFile = v, CommandLineParameterType.Required); parser.RegisterParameterHandler("--model", "FILE", "File to store the incrementally trained binary Bayes point machine model", v => outputModelFile = v, CommandLineParameterType.Required); parser.RegisterParameterHandler("--iterations", "NUM", "Number of training algorithm iterations (defaults to " + iterationCount + ")", v => iterationCount = v, CommandLineParameterType.Optional); parser.RegisterParameterHandler("--batches", "NUM", "Number of batches to split the training data into (defaults to " + batchCount + ")", v => batchCount = v, CommandLineParameterType.Optional); if (!parser.TryParse(args, usagePrefix)) { return(false); } var trainingSet = ClassifierPersistenceUtils.LoadLabeledFeatureValues(trainingSetFile); BayesPointMachineClassifierModuleUtilities.WriteDataSetInfo(trainingSet); var classifier = BayesPointMachineClassifier.LoadBinaryClassifier <IList <LabeledFeatureValues>, LabeledFeatureValues, IList <LabelDistribution>, string, IDictionary <string, double> >(inputModelFile); classifier.Settings.Training.IterationCount = iterationCount; classifier.Settings.Training.BatchCount = batchCount; classifier.TrainIncremental(trainingSet); classifier.Save(outputModelFile); return(true); }