如何在贝叶斯服务器中创建和使用HMM动态贝叶斯网络?

时间:2017-06-09 22:49:46

标签: c# naivebayes hidden-markov-models bayesian-networks

我试图在Bayes Server 7 C#中构建一个实现隐马尔可夫模型类型DBN的预测模块。我设法创建了网络结构,但我不确定它是否正确,因为他们的文档和示例不是很全面,我也不完全理解在训练后如何在代码中完成预测已经完成了。

以下是我的网络创建和培训代码的外观:

    var Feature1 = new Variable("Feature1", VariableValueType.Continuous);
    var Feature2 = new Variable("Feature2", VariableValueType.Continuous);
    var Feature3 = new Variable("Feature3", VariableValueType.Continuous);

    var nodeFeatures = new Node("Features", new Variable[] { Feature1, Feature2, Feature3 });
    nodeFeatures.TemporalType = TemporalType.Temporal;

    var nodeHypothesis = new Node(new Variable("Hypothesis", new string[] { "state1", "state2", "state3" }));
    nodeHypothesis.TemporalType = TemporalType.Temporal;

    // create network and add nodes
    var network = new Network();
    network.Nodes.Add(nodeHypothesis);
    network.Nodes.Add(nodeFeatures);

    // link the Hypothesis node to the Features node within each time slice
    network.Links.Add(new Link(nodeHypothesis, nodeFeatures));

    // add a temporal link of order 5.  This links the Hypothesis node to itself in the next time slice
    for (int order = 1; order <= 5; order++)
    {
        network.Links.Add(new Link(nodeHypothesis, nodeHypothesis, order));
    }

    var temporalDataReaderCommand = new DataTableDataReaderCommand(evidenceDataTable);
        var temporalReaderOptions = new TemporalReaderOptions("CaseId", "Index", TimeValueType.Value);

    // here we map variables to database columns
    // in this case the variables and database columns have the same name
    var temporalVariableReferences = new VariableReference[]
        {
            new VariableReference(Feature1, ColumnValueType.Value, Feature1.Name),
            new VariableReference(Feature2, ColumnValueType.Value, Feature2.Name),
            new VariableReference(Feature3, ColumnValueType.Value, Feature3.Name)
        };

    var evidenceReaderCommand = new EvidenceReaderCommand(
            temporalDataReaderCommand,
            temporalVariableReferences,
            temporalReaderOptions);

    // We will use the RelevanceTree algorithm here, as it is optimized for parameter learning
    var learning = new ParameterLearning(network, new RelevanceTreeInferenceFactory());
    var learningOptions = new ParameterLearningOptions();

    // Run the learning algorithm
    var result = learning.Learn(evidenceReaderCommand, learningOptions);

这是我的预测尝试:

    // we will now perform some queries on the network
    var inference = new RelevanceTreeInference(network);
    var queryOptions = new RelevanceTreeQueryOptions();
    var queryOutput = new RelevanceTreeQueryOutput();

    int time = 0;

    // query a probability variable 
    var queryHypothesis = new Table(nodeHypothesis, time);
    inference.QueryDistributions.Add(queryHypothesis);                

    double[] inputRow = GetInput();

    // set some temporal evidence
    inference.Evidence.Set(Feature1, inputRow[0], time);
    inference.Evidence.Set(Feature2, inputRow[1], time);
    inference.Evidence.Set(Feature3, inputRow[2], time);

    inference.Query(queryOptions, queryOutput);

    int hypothesizedClassId;
    var probability = queryHypothesis.GetMaxValue(out hypothesizedClassId);
    Console.WriteLine("hypothesizedClassId = {0}, score = {1}", hypothesizedClassId, probability);

在这里,我甚至不确定如何&#34;展开&#34;网络正确地得到预测和分配给变量的时间&#34;时间&#34;。如果有人能够了解这个工具包的工作原理,我将非常感激。感谢。

2 个答案:

答案 0 :(得分:0)

查看以下链接:

https://www.bayesserver.com/docs/modeling/time-series-model-types

隐马尔可夫模型(作为贝叶斯网络)具有离散的潜在变量和许多子节点。在Bayes Server中,您可以在子节点中组合多个变量,就像标准HMM一样。在Bayes Server中,您还可以混合和匹配离散/连续节点,处理丢失的数据,并添加其他结构(例如HMM和许多其他奇特模型的混合)。

关于预测,一旦从上面的链接构建了结构,就会在https://www.bayesserver.com/code/处有一个DBN预测示例

(请注意,您可以预测未来的个别变量(即使您缺少数据),您可以预测未来的多个变量(联合概率),您可以预测时间序列的异常程度( log-likelihood)和离散(序列)预测,你可以预测最可能的序列。)

目前尚不清楚,ping贝叶斯服务器支持,他们会为您添加一个示例。

答案 1 :(得分:0)

代码看起来很好,除了网络结构,HMM看起来应该是这样的(代码的唯一变化就是链接):

var Feature1 = new Variable("Feature1", VariableValueType.Continuous);
        var Feature2 = new Variable("Feature2", VariableValueType.Continuous);
        var Feature3 = new Variable("Feature3", VariableValueType.Continuous);

        var nodeFeatures = new Node("Features", new Variable[] { Feature1, Feature2, Feature3 });
        nodeFeatures.TemporalType = TemporalType.Temporal;

        var nodeHypothesis = new Node(new Variable("Hypothesis", new string[] { "state1", "state2", "state3" }));
        nodeHypothesis.TemporalType = TemporalType.Temporal;

        // create network and add nodes
        var network = new Network();
        network.Nodes.Add(nodeHypothesis);
        network.Nodes.Add(nodeFeatures);

        // link the Hypothesis node to the Features node within each time slice
        network.Links.Add(new Link(nodeHypothesis, nodeFeatures));

        // An HMM also has an order 1 link on the latent node
        network.Links.Add(new Link(nodeHypothesis, nodeHypothesis, 1));

值得注意的还有以下几点:

  1. 您可以向&quot; inference.QueryDistributions&#39;添加多个发行版。并立即查询所有内容
  2. 虽然手动设置证据然后查询完全有效,但如果要在多个记录上执行查询,请参阅EvidenceReader,DataReader以及DatabaseDataReader或DataTableDataReader。
  3. 查看ParameterLearningOptions上的TimeSeriesMode
  4. 如果您想要最可能的解释&#39; set queryOptions.Propagation = PropagationMethod.Max; // HMM的维特比算法的扩展