Why does this ML.NET code fail to predict the correct output?
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; }
}
}
c# ml.net
add a comment |
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; }
}
}
c# ml.net
1
Please provide a Complete, Minimal Verifyable example. I can not even get this code to compile, propably because you use very scenario specific types likeDataSourceCollection
. And so I would have to guess wich References I even need to add.
– Christopher
Nov 25 '18 at 23:20
My bad, I made the massive assumption that it would be obvious, given this is an ML.NET question, that the nuget package 'Microsoft.ML' would be needed. I've added the 'usings' to make it clearer.
– Andrew Bennett
Nov 26 '18 at 7:27
which version of ml.net are you using?
– c-chavez
Nov 29 '18 at 13:24
@c-chavez 4.7.0
– Andrew Bennett
Nov 29 '18 at 21:43
add a comment |
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; }
}
}
c# ml.net
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; }
}
}
c# ml.net
c# ml.net
edited Nov 26 '18 at 7:25
Andrew Bennett
asked Nov 25 '18 at 22:44
Andrew BennettAndrew Bennett
3111
3111
1
Please provide a Complete, Minimal Verifyable example. I can not even get this code to compile, propably because you use very scenario specific types likeDataSourceCollection
. And so I would have to guess wich References I even need to add.
– Christopher
Nov 25 '18 at 23:20
My bad, I made the massive assumption that it would be obvious, given this is an ML.NET question, that the nuget package 'Microsoft.ML' would be needed. I've added the 'usings' to make it clearer.
– Andrew Bennett
Nov 26 '18 at 7:27
which version of ml.net are you using?
– c-chavez
Nov 29 '18 at 13:24
@c-chavez 4.7.0
– Andrew Bennett
Nov 29 '18 at 21:43
add a comment |
1
Please provide a Complete, Minimal Verifyable example. I can not even get this code to compile, propably because you use very scenario specific types likeDataSourceCollection
. And so I would have to guess wich References I even need to add.
– Christopher
Nov 25 '18 at 23:20
My bad, I made the massive assumption that it would be obvious, given this is an ML.NET question, that the nuget package 'Microsoft.ML' would be needed. I've added the 'usings' to make it clearer.
– Andrew Bennett
Nov 26 '18 at 7:27
which version of ml.net are you using?
– c-chavez
Nov 29 '18 at 13:24
@c-chavez 4.7.0
– Andrew Bennett
Nov 29 '18 at 21:43
1
1
Please provide a Complete, Minimal Verifyable example. I can not even get this code to compile, propably because you use very scenario specific types like
DataSourceCollection
. And so I would have to guess wich References I even need to add.– Christopher
Nov 25 '18 at 23:20
Please provide a Complete, Minimal Verifyable example. I can not even get this code to compile, propably because you use very scenario specific types like
DataSourceCollection
. And so I would have to guess wich References I even need to add.– Christopher
Nov 25 '18 at 23:20
My bad, I made the massive assumption that it would be obvious, given this is an ML.NET question, that the nuget package 'Microsoft.ML' would be needed. I've added the 'usings' to make it clearer.
– Andrew Bennett
Nov 26 '18 at 7:27
My bad, I made the massive assumption that it would be obvious, given this is an ML.NET question, that the nuget package 'Microsoft.ML' would be needed. I've added the 'usings' to make it clearer.
– Andrew Bennett
Nov 26 '18 at 7:27
which version of ml.net are you using?
– c-chavez
Nov 29 '18 at 13:24
which version of ml.net are you using?
– c-chavez
Nov 29 '18 at 13:24
@c-chavez 4.7.0
– Andrew Bennett
Nov 29 '18 at 21:43
@c-chavez 4.7.0
– Andrew Bennett
Nov 29 '18 at 21:43
add a comment |
1 Answer
1
active
oldest
votes
Your Prediction
object is retrieving the original Label
column, instead of the output of the regressor.
Modify the code to be:
public class Prediction
{
[ColumnName("Score")] public float Output { get; set; }
}
Also note that, by choosing StochasticDualCoordinateAscentRegressor
, you are trying to fit a linear model (so, a linear function b + w1*x1 + w2*x2
to the output that is y = x1 XOR x2
. There is no linear function that will be close to XOR, and I won't be surprised at all if the learner converges to something arbitrary.
If, on the other hand, you used FastTreeRegressor
, you would be learning a decision tree, which will of course have no problem learning the XOR.
Absolutely brilliant. Thank you. I wanted to use a gradient descent approach solely because that's my 'HelloWorld' approach, but was fumbling in the dark because there're so many options with ML.NET. Looks like I picked the wrong one, but as you say, the FastTreeRegressor work well. Thanks again.
– Andrew Bennett
Dec 1 '18 at 20:58
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53472759%2fwhy-does-this-ml-net-code-fail-to-predict-the-correct-output%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your Prediction
object is retrieving the original Label
column, instead of the output of the regressor.
Modify the code to be:
public class Prediction
{
[ColumnName("Score")] public float Output { get; set; }
}
Also note that, by choosing StochasticDualCoordinateAscentRegressor
, you are trying to fit a linear model (so, a linear function b + w1*x1 + w2*x2
to the output that is y = x1 XOR x2
. There is no linear function that will be close to XOR, and I won't be surprised at all if the learner converges to something arbitrary.
If, on the other hand, you used FastTreeRegressor
, you would be learning a decision tree, which will of course have no problem learning the XOR.
Absolutely brilliant. Thank you. I wanted to use a gradient descent approach solely because that's my 'HelloWorld' approach, but was fumbling in the dark because there're so many options with ML.NET. Looks like I picked the wrong one, but as you say, the FastTreeRegressor work well. Thanks again.
– Andrew Bennett
Dec 1 '18 at 20:58
add a comment |
Your Prediction
object is retrieving the original Label
column, instead of the output of the regressor.
Modify the code to be:
public class Prediction
{
[ColumnName("Score")] public float Output { get; set; }
}
Also note that, by choosing StochasticDualCoordinateAscentRegressor
, you are trying to fit a linear model (so, a linear function b + w1*x1 + w2*x2
to the output that is y = x1 XOR x2
. There is no linear function that will be close to XOR, and I won't be surprised at all if the learner converges to something arbitrary.
If, on the other hand, you used FastTreeRegressor
, you would be learning a decision tree, which will of course have no problem learning the XOR.
Absolutely brilliant. Thank you. I wanted to use a gradient descent approach solely because that's my 'HelloWorld' approach, but was fumbling in the dark because there're so many options with ML.NET. Looks like I picked the wrong one, but as you say, the FastTreeRegressor work well. Thanks again.
– Andrew Bennett
Dec 1 '18 at 20:58
add a comment |
Your Prediction
object is retrieving the original Label
column, instead of the output of the regressor.
Modify the code to be:
public class Prediction
{
[ColumnName("Score")] public float Output { get; set; }
}
Also note that, by choosing StochasticDualCoordinateAscentRegressor
, you are trying to fit a linear model (so, a linear function b + w1*x1 + w2*x2
to the output that is y = x1 XOR x2
. There is no linear function that will be close to XOR, and I won't be surprised at all if the learner converges to something arbitrary.
If, on the other hand, you used FastTreeRegressor
, you would be learning a decision tree, which will of course have no problem learning the XOR.
Your Prediction
object is retrieving the original Label
column, instead of the output of the regressor.
Modify the code to be:
public class Prediction
{
[ColumnName("Score")] public float Output { get; set; }
}
Also note that, by choosing StochasticDualCoordinateAscentRegressor
, you are trying to fit a linear model (so, a linear function b + w1*x1 + w2*x2
to the output that is y = x1 XOR x2
. There is no linear function that will be close to XOR, and I won't be surprised at all if the learner converges to something arbitrary.
If, on the other hand, you used FastTreeRegressor
, you would be learning a decision tree, which will of course have no problem learning the XOR.
answered Dec 1 '18 at 17:22
ZrutyZruty
6,1021830
6,1021830
Absolutely brilliant. Thank you. I wanted to use a gradient descent approach solely because that's my 'HelloWorld' approach, but was fumbling in the dark because there're so many options with ML.NET. Looks like I picked the wrong one, but as you say, the FastTreeRegressor work well. Thanks again.
– Andrew Bennett
Dec 1 '18 at 20:58
add a comment |
Absolutely brilliant. Thank you. I wanted to use a gradient descent approach solely because that's my 'HelloWorld' approach, but was fumbling in the dark because there're so many options with ML.NET. Looks like I picked the wrong one, but as you say, the FastTreeRegressor work well. Thanks again.
– Andrew Bennett
Dec 1 '18 at 20:58
Absolutely brilliant. Thank you. I wanted to use a gradient descent approach solely because that's my 'HelloWorld' approach, but was fumbling in the dark because there're so many options with ML.NET. Looks like I picked the wrong one, but as you say, the FastTreeRegressor work well. Thanks again.
– Andrew Bennett
Dec 1 '18 at 20:58
Absolutely brilliant. Thank you. I wanted to use a gradient descent approach solely because that's my 'HelloWorld' approach, but was fumbling in the dark because there're so many options with ML.NET. Looks like I picked the wrong one, but as you say, the FastTreeRegressor work well. Thanks again.
– Andrew Bennett
Dec 1 '18 at 20:58
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53472759%2fwhy-does-this-ml-net-code-fail-to-predict-the-correct-output%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Please provide a Complete, Minimal Verifyable example. I can not even get this code to compile, propably because you use very scenario specific types like
DataSourceCollection
. And so I would have to guess wich References I even need to add.– Christopher
Nov 25 '18 at 23:20
My bad, I made the massive assumption that it would be obvious, given this is an ML.NET question, that the nuget package 'Microsoft.ML' would be needed. I've added the 'usings' to make it clearer.
– Andrew Bennett
Nov 26 '18 at 7:27
which version of ml.net are you using?
– c-chavez
Nov 29 '18 at 13:24
@c-chavez 4.7.0
– Andrew Bennett
Nov 29 '18 at 21:43