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

How to create and use a HMM Dynamic Bayesian Network in Bayes Server?

我正在尝试在 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);

这里我什至不确定如何 "Unroll" 网络正确地获得预测以及分配给变量什么值 "time"。如果有人能阐明这个工具包的工作原理,我将不胜感激。谢谢

查看以下内容link:

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

隐马尔可夫模型(作为贝叶斯网络)具有离散的潜在变量和多个子节点。在 Bayes Server 中,您可以在一个子节点中组合多个变量,这与标准 HMM 非常相似。在 Bayes Server 中,您还可以混合和匹配 discrete/continuous 个节点,处理丢失的数据,并添加额外的结构(例如 HMM 和许多其他奇异模型的混合)。

关于预测,一旦你从上面的link构建了结构,https://www.bayesserver.com/code/

就有了一个DBN预测的例子

(注意你可以预测未来的单个变量(即使你有缺失数据),你可以预测未来的多个变量(联合概率),你可以预测异常的程度时间序列是(对数似然),对于离散(序列)预测,您可以预测最可能的序列。)

不清楚,ping Bayes Server Support,他们会为您添加示例。

代码看起来不错,除了网络结构,对于 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. 您可以将多个分布添加到 'inference.QueryDistributions' 并一次查询它们
  2. 虽然手动设置证据然后查询是完全有效的,但如果要对多条记录执行查询,请参阅 EvidenceReader、DataReader 和 DatabaseDataReader 或 DataTableDataReader。
  3. 检查 ParameterLearningOptions 上的 TimeSeriesMode
  4. 如果你想要 'Most probable explanation' 设置 queryOptions.Propagation = PropagationMethod.Max; // HMMs
  5. 的 Viterbi 算法的扩展