【问题标题】:How to create and use a HMM Dynamic Bayesian Network in Bayes Server?如何在贝叶斯服务器中创建和使用 HMM 动态贝叶斯网络?
【发布时间】:2017-11-12 01:17:18
【问题描述】:

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

在这里,我什至不确定如何正确“展开”网络以获取预测以及为变量“时间”分配什么值。如果有人能阐明这个工具包的工作原理,我将不胜感激。谢谢。

【问题讨论】:

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


【解决方案1】:

代码除了网络结构外,否则应该为嗯,这应该看起来像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. 您可以将多个分布添加到“推理.QueryDistributions”并一次查询它们 Li>
  2. 虽然手动设置证据并查询,请参阅evidenEreader,dataReader和databaseDataReader或dataatableDataReader,如果要在多个记录上执行查询,则完全有效。
  3. 查看ParameterLearningOptions的TimeSeriesMode Li>
  4. 如果您希望“最可能的解释”设置QueryOptions.propagations = PropagationMethod.max; // HMMS的Viterbi算法的扩展名

【讨论】:

  • 你可以帮我解决这个@ span>
【解决方案2】:

查看以下链接:

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

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

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

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

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

【讨论】:

  • 感谢您提供信息,但我已经在他们的网站上,找不到足够的示例代码来构建我想要实现的模型。我也给他们发了邮件,我还在等待他们的回复
猜你喜欢
  • 2013-04-08
  • 2012-06-26
  • 2012-08-31
  • 2011-01-09
  • 2012-07-14
  • 2013-05-11
  • 2013-07-19
  • 2019-11-05
相关资源
最近更新 更多