I'm an ML.NET newbie and want to learn more about ML.NET by solving the XOR problem. This is what I've come up with so far, but the output always appears to be the same (zero), regardless of input.
No doubt I've made a rookie mistake, but what?
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();
}
}
Am I using a suitable regressor (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; }
}
}