-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Added samples for tree regression trainers. #2999
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.ML.Data; | ||
|
||
namespace Microsoft.ML.Samples.Dynamic.Trainers.Regression | ||
{ | ||
public static class FastForest | ||
{ | ||
// This example requires installation of additional NuGet package | ||
// <a href="https://www.nuget.org/packages/Microsoft.ML.FastTree/">Microsoft.ML.FastTree</a>. | ||
public static void Example() | ||
{ | ||
// Create a new context for ML.NET operations. It can be used for exception tracking and logging, | ||
// as a catalog of available operations and as the source of randomness. | ||
// Setting the seed to a fixed number in this example to make outputs deterministic. | ||
var mlContext = new MLContext(seed: 0); | ||
|
||
// Create a list of training examples. | ||
var examples = GenerateRandomDataPoints(1000); | ||
|
||
// Convert the examples list to an IDataView object, which is consumable by ML.NET API. | ||
var trainingData = mlContext.Data.LoadFromEnumerable(examples); | ||
|
||
// Define the trainer. | ||
var pipeline = mlContext.Regression.Trainers.FastForest(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe user wants to learn some little configuration. Could you somehow make it non-default? #Resolved There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had thought about this, and decided to keep it as defaults for the following reasons:
In reply to: 266665195 [](ancestors = 266665195) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there is another example in In reply to: 267017735 [](ancestors = 267017735,266665195) |
||
|
||
// Train the model. | ||
var model = pipeline.Fit(trainingData); | ||
|
||
// Create testing examples. Use different random seed to make it different from training data. | ||
var testData = mlContext.Data.LoadFromEnumerable(GenerateRandomDataPoints(500, seed:123)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
why are you setting the seed here, but not above? #Resolved There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've explained it in the comment line above. We want training and test set to be different. The first one uses default seed of 0. Here I use a different seed. In reply to: 266723117 [](ancestors = 266723117) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Can we be explicit to show the In reply to: 267018416 [](ancestors = 267018416,266723117) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think we're splitting hairs here. 123 is just a random number like saying ABC. In reply to: 267029054 [](ancestors = 267029054,267018416,266723117) |
||
|
||
// Run the model on test data set. | ||
var transformedTestData = model.Transform(testData); | ||
|
||
// Convert IDataView object to a list. | ||
var predictions = mlContext.Data.CreateEnumerable<Prediction>(transformedTestData, reuseRowObject: false).ToList(); | ||
|
||
// Look at 5 predictions | ||
foreach (var p in predictions.Take(5)) | ||
Console.WriteLine($"Label: {p.Label:F3}, Prediction: {p.Score:F3}"); | ||
|
||
// Expected output: | ||
// Label: 0.985, Prediction: 0.864 | ||
// Label: 0.155, Prediction: 0.164 | ||
// Label: 0.515, Prediction: 0.470 | ||
// Label: 0.566, Prediction: 0.501 | ||
// Label: 0.096, Prediction: 0.138 | ||
|
||
// Evaluate the overall metrics | ||
var metrics = mlContext.Regression.Evaluate(transformedTestData); | ||
SamplesUtils.ConsoleUtils.PrintMetrics(metrics); | ||
|
||
// Expected output: | ||
// Mean Absolute Error: 0.06 | ||
// Mean Squared Error: 0.01 | ||
// Root Mean Squared Error: 0.07 | ||
// RSquared: 0.93 | ||
} | ||
|
||
private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int seed=0) | ||
{ | ||
var random = new Random(seed); | ||
float randomFloat() => (float)random.NextDouble(); | ||
for (int i = 0; i < count; i++) | ||
{ | ||
var label = randomFloat(); | ||
yield return new DataPoint | ||
{ | ||
Label = label, | ||
// Create random features that are correlated with label. | ||
Features = Enumerable.Repeat(label, 50).Select(x => x + randomFloat()).ToArray() | ||
}; | ||
} | ||
} | ||
|
||
// Example with label and 50 feature values. A data set is a collection of such examples. | ||
private class DataPoint | ||
{ | ||
public float Label { get; set; } | ||
[VectorType(50)] | ||
public float[] Features { get; set; } | ||
} | ||
|
||
// Class used to capture predictions. | ||
private class Prediction | ||
{ | ||
// Original label. | ||
public float Label { get; set; } | ||
// Predicted score from the trainer. | ||
public float Score { get; set; } | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is going to be common piece of code in all the example (I assume). Did we think of templating it in some way so that in future if need to change it we can change it at one place? #Resolved There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.ML.Data; | ||
using Microsoft.ML.Trainers.FastTree; | ||
|
||
namespace Microsoft.ML.Samples.Dynamic.Trainers.Regression | ||
{ | ||
public static class FastForestWithOptions | ||
{ | ||
// This example requires installation of additional NuGet package | ||
// <a href="https://www.nuget.org/packages/Microsoft.ML.FastTree/">Microsoft.ML.FastTree</a>. | ||
public static void Example() | ||
{ | ||
// Create a new context for ML.NET operations. It can be used for exception tracking and logging, | ||
// as a catalog of available operations and as the source of randomness. | ||
// Setting the seed to a fixed number in this example to make outputs deterministic. | ||
var mlContext = new MLContext(seed: 0); | ||
|
||
// Create a list of training examples. | ||
var examples = GenerateRandomDataPoints(1000); | ||
|
||
// Convert the examples list to an IDataView object, which is consumable by ML.NET API. | ||
var trainingData = mlContext.Data.LoadFromEnumerable(examples); | ||
|
||
// Define trainer options. | ||
var options = new FastForestRegressionTrainer.Options | ||
{ | ||
// Only use 80% of features to reduce over-fitting. | ||
FeatureFraction = 0.8, | ||
// Create a simpler model by penalizing usage of new features. | ||
FeatureFirstUsePenalty = 0.1, | ||
// Reduce the number of trees to 50. | ||
NumberOfTrees = 50 | ||
}; | ||
|
||
// Define the trainer. | ||
var pipeline = mlContext.Regression.Trainers.FastForest(options); | ||
|
||
// Train the model. | ||
var model = pipeline.Fit(trainingData); | ||
|
||
// Create testing examples. Use different random seed to make it different from training data. | ||
var testData = mlContext.Data.LoadFromEnumerable(GenerateRandomDataPoints(500, seed:123)); | ||
|
||
// Run the model on test data set. | ||
var transformedTestData = model.Transform(testData); | ||
|
||
// Convert IDataView object to a list. | ||
var predictions = mlContext.Data.CreateEnumerable<Prediction>(transformedTestData, reuseRowObject: false).ToList(); | ||
|
||
// Look at 5 predictions | ||
foreach (var p in predictions.Take(5)) | ||
Console.WriteLine($"Label: {p.Label:F3}, Prediction: {p.Score:F3}"); | ||
|
||
// Expected output: | ||
// Label: 0.985, Prediction: 0.866 | ||
// Label: 0.155, Prediction: 0.171 | ||
// Label: 0.515, Prediction: 0.470 | ||
// Label: 0.566, Prediction: 0.476 | ||
// Label: 0.096, Prediction: 0.140 | ||
|
||
// Evaluate the overall metrics | ||
var metrics = mlContext.Regression.Evaluate(transformedTestData); | ||
SamplesUtils.ConsoleUtils.PrintMetrics(metrics); | ||
|
||
// Expected output: | ||
// Mean Absolute Error: 0.06 | ||
// Mean Squared Error: 0.01 | ||
// Root Mean Squared Error: 0.08 | ||
// RSquared: 0.93 | ||
} | ||
|
||
private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int seed=0) | ||
{ | ||
var random = new Random(seed); | ||
float randomFloat() => (float)random.NextDouble(); | ||
for (int i = 0; i < count; i++) | ||
{ | ||
var label = randomFloat(); | ||
yield return new DataPoint | ||
{ | ||
Label = label, | ||
// Create random features that are correlated with label. | ||
Features = Enumerable.Repeat(label, 50).Select(x => x + randomFloat()).ToArray() | ||
}; | ||
} | ||
} | ||
|
||
// Example with label and 50 feature values. A data set is a collection of such examples. | ||
private class DataPoint | ||
{ | ||
public float Label { get; set; } | ||
[VectorType(50)] | ||
public float[] Features { get; set; } | ||
} | ||
|
||
// Class used to capture predictions. | ||
private class Prediction | ||
{ | ||
// Original label. | ||
public float Label { get; set; } | ||
// Predicted score from the trainer. | ||
public float Score { get; set; } | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.ML.Data; | ||
|
||
namespace Microsoft.ML.Samples.Dynamic.Trainers.Regression | ||
{ | ||
public static class FastTreeTweedie | ||
{ | ||
// This example requires installation of additional NuGet package | ||
// <a href="https://www.nuget.org/packages/Microsoft.ML.FastTree/">Microsoft.ML.FastTree</a>. | ||
public static void Example() | ||
{ | ||
// Create a new context for ML.NET operations. It can be used for exception tracking and logging, | ||
// as a catalog of available operations and as the source of randomness. | ||
// Setting the seed to a fixed number in this example to make outputs deterministic. | ||
var mlContext = new MLContext(seed: 0); | ||
|
||
// Create a list of training examples. | ||
var examples = GenerateRandomDataPoints(1000); | ||
|
||
// Convert the examples list to an IDataView object, which is consumable by ML.NET API. | ||
var trainingData = mlContext.Data.LoadFromEnumerable(examples); | ||
|
||
// Define the trainer. | ||
var pipeline = mlContext.Regression.Trainers.FastTreeTweedie(); | ||
|
||
// Train the model. | ||
var model = pipeline.Fit(trainingData); | ||
|
||
// Create testing examples. Use different random seed to make it different from training data. | ||
var testData = mlContext.Data.LoadFromEnumerable(GenerateRandomDataPoints(500, seed:123)); | ||
|
||
// Run the model on test data set. | ||
var transformedTestData = model.Transform(testData); | ||
|
||
// Convert IDataView object to a list. | ||
var predictions = mlContext.Data.CreateEnumerable<Prediction>(transformedTestData, reuseRowObject: false).ToList(); | ||
|
||
// Look at 5 predictions | ||
foreach (var p in predictions.Take(5)) | ||
Console.WriteLine($"Label: {p.Label:F3}, Prediction: {p.Score:F3}"); | ||
|
||
// Expected output: | ||
// Label: 0.985, Prediction: 0.945 | ||
// Label: 0.155, Prediction: 0.104 | ||
// Label: 0.515, Prediction: 0.515 | ||
// Label: 0.566, Prediction: 0.448 | ||
// Label: 0.096, Prediction: 0.082 | ||
|
||
// Evaluate the overall metrics | ||
var metrics = mlContext.Regression.Evaluate(transformedTestData); | ||
SamplesUtils.ConsoleUtils.PrintMetrics(metrics); | ||
|
||
// Expected output: | ||
// Mean Absolute Error: 0.05 | ||
// Mean Squared Error: 0.00 | ||
// Root Mean Squared Error: 0.06 | ||
// RSquared: 0.95 | ||
} | ||
|
||
private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count, int seed=0) | ||
{ | ||
var random = new Random(seed); | ||
float randomFloat() => (float)random.NextDouble(); | ||
for (int i = 0; i < count; i++) | ||
{ | ||
var label = randomFloat(); | ||
yield return new DataPoint | ||
{ | ||
Label = label, | ||
// Create random features that are correlated with label. | ||
Features = Enumerable.Repeat(label, 50).Select(x => x + randomFloat()).ToArray() | ||
}; | ||
} | ||
} | ||
|
||
// Example with label and 50 feature values. A data set is a collection of such examples. | ||
private class DataPoint | ||
{ | ||
public float Label { get; set; } | ||
[VectorType(50)] | ||
public float[] Features { get; set; } | ||
} | ||
|
||
// Class used to capture predictions. | ||
private class Prediction | ||
{ | ||
// Original label. | ||
public float Label { get; set; } | ||
// Predicted score from the trainer. | ||
public float Score { get; set; } | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be xml doc. #Resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
xml looks busy, and it won't render as xml inside the example. This code goes in the example block.
In reply to: 266665386 [](ancestors = 266665386)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// xml are not used in .net examples, so we're sticking to // comments inside samples' code.
In reply to: 266722954 [](ancestors = 266722954,266665386)