0

我正在尝试修改Infer.NET 中的示例以使其更加灵活。我希望能够发送任意数量的指标来进行预测。

我创建了以下包装类(非常接近原始示例):

class Inference
{
    private readonly List<List<double>> _pastMetrics = new List<List<double>>();

    public void AddPastMetrics(List<double> pastMetrics)
    {
        _pastMetrics.Add(pastMetrics);
    }

    private readonly List<Boolean> _pastResults = new List<Boolean>();

    public void AddPastResults(Boolean pastResults)
    {
        _pastResults.Add(pastResults);
    }

    private readonly List<List<double>> _testMetrics = new List<List<double>>();

    public void AddTestMetrics(List<double> dayMetrics)
    {
        _testMetrics.Add(dayMetrics);
    }

    public Object GetInfer()
    {
        // Create x vector, augmented by 1
        Vector[] xdata = new Vector[_pastMetrics.Count];
        for (int i = 0; i < xdata.Length; i++)
            xdata[i] = Vector.FromList(_pastMetrics[i]);
        VariableArray<Vector> x = Variable.Observed(xdata);

        // Create target y
        VariableArray<bool> y = Variable.Observed(_pastResults.ToArray(), x.Range);
        var count = _pastMetrics.First().Count;
        Variable<Vector> w = Variable.Random(new VectorGaussian(Vector.Zero(count), PositiveDefiniteMatrix.Identity(count)));
        Range j = y.Range;
        double noise = 0.1;
        y[j] = Variable.GaussianFromMeanAndVariance(Variable.InnerProduct(w, x[j]), noise) > 0;

        InferenceEngine engine = new InferenceEngine(new ExpectationPropagation());
        VectorGaussian wPosterior = engine.Infer<VectorGaussian>(w);

        VariableArray<bool> ytest = Variable.Array<bool>(new Range(_pastMetrics.Count));
        BayesPointMachine(Variable.Random(wPosterior), ytest);
        return engine.Infer(ytest);

    }

    void BayesPointMachine(Variable<Vector> w, VariableArray<bool> y)
    {
        // Create x vector, augmented by 1
        Range j = y.Range;
        Vector[] xdata = new Vector[_testMetrics.Count];
        for (int i = 0; i < xdata.Length; i++)
            xdata[i] = Vector.FromList(_testMetrics[i]);
        VariableArray<Vector> x = Variable.Observed(xdata, j);

        // Bayes Point Machine
        double noise = 0.1;
        y[j] = Variable.GaussianFromMeanAndVariance(Variable.InnerProduct(w, x[j]), noise) > 0;
    }
}

这是主要程序:

static void Main()
{   
    var inf = new Inference();
    double[] incomes = { 63, 16, 28, 55, 22, 20 };
    double[] ages = { 38, 23, 40, 27, 18, 40 };
    for (int i = 0; i < incomes.Length; i++)
        inf.AddPastMetrics(new List<double> {incomes[i], ages[i]});

    double[] incomesTest = { 58, 18, 22 };
    double[] agesTest = { 36, 24, 37 };
    for (int i = 0; i < incomesTest.Length; i++)
        inf.AddTestMetrics(new List<double> { incomesTest[i], agesTest[i] });

    bool[] willBuy = { true, false, true, true, false, false };
    for (int i = 0; i < willBuy.Length; i++)
        inf.AddPastResults(willBuy[i]);

    Console.WriteLine("output=\n" + inf.GetInfer());

}

当我调用推断方法时,出现以下错误:

当变量“vVector__1”预期长度为 6 时,提供长度为 3 的数组

知道为什么吗?

4

1 回答 1

0

我忘了在数组中添加 1。从示例页面:

我们将使用这些数据来训练我们的贝叶斯点机器。为此,我们需要创建两个观察数组,一个称为 x,由输入特征的向量组成(通过附加 1 进行增强),另一个称为 y,它只包装了 willBuy。

于 2012-11-10T05:59:18.053 回答