Inheritance: ILearningTask
示例#1
0
        protected void DoLearning(LearningTask learningTask)
        {
            if (learningTask.LearningState == ComputationState.Done)
            {
                return;
            }

            // Grab options.
            var options = learningTask.Options;

            // Grab training set.
            var trainingSet = learningTask.TrainingSet;

            // Grab variable names.
            var firstObservation = learningTask.TrainingSet.First();
            var variableNames = firstObservation.VariableNames;

            // Build a starter Bayesian network.
            var bn = new BayesianNetwork(learningTask.TrainingSet.Name);

            // Build variables for each variable in the training set.
            foreach (var variableName in variableNames)
            {
                var space = trainingSet.Variables.TryFind(variableName).Value;
                var rv = new RandomVariable(variableName, space);
                bn.AddVariable(rv);
            }

            // Store in task so that task can send updates to its listeners.
            learningTask.BayesianNetwork = bn;
            learningTask.LearningState = ComputationState.Computing;

            // Build Dirichlet parameters
            IDictionary<string, DirichletDistribution> priors = new Dictionary<string, DirichletDistribution>();
            foreach (var variableName in variableNames)
            {
                var space = trainingSet.Variables.TryFind(variableName).Value;

                DirichletDistribution prior;
                if (options.DistributionDirichletAlpha > 0)
                {
                    prior = new DirichletDistribution();
                    foreach (var value in space.Values)
                    {
                        prior.SetParameter(value, options.DistributionDirichletAlpha);
                    }
                    priors[variableName] = prior;
                }
            }

            // Initialize a sufficient statistics.
            SufficientStatistics sufficientStatistics
                = new SufficientStatistics(trainingSet, Utils.Some(priors));

            // Learn structure.
            switch (options.Structure)
            {
                case LearningOptions.StructureEnum.DisconnectedStructure:
                    break;
                case LearningOptions.StructureEnum.RandomStructure:
                    bn.GenerateStructure(
                        StructureClass.Random,
                        Utils.Some(options.StructureSeed),
                        Utils.Some(options.StructureParentLimit));
                    break;
                case LearningOptions.StructureEnum.TreeStructure:
                    bn.LearnStructure(
                        sufficientStatistics,
                        StructureClass.Tree,
                        Utils.None<int>(),
                        Utils.None<int>());
                    break;
                case LearningOptions.StructureEnum.GeneralStructure:
                    bn.LearnStructure(
                        sufficientStatistics,
                        StructureClass.General,
                        Utils.None<int>(),
                        Utils.Some(options.StructureParentLimit));
                    break;
            }

            // Learn distributions.
            bn.LearnDistributions(sufficientStatistics);

            // Done.
            learningTask.LearningState = ComputationState.Done;
        }
示例#2
0
        internal void RequestTraining(IObservationSet trainingSet, LearningOptions options)
        {
            if (trainingSet == null)
            {
                throw new ArgumentNullException();
            }

            ILearningTask learningTask
                = new LearningTask(
                    Guid.NewGuid().ToString(),
                    trainingSet,
                    options);

            learningTask.BayesianNetworkStarted += OnLearningStarted;
            learningTask.BayesianNetworkFinished += OnLearningFinished;

            Model.LearningTasks.Clear();
            Model.LearningTasks.Add(learningTask);

            xLearningInspector.SetIsLearning(true);
        }
示例#3
0
        protected void DoLearning(LearningTask learningTask)
        {
            if (learningTask.LearningState == ComputationState.Done)
            {
                return;
            }

            // Grab options.
            var options = learningTask.Options;

            // Grab training set.
            var trainingSet = learningTask.TrainingSet;

            // Grab variable names.
            var firstObservation = learningTask.TrainingSet.First();
            var variableNames    = firstObservation.VariableNames;

            // Build a starter Bayesian network.
            var bn = new BayesianNetwork(learningTask.TrainingSet.Name);

            // Build variables for each variable in the training set.
            foreach (var variableName in variableNames)
            {
                var space = trainingSet.Variables.TryFind(variableName).Value;
                var rv    = new RandomVariable(variableName, space);
                bn.AddVariable(rv);
            }

            // Store in task so that task can send updates to its listeners.
            learningTask.BayesianNetwork = bn;
            learningTask.LearningState   = ComputationState.Computing;

            // Build Dirichlet parameters
            IDictionary <string, DirichletDistribution> priors = new Dictionary <string, DirichletDistribution>();

            foreach (var variableName in variableNames)
            {
                var space = trainingSet.Variables.TryFind(variableName).Value;

                DirichletDistribution prior;
                if (options.DistributionDirichletAlpha > 0)
                {
                    prior = new DirichletDistribution();
                    foreach (var value in space.Values)
                    {
                        prior.SetParameter(value, options.DistributionDirichletAlpha);
                    }
                    priors[variableName] = prior;
                }
            }

            // Initialize a sufficient statistics.
            SufficientStatistics sufficientStatistics
                = new SufficientStatistics(trainingSet, Utils.Some(priors));

            // Learn structure.
            switch (options.Structure)
            {
            case LearningOptions.StructureEnum.DisconnectedStructure:
                break;

            case LearningOptions.StructureEnum.RandomStructure:
                bn.GenerateStructure(
                    StructureClass.Random,
                    Utils.Some(options.StructureSeed),
                    Utils.Some(options.StructureParentLimit));
                break;

            case LearningOptions.StructureEnum.TreeStructure:
                bn.LearnStructure(
                    sufficientStatistics,
                    StructureClass.Tree,
                    Utils.None <int>(),
                    Utils.None <int>());
                break;

            case LearningOptions.StructureEnum.GeneralStructure:
                bn.LearnStructure(
                    sufficientStatistics,
                    StructureClass.General,
                    Utils.None <int>(),
                    Utils.Some(options.StructureParentLimit));
                break;
            }

            // Learn distributions.
            bn.LearnDistributions(sufficientStatistics);

            // Done.
            learningTask.LearningState = ComputationState.Done;
        }