我是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; }
}
}
答案 0 :(得分:1)
您的Prediction
对象正在检索原始的Label
列,而不是返回变量的输出。
将代码修改为:
public class Prediction
{
[ColumnName("Score")] public float Output { get; set; }
}
还要注意,通过选择StochasticDualCoordinateAscentRegressor
,您正在尝试拟合线性模型(因此,将线性函数b + w1*x1 + w2*x2
拟合到y = x1 XOR x2
的输出中。没有线性函数这将接近XOR,而且如果学习者收敛到任意的东西,我也不会感到惊讶。
另一方面,如果您使用FastTreeRegressor
,则您将学习决策树,当然,学习XOR毫无问题。