变量’vVector__1’期望长度为6,但提供了长度为3的数组

我正在尝试修改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()    {        // 创建x向量,并附加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);        // 创建目标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)    {        // 创建x向量,并附加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);        // 贝叶斯点机        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的数组

你知道这是为什么吗?


回答:

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

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

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注