Exemple #1
0
    public override void Train(
        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.Train(
            outputModelFileName: outputModelFileName,
            iterationCount: iterationCount,
            batchCount: batchCount);

        Train(
            outputModelFileName,
            iterationCount,
            computeModelEvidence,
            batchCount);
    }
Exemple #2
0
    public override void Train(
        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: " +
                           "batchCount.", false, true);

        // Validate
        _validate.Train(
            outputModelFileName: outputModelFileName,
            iterationCount: iterationCount,
            batchCount: batchCount);

        // The inference engine
        _engine[DistributionType.Prior] = SetInferenceEngine(
            inferenceEngineAlgorithm, iterationCount);

        // Initialise prior weights
        _w[DistributionType.Prior] = InitialiseWeights(
            distributionType: DistributionType.Prior,
            distributionName: distributionName,
            dimension: _numFeatures,
            hyperParameters: null);

        // BPM
        BayesPointMachine(_x, _y, _w[DistributionType.Prior], noise);
    }
Exemple #3
0
 public virtual void Predict(
     string inputModelFileName,
     DistributionName distributionName           = DistributionName.Null,
     InferenceAlgorithm inferenceEngineAlgorithm = InferenceAlgorithm.Null,
     double noise = 0)
 {
     TraceListeners.Log(TraceEventType.Information, 0,
                        "Predict...", false, true);
     _model.Predict(
         inputModelFileName,
         distributionName,
         inferenceEngineAlgorithm,
         noise);
 }
Exemple #4
0
 public virtual IDictionary <string, double> PredictInstance(
     string inputModelFileName,
     DistributionName distributionName,
     InferenceAlgorithm inferenceEngineAlgorithm,
     int instance,
     double noise = 0)
 {
     TraceListeners.Log(TraceEventType.Information, 0,
                        "PredictInstance...", false, true);
     return(_model.PredictInstance(
                inputModelFileName,
                distributionName,
                inferenceEngineAlgorithm,
                instance,
                noise));
 }
Exemple #5
0
    public override void Predict(
        string inputModelFileName,
        DistributionName distributionName,
        InferenceAlgorithm inferenceEngineAlgorithm,
        double noise)
    {
        // Validate
        // _validate.Predict(inputModelFileName);

        // Initialise
        List <IDictionary <string, double> > yPredicDistrib = new List <IDictionary <string, double> >();
        List <string>        yPredicLabel = new List <string>();
        VariableArray <bool> yTest        = Variable.Array <bool>(new Range(_numObservations)).
                                            Named("y." + _availableDatasetName.ToString()); // to do: correct the size

        Bernoulli[] yPredic;

        // The inference engine
        _engine[DistributionType.Posterior] = SetInferenceEngine(
            inferenceEngineAlgorithm, 1);

        // Infer postrior weights from training
        _w[DistributionType.Posterior] = InferWeights(
            distributionType: DistributionType.Posterior,
            distributionName: distributionName,
            hyperParameters: null);

        // BPM
        BayesPointMachine(_x, yTest, _w[DistributionType.Posterior], noise);

        // predict
        // _yPredicLabel = _engine[DistributionType.Posterior].Infer(_y);
        yPredic = _engine[DistributionType.Posterior].Infer <Bernoulli[]>(yTest);
        for (int i = 0; i < yPredic.Length; i++)
        {
            yPredicDistrib.Add(new Dictionary <string, double>()
            {
                { Convert.ToInt32(yPredic[i].GetProbFalse() > _cutoffPoint).ToString(), yPredic[i].GetProbFalse() },
                { Convert.ToInt32(yPredic[i].GetProbTrue() > _cutoffPoint).ToString(), yPredic[i].GetProbTrue() }
            });
            yPredicLabel.Add(Convert.ToInt32(yPredic[i].GetProbTrue() > _cutoffPoint).ToString());
        }
        _yPredicDistrib = yPredicDistrib;
        _yPredicLabel   = yPredicLabel;
    }
Exemple #6
0
 public virtual void Train(
     string outputModelFileName,
     int iterationCount,
     bool computeModelEvidence,
     int batchCount,
     DistributionName distributionName           = DistributionName.Null,
     InferenceAlgorithm inferenceEngineAlgorithm = InferenceAlgorithm.Null,
     double noise = 0)
 {
     TraceListeners.Log(TraceEventType.Information, 0,
                        "Train...", false, true);
     _model.Train(
         outputModelFileName,
         iterationCount,
         computeModelEvidence,
         batchCount,
         distributionName,
         inferenceEngineAlgorithm,
         noise);
 }
Exemple #7
0
    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);
    }
Exemple #8
0
    public override IDictionary <string, double> PredictInstance(
        string inputModelFileName,
        DistributionName distributionName,
        InferenceAlgorithm inferenceEngineAlgorithm,
        int instance,
        double noise)
    {
        // Validate
        // _validate.Predict(inputModelFileName);

        // Initialise
        Dictionary <string, double> yPredicDistrib = new Dictionary <string, double>();
        VariableArray <bool>        yTest          = Variable.Array <bool>(new Range(1)).
                                                     Named("y." + _availableDatasetName.ToString()); // to do: correct the size
        Bernoulli yPredic;

        // The inference engine
        _engine[DistributionType.Posterior] = SetInferenceEngine(
            inferenceEngineAlgorithm, 1);

        // Infer postrior weights from training
        _w[DistributionType.Posterior] = InferWeights(
            distributionType: DistributionType.Posterior,
            distributionName: distributionName,
            hyperParameters: null);

        // BPM
        BayesPointMachine(_x[instance], yTest[0], _w[DistributionType.Posterior], noise);

        // predict
        // _yPredicLabel = _engine[DistributionType.Posterior].Infer(_y);
        yPredic        = _engine[DistributionType.Posterior].Infer <Bernoulli>(yTest[0]);
        yPredicDistrib = new Dictionary <string, double>()
        {
            { Convert.ToInt32(yPredic.GetProbFalse() > _cutoffPoint).ToString(), yPredic.GetProbFalse() },
            { Convert.ToInt32(yPredic.GetProbTrue() > _cutoffPoint).ToString(), yPredic.GetProbTrue() }
        };

        return(yPredicDistrib);
    }
Exemple #9
0
    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);
    }
Exemple #10
0
    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);
    }
Exemple #11
0
    private InferenceEngine SetInferenceEngine(
        InferenceAlgorithm inferenceEngineAlgorithm,
        int iterationCount,
        bool ShowProgress = true,
        bool ShowTimings  = true)
    {
        InferenceEngine engine = null;

        switch (inferenceEngineAlgorithm)
        {
        case InferenceAlgorithm.EP:
            engine = new InferenceEngine(new ExpectationPropagation());
            engine.ShowProgress       = true;
            engine.ShowTimings        = true;
            engine.NumberOfIterations = iterationCount;
            break;

        default:
            TraceListeners.Log(TraceEventType.Error, 0,
                               "Invalid inference algorithm: " + inferenceEngineAlgorithm, true, true);
            break;
        }
        return(engine);
    }