Skip to content

Commit 8675ec5

Browse files
authored
Add samples for MLContext.Model (root) and add links to documentation. (#3352)
* add links to code samples in documentation. * PR feedback.
1 parent 737af03 commit 8675ec5

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using Microsoft.ML;
5+
6+
namespace Samples.Dynamic.ModelOperations
7+
{
8+
public class SaveLoadModel
9+
{
10+
public static void Example()
11+
{
12+
// Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging,
13+
// as well as the source of randomness.
14+
var mlContext = new MLContext();
15+
16+
// Generate sample data.
17+
var data = new List<Data>()
18+
{
19+
new Data() { Value="abc" }
20+
};
21+
22+
// Convert data to IDataView.
23+
var dataView = mlContext.Data.LoadFromEnumerable(data);
24+
var inputColumnName = nameof(Data.Value);
25+
var outputColumnName = nameof(Transformation.Key);
26+
27+
// Transform.
28+
ITransformer model = mlContext.Transforms.Conversion.MapValueToKey(outputColumnName, inputColumnName).Fit(dataView);
29+
30+
// Save model.
31+
mlContext.Model.Save(model, dataView.Schema, "model.zip");
32+
33+
// Load model.
34+
using (var file = File.OpenRead("model.zip"))
35+
model = mlContext.Model.Load(file, out DataViewSchema schema);
36+
37+
// Create a prediction engine from the model for feeding new data.
38+
var engine = mlContext.Model.CreatePredictionEngine<Data, Transformation>(model);
39+
var transformation = engine.Predict(new Data() { Value = "abc" });
40+
41+
// Print transformation to console.
42+
Console.WriteLine("Value: {0}\t Key:{1}", transformation.Value, transformation.Key);
43+
// Value: abc Key:1
44+
45+
}
46+
47+
private class Data
48+
{
49+
public string Value { get; set; }
50+
}
51+
52+
private class Transformation
53+
{
54+
public string Value { get; set; }
55+
public uint Key { get; set; }
56+
}
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using Microsoft.ML;
5+
6+
namespace Samples.Dynamic.ModelOperations
7+
{
8+
public class SaveLoadModelFile
9+
{
10+
public static void Example()
11+
{
12+
// Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging,
13+
// as well as the source of randomness.
14+
var mlContext = new MLContext();
15+
16+
// Generate sample data.
17+
var data = new List<Data>()
18+
{
19+
new Data() { Value="abc" }
20+
};
21+
22+
// Convert data to IDataView.
23+
var dataView = mlContext.Data.LoadFromEnumerable(data);
24+
var inputColumnName = nameof(Data.Value);
25+
var outputColumnName = nameof(Transformation.Key);
26+
27+
// Transform.
28+
ITransformer model = mlContext.Transforms.Conversion.MapValueToKey(outputColumnName, inputColumnName).Fit(dataView);
29+
30+
// Save model.
31+
mlContext.Model.Save(model, dataView.Schema, "model.zip");
32+
33+
// Load model.
34+
model = mlContext.Model.Load("model.zip", out DataViewSchema schema);
35+
36+
// Create a prediction engine from the model for feeding new data.
37+
var engine = mlContext.Model.CreatePredictionEngine<Data, Transformation>(model);
38+
var transformation = engine.Predict(new Data() { Value = "abc" });
39+
40+
// Print transformation to console.
41+
Console.WriteLine("Value: {0}\t Key:{1}", transformation.Value, transformation.Key);
42+
// Value: abc Key:1
43+
44+
}
45+
46+
private class Data
47+
{
48+
public string Value { get; set; }
49+
}
50+
51+
private class Transformation
52+
{
53+
public string Value { get; set; }
54+
public uint Key { get; set; }
55+
}
56+
}
57+
}

src/Microsoft.ML.Data/Model/ModelOperationsCatalog.cs

+28
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ public void Save(ITransformer model, DataViewSchema inputSchema, Stream stream)
102102
/// be an empty <see cref="TransformerChain{TLastTransformer}"/>.</param>
103103
/// <param name="inputSchema">The schema of the input to the transformer. This can be <see langword="null"/>.</param>
104104
/// <param name="filePath">Path where model should be saved.</param>
105+
/// <example>
106+
/// <format type="text/markdown">
107+
/// <![CDATA[
108+
/// [!code-csharp[Save](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/ModelOperations/SaveLoadModel.cs)]
109+
/// ]]>
110+
/// </format>
111+
/// </example>
105112
public void Save(ITransformer model, DataViewSchema inputSchema, string filePath)
106113
{
107114
_env.CheckValueOrNull(model);
@@ -135,6 +142,13 @@ private void SaveInputSchema(DataViewSchema inputSchema, RepositoryWriter rep)
135142
/// <param name="inputSchema">Will contain the input schema for the model. If the model was saved without
136143
/// any description of the input, there will be no input schema. In this case this can be <see langword="null"/>.</param>
137144
/// <returns>The loaded model.</returns>
145+
/// <example>
146+
/// <format type="text/markdown">
147+
/// <![CDATA[
148+
/// [!code-csharp[Save](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/ModelOperations/SaveLoadModel.cs)]
149+
/// ]]>
150+
/// </format>
151+
/// </example>
138152
public ITransformer Load(Stream stream, out DataViewSchema inputSchema)
139153
{
140154
_env.CheckValue(stream, nameof(stream));
@@ -187,6 +201,13 @@ public ITransformer Load(Stream stream, out DataViewSchema inputSchema)
187201
/// <param name="inputSchema">Will contain the input schema for the model. If the model was saved without
188202
/// any description of the input, there will be no input schema. In this case this can be <see langword="null"/>.</param>
189203
/// <returns>The loaded model.</returns>
204+
/// <example>
205+
/// <format type="text/markdown">
206+
/// <![CDATA[
207+
/// [!code-csharp[Save](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/ModelOperations/SaveLoadModelFile.cs)]
208+
/// ]]>
209+
/// </format>
210+
/// </example>
190211
public ITransformer Load(string filePath, out DataViewSchema inputSchema)
191212
{
192213
_env.CheckNonEmpty(filePath, nameof(filePath));
@@ -281,6 +302,13 @@ public ITransformer LoadWithDataLoader(string filePath, out IDataLoader<IMultiSt
281302
/// <typeparamref name="TDst"/>.</param>
282303
/// <param name="inputSchemaDefinition">Additional settings of the input schema.</param>
283304
/// <param name="outputSchemaDefinition">Additional settings of the output schema.</param>
305+
/// <example>
306+
/// <format type="text/markdown">
307+
/// <![CDATA[
308+
/// [!code-csharp[Save](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/ModelOperations/SaveLoadModel.cs)]
309+
/// ]]>
310+
/// </format>
311+
/// </example>
284312
public PredictionEngine<TSrc, TDst> CreatePredictionEngine<TSrc, TDst>(ITransformer transformer,
285313
bool ignoreMissingColumns = true, SchemaDefinition inputSchemaDefinition = null, SchemaDefinition outputSchemaDefinition = null)
286314
where TSrc : class

0 commit comments

Comments
 (0)