Skip to content

Commit 0153754

Browse files
author
Rayan-Krishnan
authored
Reformatting Featurization of Transform and Misc files in Transform to Width 85 (dotnet#3950)
* transform/treefeaturization formatted to 85 char * transform files not in folder formatted to 35 char
1 parent 541d268 commit 0153754

40 files changed

+1306
-641
lines changed

docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/ApplyONNXModelWithInMemoryImages.cs

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,61 @@ public static void Example()
1515
// Download the squeeznet image model from ONNX model zoo, version 1.2
1616
// https://github.com/onnx/models/tree/master/squeezenet or use
1717
// Microsoft.ML.Onnx.TestModels nuget.
18-
// It's a multiclass classifier. It consumes an input "data_0" and produces
19-
// an output "softmaxout_1".
18+
// It's a multiclass classifier. It consumes an input "data_0" and
19+
// produces an output "softmaxout_1".
2020
var modelPath = @"squeezenet\00000001\model.onnx";
2121

2222
// Create ML pipeline to score the data using OnnxScoringEstimator
2323
var mlContext = new MLContext();
2424

25-
// Create in-memory data points. Its Image/Scores field is the input/output of the used ONNX model.
25+
// Create in-memory data points. Its Image/Scores field is the
26+
// input /output of the used ONNX model.
2627
var dataPoints = new ImageDataPoint[]
2728
{
2829
new ImageDataPoint(Color.Red),
2930
new ImageDataPoint(Color.Green)
3031
};
3132

32-
// Convert training data to IDataView, the general data type used in ML.NET.
33+
// Convert training data to IDataView, the general data type used in
34+
// ML.NET.
3335
var dataView = mlContext.Data.LoadFromEnumerable(dataPoints);
3436

35-
// Create a ML.NET pipeline which contains two steps. First, ExtractPixle is used to convert the 224x224 image to a 3x224x224 float tensor.
36-
// Then the float tensor is fed into a ONNX model with an input called "data_0" and an output called "softmaxout_1". Note that "data_0" and
37-
// "softmaxout_1" are model input and output names stored in the used ONNX model file. Users may need to inspect their own models to
38-
// get the right input and output column names.
39-
var pipeline = mlContext.Transforms.ExtractPixels("data_0", "Image") // Map column "Image" to column "data_0"
40-
.Append(mlContext.Transforms.ApplyOnnxModel("softmaxout_1", "data_0", modelPath)); // Map column "data_0" to column "softmaxout_1"
37+
// Create a ML.NET pipeline which contains two steps. First,
38+
// ExtractPixle is used to convert the 224x224 image to a 3x224x224
39+
// float tensor. Then the float tensor is fed into a ONNX model with an
40+
// input called "data_0" and an output called "softmaxout_1". Note that
41+
// "data_0" and "softmaxout_1" are model input and output names stored
42+
// in the used ONNX model file. Users may need to inspect their own
43+
// models to get the right input and output column names.
44+
// Map column "Image" to column "data_0"
45+
// Map column "data_0" to column "softmaxout_1"
46+
var pipeline = mlContext.Transforms.ExtractPixels("data_0", "Image")
47+
.Append(mlContext.Transforms.ApplyOnnxModel("softmaxout_1",
48+
"data_0", modelPath));
49+
4150
var model = pipeline.Fit(dataView);
4251
var onnx = model.Transform(dataView);
4352

44-
// Convert IDataView back to IEnumerable<ImageDataPoint> so that user can inspect the output, column "softmaxout_1", of the ONNX transform.
45-
// Note that Column "softmaxout_1" would be stored in ImageDataPont.Scores because the added attributed [ColumnName("softmaxout_1")]
46-
// tells that ImageDataPont.Scores is equivalent to column "softmaxout_1".
47-
var transformedDataPoints = mlContext.Data.CreateEnumerable<ImageDataPoint>(onnx, false).ToList();
53+
// Convert IDataView back to IEnumerable<ImageDataPoint> so that user
54+
// can inspect the output, column "softmaxout_1", of the ONNX transform.
55+
// Note that Column "softmaxout_1" would be stored in ImageDataPont
56+
//.Scores because the added attributed [ColumnName("softmaxout_1")]
57+
// tells that ImageDataPont.Scores is equivalent to column
58+
// "softmaxout_1".
59+
var transformedDataPoints = mlContext.Data.CreateEnumerable<
60+
ImageDataPoint>(onnx, false).ToList();
4861

49-
// The scores are probabilities of all possible classes, so they should all be positive.
62+
// The scores are probabilities of all possible classes, so they should
63+
// all be positive.
5064
foreach (var dataPoint in transformedDataPoints)
5165
{
5266
var firstClassProb = dataPoint.Scores.First();
5367
var lastClassProb = dataPoint.Scores.Last();
54-
Console.WriteLine($"The probability of being the first class is {firstClassProb * 100}%.");
55-
Console.WriteLine($"The probability of being the last class is {lastClassProb * 100}%.");
68+
Console.WriteLine("The probability of being the first class is " +
69+
(firstClassProb * 100) + "%.");
70+
71+
Console.WriteLine($"The probability of being the last class is " +
72+
(lastClassProb * 100) + "%.");
5673
}
5774

5875
// Expected output:
@@ -62,7 +79,8 @@ public static void Example()
6279
// The probability of being the last class is 0.394428%.
6380
}
6481

65-
// This class is used in Example() to describe data points which will be consumed by ML.NET pipeline.
82+
// This class is used in Example() to describe data points which will be
83+
// consumed by ML.NET pipeline.
6684
private class ImageDataPoint
6785
{
6886
// Height of Image.
@@ -75,9 +93,9 @@ private class ImageDataPoint
7593
[ImageType(height, width)]
7694
public Bitmap Image { get; set; }
7795

78-
// Expected output of ONNX model. It contains probabilities of all classes.
79-
// Note that the ColumnName below should match the output name in the used
80-
// ONNX model file.
96+
// Expected output of ONNX model. It contains probabilities of all
97+
// classes. Note that the ColumnName below should match the output name
98+
// in the used ONNX model file.
8199
[ColumnName("softmaxout_1")]
82100
public float[] Scores { get; set; }
83101

docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/ApplyOnnxModel.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,25 @@ public static void Example()
1919

2020
// Generate sample test data.
2121
var samples = GetTensorData();
22-
// Convert training data to IDataView, the general data type used in ML.NET.
22+
// Convert training data to IDataView, the general data type used in
23+
// ML.NET.
2324
var data = mlContext.Data.LoadFromEnumerable(samples);
2425
// Create the pipeline to score using provided onnx model.
2526
var pipeline = mlContext.Transforms.ApplyOnnxModel(modelPath);
2627
// Fit the pipeline and get the transformed values
2728
var transformedValues = pipeline.Fit(data).Transform(data);
2829
// Retrieve model scores into Prediction class
29-
var predictions = mlContext.Data.CreateEnumerable<Prediction>(transformedValues, reuseRowObject: false);
30+
var predictions = mlContext.Data.CreateEnumerable<Prediction>(
31+
transformedValues, reuseRowObject: false);
3032

3133
// Iterate rows
3234
foreach (var prediction in predictions)
3335
{
3436
int numClasses = 0;
3537
foreach (var classScore in prediction.softmaxout_1.Take(3))
3638
{
37-
Console.WriteLine($"Class #{numClasses++} score = {classScore}");
39+
Console.WriteLine("Class #" + numClasses++ + " score = " +
40+
classScore);
3841
}
3942
Console.WriteLine(new string('-', 10));
4043
}
@@ -65,9 +68,14 @@ public class TensorData
6568
public static TensorData[] GetTensorData()
6669
{
6770
// This can be any numerical data. Assume image pixel values.
68-
var image1 = Enumerable.Range(0, inputSize).Select(x => (float)x / inputSize).ToArray();
69-
var image2 = Enumerable.Range(0, inputSize).Select(x => (float)(x + 10000) / inputSize).ToArray();
70-
return new TensorData[] { new TensorData() { data_0 = image1 }, new TensorData() { data_0 = image2 } };
71+
var image1 = Enumerable.Range(0, inputSize).Select(x => (float)x /
72+
inputSize).ToArray();
73+
74+
var image2 = Enumerable.Range(0, inputSize).Select(x => (float)(x +
75+
10000) / inputSize).ToArray();
76+
77+
return new TensorData[] { new TensorData() { data_0 = image1 }, new
78+
TensorData() { data_0 = image2 } };
7179
}
7280

7381
// Class to contain the output values from the transformation.

docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/ApproximatedKernelMap.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ namespace Samples.Dynamic
99
{
1010
public static class ApproximatedKernelMap
1111
{
12-
// Transform feature vector to another non-linear space. See https://people.eecs.berkeley.edu/~brecht/papers/07.rah.rec.nips.pdf.
12+
// Transform feature vector to another non-linear space. See
13+
// https://people.eecs.berkeley.edu/~brecht/papers/07.rah.rec.nips.pdf.
1314
public static void Example()
1415
{
15-
// Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging,
16-
// as well as the source of randomness.
16+
// Create a new ML context, for ML.NET operations. It can be used for
17+
// exception tracking and logging, as well as the source of randomness.
1718
var mlContext = new MLContext();
1819
var samples = new List<DataPoint>()
1920
{
@@ -22,19 +23,26 @@ public static void Example()
2223
new DataPoint(){ Features = new float[7] {-1, 1, 0,-1,-1, 0,-1} },
2324
new DataPoint(){ Features = new float[7] { 0,-1, 0, 1, 0,-1,-1} }
2425
};
25-
// Convert training data to IDataView, the general data type used in ML.NET.
26+
// Convert training data to IDataView, the general data type used in
27+
// ML.NET.
2628
var data = mlContext.Data.LoadFromEnumerable(samples);
27-
// ApproximatedKernel map takes data and maps it's to a random low-dimensional space.
28-
var approximation = mlContext.Transforms.ApproximatedKernelMap("Features", rank: 4, generator: new GaussianKernel(gamma: 0.7f), seed: 1);
29+
// ApproximatedKernel map takes data and maps it's to a random
30+
// low -dimensional space.
31+
var approximation = mlContext.Transforms.ApproximatedKernelMap(
32+
"Features", rank: 4, generator: new GaussianKernel(gamma: 0.7f),
33+
seed: 1);
2934

30-
// Now we can transform the data and look at the output to confirm the behavior of the estimator.
31-
// This operation doesn't actually evaluate data until we read the data below.
35+
// Now we can transform the data and look at the output to confirm the
36+
// behavior of the estimator. This operation doesn't actually evaluate
37+
// data until we read the data below.
3238
var tansformer = approximation.Fit(data);
3339
var transformedData = tansformer.Transform(data);
3440

3541
var column = transformedData.GetColumn<float[]>("Features").ToArray();
3642
foreach (var row in column)
37-
Console.WriteLine(string.Join(", ", row.Select(x => x.ToString("f4"))));
43+
Console.WriteLine(string.Join(", ", row.Select(x => x.ToString(
44+
"f4"))));
45+
3846
// Expected output:
3947
// -0.0119, 0.5867, 0.4942, 0.7041
4048
// 0.4720, 0.5639, 0.4346, 0.2671

docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/CalculateFeatureContribution.cs

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ public static class CalculateFeatureContribution
99
{
1010
public static void Example()
1111
{
12-
// Create a new context for ML.NET operations. It can be used for exception tracking and logging,
12+
// Create a new context for ML.NET operations. It can be used for
13+
// exception tracking and logging,
1314
// as a catalog of available operations and as the source of randomness.
1415
var mlContext = new MLContext(seed: 1);
1516

@@ -19,7 +20,8 @@ public static void Example()
1920
// Convert training data to IDataView.
2021
var data = mlContext.Data.LoadFromEnumerable(samples);
2122

22-
// Create a pipeline to concatenate the features into a feature vector and normalize it.
23+
// Create a pipeline to concatenate the features into a feature vector
24+
// and normalize it.
2325
var transformPipeline = mlContext.Transforms.Concatenate("Features",
2426
new string[] { nameof(Data.Feature1), nameof(Data.Feature2) })
2527
.Append(mlContext.Transforms.NormalizeMeanVariance("Features"));
@@ -37,28 +39,45 @@ public static void Example()
3739
var linearModel = linearTrainer.Fit(transformedData);
3840
// Print the model parameters.
3941
Console.WriteLine($"Linear Model Parameters");
40-
Console.WriteLine($"Bias: {linearModel.Model.Bias} Feature1: {linearModel.Model.Weights[0]} Feature2: {linearModel.Model.Weights[1]}");
42+
Console.WriteLine("Bias: " + linearModel.Model.Bias+ " Feature1: " +
43+
linearModel.Model.Weights[0] + " Feature2: " +linearModel.Model
44+
.Weights[1]);
4145

42-
// Define a feature contribution calculator for all the features, and don't normalize the contributions.
43-
// These are "trivial estimators" and they don't need to fit to the data, so we can feed a subset.
44-
var simpleScoredDataset = linearModel.Transform(mlContext.Data.TakeRows(transformedData, 1));
45-
var linearFeatureContributionCalculator = mlContext.Transforms.CalculateFeatureContribution(linearModel, normalize: false).Fit(simpleScoredDataset);
46+
// Define a feature contribution calculator for all the features, and
47+
// don't normalize the contributions.These are "trivial estimators" and
48+
// they don't need to fit to the data, so we can feed a subset.
49+
var simpleScoredDataset = linearModel.Transform(mlContext.Data
50+
.TakeRows(transformedData, 1));
51+
52+
var linearFeatureContributionCalculator = mlContext.Transforms
53+
.CalculateFeatureContribution(linearModel, normalize: false).Fit(
54+
simpleScoredDataset);
4655

4756
// Create a transformer chain to describe the entire pipeline.
48-
var scoringPipeline = transformer.Append(linearModel).Append(linearFeatureContributionCalculator);
57+
var scoringPipeline = transformer.Append(linearModel).Append(
58+
linearFeatureContributionCalculator);
4959

50-
// Create the prediction engine to get the features extracted from the text.
51-
var predictionEngine = mlContext.Model.CreatePredictionEngine<Data, ScoredData>(scoringPipeline);
60+
// Create the prediction engine to get the features extracted from the
61+
// text.
62+
var predictionEngine = mlContext.Model.CreatePredictionEngine<Data,
63+
ScoredData>(scoringPipeline);
5264

5365
// Convert the text into numeric features.
5466
var prediction = predictionEngine.Predict(samples.First());
5567

5668
// Write out the prediction, with contributions.
57-
// Note that for the linear model, the feature contributions for a feature in an example is the feature-weight*feature-value.
69+
// Note that for the linear model, the feature contributions for a
70+
// feature in an example is the feature-weight*feature-value.
5871
// The total prediction is thus the bias plus the feature contributions.
59-
Console.WriteLine($"Label: {prediction.Label} Prediction: {prediction.Score}");
60-
Console.WriteLine($"Feature1: {prediction.Features[0]} Feature2: {prediction.Features[1]}");
61-
Console.WriteLine($"Feature Contributions: {prediction.FeatureContributions[0]} {prediction.FeatureContributions[1]}");
72+
Console.WriteLine("Label: " + prediction.Label + " Prediction: " +
73+
prediction.Score);
74+
75+
Console.WriteLine("Feature1: " + prediction.Features[0] +
76+
" Feature2: " + prediction.Features[1]);
77+
78+
Console.WriteLine("Feature Contributions: " + prediction
79+
.FeatureContributions[0] + " " + prediction
80+
.FeatureContributions[1]);
6281

6382
// Expected output:
6483
// Linear Model Parameters
@@ -107,7 +126,9 @@ private static IEnumerable<Data> GenerateData(int nExamples = 10000,
107126
};
108127

109128
// Create a noisy label.
110-
data.Label = (float)(bias + weight1 * data.Feature1 + weight2 * data.Feature2 + rng.NextDouble() - 0.5);
129+
data.Label = (float)(bias + weight1 * data.Feature1 + weight2 *
130+
data.Feature2 + rng.NextDouble() - 0.5);
131+
111132
yield return data;
112133
}
113134
}

0 commit comments

Comments
 (0)