Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,32 @@ namespace Samples.Dynamic.Trainers.Recommendation
public static class MatrixFactorization
{

// This example requires installation of additional nuget package <a href="https://www.nuget.org/packages/Microsoft.ML.Recommender/">Microsoft.ML.Recommender</a>.
// In this example we will create in-memory data and then use it to train
// a matrix factorization model with default parameters. Afterward, quality metrics are reported.
// This example requires installation of additional nuget package at
// for Microsoft.ML.Recommender at
// https://www.nuget.org/packages/Microsoft.ML.Recommender/
// In this example we will create in-memory data and then use it to train
// a matrix factorization model with default parameters. Afterward, quality
// metrics are reported.
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.
// 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 data points.
var dataPoints = GenerateMatrix();

// Convert the list of data points to an IDataView object, which is consumable by ML.NET API.
// Convert the list of data points to an IDataView object, which is
// consumable by ML.NET API.
var trainingData = mlContext.Data.LoadFromEnumerable(dataPoints);

// Define the trainer.
var pipeline = mlContext.Recommendation().Trainers.MatrixFactorization(nameof(MatrixElement.Value), nameof(MatrixElement.MatrixColumnIndex),
nameof(MatrixElement.MatrixRowIndex), 10, 0.2, 1);
var pipeline = mlContext.Recommendation().Trainers.
MatrixFactorization(nameof(MatrixElement.Value),
nameof(MatrixElement.MatrixColumnIndex),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 This is incorrectly indented

nameof(MatrixElement.MatrixRowIndex), 10, 0.2, 1);

// Train the model.
var model = pipeline.Fit(trainingData);
Expand All @@ -36,11 +43,15 @@ public static void Example()
var transformedData = model.Transform(trainingData);

// Convert IDataView object to a list.
var predictions = mlContext.Data.CreateEnumerable<MatrixElement>(transformedData, reuseRowObject: false).Take(5).ToList();
var predictions = mlContext.Data
.CreateEnumerable<MatrixElement>(transformedData,
reuseRowObject: false).Take(5).ToList();
Copy link
Contributor

@sharwell sharwell Jul 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 This layout is very difficult to read. The following would be preferable:

var predictions = mlContext.Data
    .CreateEnumerable<MatrixElement>(
        transformedData,
        reuseRowObject: false)
    .Take(5)
    .ToList();


// Look at 5 predictions for the Label, side by side with the actual Label for comparison.
// Look at 5 predictions for the Label, side by side with the actual
// Label for comparison.
foreach (var p in predictions)
Console.WriteLine($"Actual value: {p.Value:F3}, Predicted score: {p.Score:F3}");
Console.WriteLine($"Actual value: {p.Value:F3}," +
$"Predicted score: {p.Score:F3}");

// Expected output:
// Actual value: 0.000, Predicted score: 1.234
Expand All @@ -50,7 +61,10 @@ public static void Example()
// Actual value: 4.000, Predicted score: 2.362

// Evaluate the overall metrics
var metrics = mlContext.Regression.Evaluate(transformedData, labelColumnName: nameof(MatrixElement.Value), scoreColumnName: nameof(MatrixElement.Score));
var metrics = mlContext.Regression.Evaluate(transformedData,
labelColumnName: nameof(MatrixElement.Value),
scoreColumnName: nameof(MatrixElement.Score));

PrintMetrics(metrics);

// Expected output:
Expand All @@ -60,11 +74,15 @@ public static void Example()
// RSquared: 0.61 (closer to 1 is better. The worest case is 0)
}

// The following variables are used to define the shape of the example matrix. Its shape is MatrixRowCount-by-MatrixColumnCount.
// Because in ML.NET key type's minimal value is zero, the first row index is always zero in C# data structure (e.g., MatrixColumnIndex=0
// and MatrixRowIndex=0 in MatrixElement below specifies the value at the upper-left corner in the training matrix). If user's row index
// starts with 1, their row index 1 would be mapped to the 2nd row in matrix factorization module and their first row may contain no values.
// This behavior is also true to column index.
// The following variables are used to define the shape of the example
// matrix. Its shape is MatrixRowCount-by-MatrixColumnCount. Because in
// ML.NET key type's minimal value is zero, the first row index is always
// zero in C# data structure (e.g., MatrixColumnIndex=0 and MatrixRowIndex=0
// in MatrixElement below specifies the value at the upper-left corner in
// the training matrix). If user's row index starts with 1, their row index
// 1 would be mapped to the 2nd row in matrix factorization module and their
// first row may contain no values. This behavior is also true to column
// index.
private const uint MatrixColumnCount = 60;
private const uint MatrixRowCount = 100;

Expand All @@ -74,32 +92,40 @@ private static List<MatrixElement> GenerateMatrix()
var dataMatrix = new List<MatrixElement>();
for (uint i = 0; i < MatrixColumnCount; ++i)
for (uint j = 0; j < MatrixRowCount; ++j)
dataMatrix.Add(new MatrixElement() { MatrixColumnIndex = i, MatrixRowIndex = j, Value = (i + j) % 5 });
dataMatrix.Add(new MatrixElement() { MatrixColumnIndex = i,
MatrixRowIndex = j, Value = (i + j) % 5 });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Incorrect line wrapping and indentation


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty line. Is it produced by an auto-formatting tool?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, Zeeshan asked us to do that, so that's what we've been doing as a norm.

return dataMatrix;
}

// A class used to define a matrix element and capture its prediction result.
// A class used to define a matrix element and capture its prediction
// result.
private class MatrixElement
{
// Matrix column index. Its allowed range is from 0 to MatrixColumnCount - 1.
// Matrix column index. Its allowed range is from 0 to
// MatrixColumnCount - 1.
[KeyType(MatrixColumnCount)]
public uint MatrixColumnIndex { get; set; }
// Matrix row index. Its allowed range is from 0 to MatrixRowCount - 1.
[KeyType(MatrixRowCount)]
public uint MatrixRowIndex { get; set; }
// The actual value at the MatrixColumnIndex-th column and the MatrixRowIndex-th row.
// The actual value at the MatrixColumnIndex-th column and the
// MatrixRowIndex-th row.
public float Value { get; set; }
// The predicted value at the MatrixColumnIndex-th column and the MatrixRowIndex-th row.
// The predicted value at the MatrixColumnIndex-th column and the
// MatrixRowIndex-th row.
public float Score { get; set; }
}

// Print some evaluation metrics to regression problems.
private static void PrintMetrics(RegressionMetrics metrics)
{
Console.WriteLine($"Mean Absolute Error: {metrics.MeanAbsoluteError:F2}");
Console.WriteLine($"Mean Squared Error: {metrics.MeanSquaredError:F2}");
Console.WriteLine($"Root Mean Squared Error: {metrics.RootMeanSquaredError:F2}");
Console.WriteLine($"RSquared: {metrics.RSquared:F2}");
Console.WriteLine("Mean Absolute Error: " + metrics.MeanAbsoluteError);
Console.WriteLine("Mean Squared Error: " + metrics.MeanSquaredError);
Console.WriteLine("Root Mean Squared Error: " +
metrics.RootMeanSquaredError);

Console.WriteLine("RSquared: " + metrics.RSquared);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

<#+
string ClassHeader = @"
// This example requires installation of additional nuget package <a href=""https://www.nuget.org/packages/Microsoft.ML.Recommender/"">Microsoft.ML.Recommender</a>.
// In this example we will create in-memory data and then use it to train
// a matrix factorization model with default parameters. Afterward, quality metrics are reported.";
// This example requires installation of additional nuget package at
// for Microsoft.ML.Recommender at
// https://www.nuget.org/packages/Microsoft.ML.Recommender/
// In this example we will create in-memory data and then use it to train
// a matrix factorization model with default parameters. Afterward, quality
// metrics are reported.";
string ClassName="MatrixFactorization";
string ExtraUsing = null;
string Trainer = @"MatrixFactorization(nameof(MatrixElement.Value), nameof(MatrixElement.MatrixColumnIndex),
nameof(MatrixElement.MatrixRowIndex), 10, 0.2, 1)";
string Trainer = @"
MatrixFactorization(nameof(MatrixElement.Value),
nameof(MatrixElement.MatrixColumnIndex),
nameof(MatrixElement.MatrixRowIndex), 10, 0.2, 1)";
string TrainerOptions = null;

string ExpectedOutputPerInstance= @"// Expected output:
Expand All @@ -23,4 +28,4 @@ string ExpectedOutput = @"// Expected output:
// Mean Squared Error: 0.79
// Root Mean Squared Error: 0.89
// RSquared: 0.61 (closer to 1 is better. The worest case is 0)";
#>
#>
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ namespace Samples.Dynamic.Trainers.Recommendation
<# } #>
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.
// 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 data points.
var dataPoints = GenerateMatrix();

// Convert the list of data points to an IDataView object, which is consumable by ML.NET API.
// Convert the list of data points to an IDataView object, which is
// consumable by ML.NET API.
var trainingData = mlContext.Data.LoadFromEnumerable(dataPoints);

<# if (TrainerOptions == null) { #>
Expand All @@ -35,7 +37,8 @@ namespace Samples.Dynamic.Trainers.Recommendation
var options = new <#=TrainerOptions#>;

// Define the trainer.
var pipeline = mlContext.Recommendation().Trainers.<#=Trainer#>(options);
var pipeline = mlContext.Recommendation().Trainers.<#=Trainer#>(
options);
<# } #>

// Train the model.
Expand All @@ -45,26 +48,37 @@ namespace Samples.Dynamic.Trainers.Recommendation
var transformedData = model.Transform(trainingData);

// Convert IDataView object to a list.
var predictions = mlContext.Data.CreateEnumerable<MatrixElement>(transformedData, reuseRowObject: false).Take(5).ToList();
var predictions = mlContext.Data
.CreateEnumerable<MatrixElement>(transformedData,
reuseRowObject: false).Take(5).ToList();

// Look at 5 predictions for the Label, side by side with the actual Label for comparison.
// Look at 5 predictions for the Label, side by side with the actual
// Label for comparison.
foreach (var p in predictions)
Console.WriteLine($"Actual value: {p.Value:F3}, Predicted score: {p.Score:F3}");
Console.WriteLine($"Actual value: {p.Value:F3}," +
$"Predicted score: {p.Score:F3}");

<#=ExpectedOutputPerInstance#>

// Evaluate the overall metrics
var metrics = mlContext.Regression.Evaluate(transformedData, labelColumnName: nameof(MatrixElement.Value), scoreColumnName: nameof(MatrixElement.Score));
var metrics = mlContext.Regression.Evaluate(transformedData,
labelColumnName: nameof(MatrixElement.Value),
scoreColumnName: nameof(MatrixElement.Score));

PrintMetrics(metrics);

<#=ExpectedOutput#>
}

// The following variables are used to define the shape of the example matrix. Its shape is MatrixRowCount-by-MatrixColumnCount.
// Because in ML.NET key type's minimal value is zero, the first row index is always zero in C# data structure (e.g., MatrixColumnIndex=0
// and MatrixRowIndex=0 in MatrixElement below specifies the value at the upper-left corner in the training matrix). If user's row index
// starts with 1, their row index 1 would be mapped to the 2nd row in matrix factorization module and their first row may contain no values.
// This behavior is also true to column index.
// The following variables are used to define the shape of the example
// matrix. Its shape is MatrixRowCount-by-MatrixColumnCount. Because in
// ML.NET key type's minimal value is zero, the first row index is always
// zero in C# data structure (e.g., MatrixColumnIndex=0 and MatrixRowIndex=0
// in MatrixElement below specifies the value at the upper-left corner in
// the training matrix). If user's row index starts with 1, their row index
// 1 would be mapped to the 2nd row in matrix factorization module and their
// first row may contain no values. This behavior is also true to column
// index.
private const uint MatrixColumnCount = 60;
private const uint MatrixRowCount = 100;

Expand All @@ -74,32 +88,40 @@ namespace Samples.Dynamic.Trainers.Recommendation
var dataMatrix = new List<MatrixElement>();
for (uint i = 0; i < MatrixColumnCount; ++i)
for (uint j = 0; j < MatrixRowCount; ++j)
dataMatrix.Add(new MatrixElement() { MatrixColumnIndex = i, MatrixRowIndex = j, Value = (i + j) % 5 });
dataMatrix.Add(new MatrixElement() { MatrixColumnIndex = i,
MatrixRowIndex = j, Value = (i + j) % 5 });

return dataMatrix;
}

// A class used to define a matrix element and capture its prediction result.
// A class used to define a matrix element and capture its prediction
// result.
private class MatrixElement
{
// Matrix column index. Its allowed range is from 0 to MatrixColumnCount - 1.
// Matrix column index. Its allowed range is from 0 to
// MatrixColumnCount - 1.
[KeyType(MatrixColumnCount)]
public uint MatrixColumnIndex { get; set; }
// Matrix row index. Its allowed range is from 0 to MatrixRowCount - 1.
[KeyType(MatrixRowCount)]
public uint MatrixRowIndex { get; set; }
// The actual value at the MatrixColumnIndex-th column and the MatrixRowIndex-th row.
// The actual value at the MatrixColumnIndex-th column and the
// MatrixRowIndex-th row.
public float Value { get; set; }
// The predicted value at the MatrixColumnIndex-th column and the MatrixRowIndex-th row.
// The predicted value at the MatrixColumnIndex-th column and the
// MatrixRowIndex-th row.
public float Score { get; set; }
}

// Print some evaluation metrics to regression problems.
private static void PrintMetrics(RegressionMetrics metrics)
{
Console.WriteLine($"Mean Absolute Error: {metrics.MeanAbsoluteError:F2}");
Console.WriteLine($"Mean Squared Error: {metrics.MeanSquaredError:F2}");
Console.WriteLine($"Root Mean Squared Error: {metrics.RootMeanSquaredError:F2}");
Console.WriteLine($"RSquared: {metrics.RSquared:F2}");
Console.WriteLine("Mean Absolute Error: " + metrics.MeanAbsoluteError);
Console.WriteLine("Mean Squared Error: " + metrics.MeanSquaredError);
Console.WriteLine("Root Mean Squared Error: " +
metrics.RootMeanSquaredError);

Console.WriteLine("RSquared: " + metrics.RSquared);
}
}
}
Loading