为什么这个 ML.NET 代码无法预测正确的输出?

Why does this ML.NET code fail to predict the correct output?

我是 ML.NET 新手,想通过解决 XOR 问题进一步了解 ML.NET。这是我到目前为止的想法,但无论输入如何,输出总是相同(零)。

毫无疑问,我犯了一个菜鸟错误,但是呢?

using Microsoft.ML.Legacy;
using Microsoft.ML.Legacy.Data;
using Microsoft.ML.Legacy.Models;
using Microsoft.ML.Legacy.Trainers;
using Microsoft.ML.Legacy.Transforms;
using Microsoft.ML.Runtime.Api;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.ML.Runtime; 

public class Program
{
    static void Main(string[] args)
    {
        MlNet.Solve();
        Console.ReadLine();
    }
}

我是否使用了合适的回归量 (StochasticDualCoordinateAscentRegressor)?

public class MlNet
{
    public static void Solve()
    {
        var data = new List<Input>
        {
            new Input {Input1 = 0.0f, Input2 = 0.0f, Output = 0.0f},
            new Input {Input1 = 0.0f, Input2 = 1.0f, Output = 1.0f},
            new Input {Input1 = 1.0f, Input2 = 0.0f, Output = 1.0f},
            new Input {Input1 = 1.0f, Input2 = 1.0f, Output = 0.0f}
        };

        var largeSet = Enumerable.Repeat(data, 1000).SelectMany(a => a).ToList();
        var dataSource = CollectionDataSource.Create(largeSet.AsEnumerable());
        var pipeline = new LearningPipeline
        {
            dataSource,
            new ColumnConcatenator("Features", "Input1", "Input2"),
            new StochasticDualCoordinateAscentRegressor
            {
                LossFunction = new SquaredLossSDCARegressionLossFunction(),
                MaxIterations = 500,
                BiasLearningRate = 0.2f,
                Shuffle = true
            }
        };

        var model = pipeline.Train<Input, Prediction>();
        var evaluator = new RegressionEvaluator();
        var metrics = evaluator.Evaluate(model, dataSource);

        Console.WriteLine($"Accuracy: {Math.Round(metrics.Rms, 2)}");

        var prediction = model.Predict(new Input { Input1 = 0.0f, Input2 = 1.0f });

        Console.WriteLine($"Prediction: {prediction.Output}");
    }


    [DebuggerDisplay("Input1={Input1}, Input2={Input2}, Output={Output}")]
    public class Input
    {
        [Column("0", "Input1")] public float Input1 { get; set; }

        [Column("1", "Input2")] public float Input2 { get; set; }

        [Column("2", "Label")] public float Output { get; set; }
    }

    public class Prediction
    {
        [ColumnName("Label")] public float Output { get; set; }
    }
}

您的 Prediction 对象正在检索原始 Label 列,而不是回归器的输出。

修改代码为:

public class Prediction
{
    [ColumnName("Score")] public float Output { get; set; }
}

另请注意,通过选择 StochasticDualCoordinateAscentRegressor,您正在尝试拟合线性模型(因此,线性函数 b + w1*x1 + w2*x2y = x1 XOR x2 的输出。没有接近异或的线性函数,如果学习者收敛到任意值,我一点也不会感到惊讶。

另一方面,如果您使用 FastTreeRegressor,您将学习决策树,当然这在学习 XOR 时没有问题。