Skip to content

Commit e0c4caa

Browse files
authored
Reformatting TensorFlow and AnomalyDetection samples to width 85 (#3922)
* Testing * Reformatted TensorFlow samples to width 85 * Reformatted AnomalyDetection samples to width 85 * Fixed formatting errors * Made a few edits to TensorFlow samples format * Fixed syntax errors * Spacing changes
1 parent 17c9155 commit e0c4caa

File tree

4 files changed

+143
-74
lines changed

4 files changed

+143
-74
lines changed

docs/samples/Microsoft.ML.Samples/Dynamic/TensorFlow/ImageClassification.cs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ public static void Example()
2323
if (!File.Exists(modelLocation))
2424
{
2525
modelLocation = Download(@"https://storage.googleapis.com/download.tensorflow.org/models/tflite_11_05_08/resnet_v2_101.tgz", @"resnet_v2_101_299_frozen.tgz");
26-
Unzip(Path.Join(Directory.GetCurrentDirectory(), modelLocation), Directory.GetCurrentDirectory());
26+
Unzip(Path.Join(Directory.GetCurrentDirectory(), modelLocation),
27+
Directory.GetCurrentDirectory());
28+
2729
modelLocation = "resnet_v2_101_299_frozen.pb";
2830
}
2931

@@ -32,7 +34,8 @@ public static void Example()
3234
var idv = mlContext.Data.LoadFromEnumerable(data);
3335

3436
// Create a ML pipeline.
35-
var pipeline = mlContext.Model.LoadTensorFlowModel(modelLocation).ScoreTensorFlowModel(
37+
var pipeline = mlContext.Model.LoadTensorFlowModel(modelLocation)
38+
.ScoreTensorFlowModel(
3639
new[] { nameof(OutputScores.output) },
3740
new[] { nameof(TensorData.input) }, addBatchDimensionInput: true);
3841

@@ -41,15 +44,18 @@ public static void Example()
4144
var transformedValues = estimator.Transform(idv);
4245

4346
// Retrieve model scores.
44-
var outScores = mlContext.Data.CreateEnumerable<OutputScores>(transformedValues, reuseRowObject: false);
47+
var outScores = mlContext.Data.CreateEnumerable<OutputScores>(
48+
transformedValues, reuseRowObject: false);
4549

46-
// Display scores. (for the sake of brevity we display scores of the first 3 classes)
50+
// Display scores. (for the sake of brevity we display scores of the
51+
// first 3 classes)
4752
foreach (var prediction in outScores)
4853
{
4954
int numClasses = 0;
5055
foreach (var classScore in prediction.output.Take(3))
5156
{
52-
Console.WriteLine($"Class #{numClasses++} score = {classScore}");
57+
Console.WriteLine(
58+
$"Class #{numClasses++} score = {classScore}");
5359
}
5460
Console.WriteLine(new string('-', 10));
5561
}
@@ -72,7 +78,8 @@ public static void Example()
7278

7379
/// <summary>
7480
/// A class to hold sample tensor data.
75-
/// Member name should match the inputs that the model expects (in this case, input).
81+
/// Member name should match the inputs that the model expects (in this
82+
/// case, input).
7683
/// </summary>
7784
public class TensorData
7885
{
@@ -86,9 +93,13 @@ public class TensorData
8693
public static TensorData[] GetTensorData()
8794
{
8895
// This can be any numerical data. Assume image pixel values.
89-
var image1 = Enumerable.Range(0, inputSize).Select(x => (float)x / inputSize).ToArray();
90-
var image2 = Enumerable.Range(0, inputSize).Select(x => (float)(x + 10000) / inputSize).ToArray();
91-
return new TensorData[] { new TensorData() { input = image1 }, new TensorData() { input = image2 } };
96+
var image1 = Enumerable.Range(0, inputSize).Select(
97+
x => (float)x / inputSize).ToArray();
98+
99+
var image2 = Enumerable.Range(0, inputSize).Select(
100+
x => (float)(x + 10000) / inputSize).ToArray();
101+
return new TensorData[] { new TensorData() { input = image1 },
102+
new TensorData() { input = image2 } };
92103
}
93104

94105
/// <summary>
@@ -110,7 +121,8 @@ private static string Download(string baseGitPath, string dataFile)
110121
}
111122

112123
/// <summary>
113-
/// Taken from https://github.com/icsharpcode/SharpZipLib/wiki/GZip-and-Tar-Samples.
124+
/// Taken from
125+
/// https://github.com/icsharpcode/SharpZipLib/wiki/GZip-and-Tar-Samples.
114126
/// </summary>
115127
private static void Unzip(string path, string targetDir)
116128
{
@@ -125,4 +137,4 @@ private static void Unzip(string path, string targetDir)
125137
inStream.Close();
126138
}
127139
}
128-
}
140+
}

docs/samples/Microsoft.ML.Samples/Dynamic/TensorFlow/TextClassification.cs

Lines changed: 80 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,36 @@ public static class TextClassification
1313
/// </summary>
1414
public static void Example()
1515
{
16-
string modelLocation = Microsoft.ML.SamplesUtils.DatasetUtils.DownloadTensorFlowSentimentModel();
16+
string modelLocation = Microsoft.ML.SamplesUtils.DatasetUtils
17+
.DownloadTensorFlowSentimentModel();
1718

1819
var mlContext = new MLContext();
1920
var data = new[] { new IMDBSentiment() {
20-
Sentiment_Text = "this film was just brilliant casting location scenery story direction " +
21-
"everyone's really suited the part they played and you could just imagine being there robert " +
22-
"is an amazing actor and now the same being director father came from the same scottish " +
23-
"island as myself so i loved the fact there was a real connection with this film the witty " +
24-
"remarks throughout the film were great it was just brilliant so much that i bought the " +
25-
"film as soon as it was released for and would recommend it to everyone to watch and the " +
26-
"fly fishing was amazing really cried at the end it was so sad and you know what they say " +
27-
"if you cry at a film it must have been good and this definitely was also to the two " +
28-
"little boy's that played the of norman and paul they were just brilliant children are " +
29-
"often left out of the list i think because the stars that play them all grown up are " +
30-
"such a big profile for the whole film but these children are amazing and should be praised " +
31-
"for what they have done don't you think the whole story was so lovely because it was true " +
32-
"and was someone's life after all that was shared with us all" } };
21+
Sentiment_Text = "this film was just brilliant casting location " +
22+
"scenery story direction everyone's really suited the part they " +
23+
"played and you could just imagine being there robert is an " +
24+
"amazing actor and now the same being director father came from " +
25+
"the same scottish island as myself so i loved the fact there " +
26+
"was a real connection with this film the witty remarks " +
27+
"throughout the film were great it was just brilliant so much " +
28+
"that i bought the film as soon as it was released for and " +
29+
"would recommend it to everyone to watch and the fly fishing was " +
30+
"amazing really cried at the end it was so sad and you know what " +
31+
"they say if you cry at a film it must have been good and this " +
32+
"definitely was also to the two little boy's that played the of " +
33+
"norman and paul they were just brilliant children are often " +
34+
"left out of the list i think because the stars that play them " +
35+
"all grown up are such a big profile for the whole film but " +
36+
"these children are amazing and should be praised for what " +
37+
"they have done don't you think the whole story was so lovely" +
38+
"because it was true and was someone's life after all that was" +
39+
"shared with us all" } };
3340
var dataView = mlContext.Data.LoadFromEnumerable(data);
3441

3542
// This is the dictionary to convert words into the integer indexes.
36-
var lookupMap = mlContext.Data.LoadFromTextFile(Path.Combine(modelLocation, "imdb_word_index.csv"),
43+
var lookupMap = mlContext.Data.LoadFromTextFile(Path.Combine(
44+
modelLocation, "imdb_word_index.csv"),
45+
3746
columns: new[]
3847
{
3948
new TextLoader.Column("Words", DataKind.String, 0),
@@ -43,49 +52,79 @@ public static void Example()
4352
);
4453

4554
// Load the TensorFlow model once.
46-
// - Use it for quering the schema for input and output in the model
55+
// - Use it for quering the schema for input and output in the
56+
// model
4757
// - Use it for prediction in the pipeline.
48-
var tensorFlowModel = mlContext.Model.LoadTensorFlowModel(modelLocation);
58+
var tensorFlowModel = mlContext.Model.LoadTensorFlowModel(
59+
modelLocation);
4960
var schema = tensorFlowModel.GetModelSchema();
5061
var featuresType = (VectorDataViewType)schema["Features"].Type;
51-
Console.WriteLine("Name: {0}, Type: {1}, Shape: (-1, {2})", "Features", featuresType.ItemType.RawType, featuresType.Dimensions[0]);
52-
var predictionType = (VectorDataViewType)schema["Prediction/Softmax"].Type;
53-
Console.WriteLine("Name: {0}, Type: {1}, Shape: (-1, {2})", "Prediction/Softmax", predictionType.ItemType.RawType, predictionType.Dimensions[0]);
54-
55-
// The model expects the input feature vector to be a fixed length vector.
56-
// In this sample, CustomMappingEstimator is used to resize variable length vector to fixed length vector.
62+
Console.WriteLine("Name: {0}, Type: {1}, Shape: (-1, {2})", "Features",
63+
featuresType.ItemType.RawType, featuresType.Dimensions[0]);
64+
65+
var predictionType = (VectorDataViewType)schema["Prediction/Softmax"]
66+
.Type;
67+
Console.WriteLine("Name: {0}, Type: {1}, Shape: (-1, {2})",
68+
"Prediction/Softmax", predictionType.ItemType.RawType,
69+
predictionType.Dimensions[0]);
70+
71+
// The model expects the input feature vector to be a fixed length
72+
// vector.
73+
// In this sample, CustomMappingEstimator is used to resize variable
74+
// length vector to fixed length vector.
5775
// The following ML.NET pipeline
5876
// 1. tokenzies the string into words,
59-
// 2. maps each word to an integer which is an index in the dictionary ('lookupMap'),
60-
// 3. Resizes the integer vector to a fixed length vector using CustomMappingEstimator ('ResizeFeaturesAction')
77+
// 2. maps each word to an integer which is an index in the
78+
// dictionary ('lookupMap'),
79+
// 3. Resizes the integer vector to a fixed length vector using
80+
// CustomMappingEstimator ('ResizeFeaturesAction')
6181
// 4. Passes the data to TensorFlow for scoring.
62-
// 5. Retreives the 'Prediction' from TensorFlow and put it into ML.NET Pipeline
82+
// 5. Retreives the 'Prediction' from TensorFlow and put it into
83+
// ML.NET Pipeline
6384

64-
Action<IMDBSentiment, IntermediateFeatures> ResizeFeaturesAction = (i, j) =>
85+
Action<IMDBSentiment, IntermediateFeatures> ResizeFeaturesAction =
86+
(i, j) =>
6587
{
6688
j.Sentiment_Text = i.Sentiment_Text;
6789
var features = i.VariableLengthFeatures;
6890
Array.Resize(ref features, MaxSentenceLength);
6991
j.Features = features;
7092
};
7193

72-
var model = mlContext.Transforms.Text.TokenizeIntoWords("TokenizedWords", "Sentiment_Text")
73-
.Append(mlContext.Transforms.Conversion.MapValue("VariableLengthFeatures", lookupMap,
74-
lookupMap.Schema["Words"], lookupMap.Schema["Ids"], "TokenizedWords"))
75-
.Append(mlContext.Transforms.CustomMapping(ResizeFeaturesAction, "Resize"))
76-
.Append(tensorFlowModel.ScoreTensorFlowModel("Prediction/Softmax", "Features"))
77-
.Append(mlContext.Transforms.CopyColumns("Prediction", "Prediction/Softmax"))
94+
var model =
95+
mlContext.Transforms.Text.TokenizeIntoWords(
96+
"TokenizedWords",
97+
"Sentiment_Text")
98+
.Append(mlContext.Transforms.Conversion.MapValue(
99+
"VariableLengthFeatures",
100+
lookupMap,
101+
lookupMap.Schema["Words"],
102+
lookupMap.Schema["Ids"],
103+
"TokenizedWords"))
104+
.Append(mlContext.Transforms.CustomMapping(
105+
ResizeFeaturesAction,
106+
"Resize"))
107+
.Append(tensorFlowModel.ScoreTensorFlowModel(
108+
"Prediction/Softmax",
109+
"Features"))
110+
.Append(mlContext.Transforms.CopyColumns(
111+
"Prediction",
112+
"Prediction/Softmax"))
78113
.Fit(dataView);
79-
var engine = mlContext.Model.CreatePredictionEngine<IMDBSentiment, OutputScores>(model);
114+
var engine = mlContext.Model.CreatePredictionEngine<IMDBSentiment,
115+
OutputScores>(model);
80116

81117
// Predict with TensorFlow pipeline.
82118
var prediction = engine.Predict(data[0]);
83119

84-
Console.WriteLine("Number of classes: {0}", prediction.Prediction.Length);
85-
Console.WriteLine("Is sentiment/review positive? {0}", prediction.Prediction[1] > 0.5 ? "Yes." : "No.");
86-
Console.WriteLine("Prediction Confidence: {0}", prediction.Prediction[1].ToString("0.00"));
120+
Console.WriteLine("Number of classes: {0}", prediction.Prediction
121+
.Length);
122+
Console.WriteLine("Is sentiment/review positive? {0}", prediction
123+
.Prediction[1] > 0.5 ? "Yes." : "No.");
124+
Console.WriteLine("Prediction Confidence: {0}", prediction.Prediction[1]
125+
.ToString("0.00"));
87126

88-
/////////////////////////////////// Expected output ///////////////////////////////////
127+
///////////////////////////// Expected output //////////////////////////
89128
//
90129
// Name: Features, Type: System.Int32, Shape: (-1, 600)
91130
// Name: Prediction/Softmax, Type: System.Single, Shape: (-1, 2)
@@ -105,8 +144,9 @@ public class IMDBSentiment
105144

106145
/// <summary>
107146
/// This is a variable length vector designated by VectorType attribute.
108-
/// Variable length vectors are produced by applying operations such as 'TokenizeWords' on strings
109-
/// resulting in vectors of tokens of variable lengths.
147+
/// Variable length vectors are produced by applying operations such as
148+
/// 'TokenizeWords' on strings resulting in vectors of tokens of
149+
/// variable lengths.
110150
/// </summary>
111151
[VectorType]
112152
public int[] VariableLengthFeatures { get; set; }

docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/AnomalyDetection/RandomizedPcaSample.cs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ public static class RandomizedPcaSample
1010
{
1111
public static void Example()
1212
{
13-
// Create a new context for ML.NET operations. It can be used for exception tracking and logging,
14-
// as a catalog of available operations and as the source of randomness.
15-
// Setting the seed to a fixed number in this example to make outputs deterministic.
13+
// Create a new context for ML.NET operations. It can be used for except
14+
// ion tracking and logging, as a catalog of available operations and as
15+
// the source of randomness. Setting the seed to a fixed number in this
16+
// example to make outputs deterministic.
1617
var mlContext = new MLContext(seed: 0);
1718

1819
// Training data.
@@ -26,11 +27,15 @@ public static void Example()
2627
new DataPoint(){ Features = new float[3] {-100, 50, -100} }
2728
};
2829

29-
// Convert the List<DataPoint> to IDataView, a consumble format to ML.NET functions.
30+
// Convert the List<DataPoint> to IDataView, a consumble format to
31+
// ML.NET functions.
3032
var data = mlContext.Data.LoadFromEnumerable(samples);
3133

32-
// Create an anomaly detector. Its underlying algorithm is randomized PCA.
33-
var pipeline = mlContext.AnomalyDetection.Trainers.RandomizedPca(featureColumnName: nameof(DataPoint.Features), rank: 1, ensureZeroMean: false);
34+
// Create an anomaly detector. Its underlying algorithm is randomized
35+
// PCA.
36+
var pipeline = mlContext.AnomalyDetection.Trainers.RandomizedPca(
37+
featureColumnName: nameof(DataPoint.Features), rank: 1,
38+
ensureZeroMean: false);
3439

3540
// Train the anomaly detector.
3641
var model = pipeline.Fit(data);
@@ -39,7 +44,8 @@ public static void Example()
3944
var transformed = model.Transform(data);
4045

4146
// Read ML.NET predictions into IEnumerable<Result>.
42-
var results = mlContext.Data.CreateEnumerable<Result>(transformed, reuseRowObject: false).ToList();
47+
var results = mlContext.Data.CreateEnumerable<Result>(transformed,
48+
reuseRowObject: false).ToList();
4349

4450
// Let's go through all predictions.
4551
for (int i = 0; i < samples.Count; ++i)
@@ -52,12 +58,14 @@ public static void Example()
5258

5359
if (result.PredictedLabel)
5460
// The i-th sample is predicted as an inlier.
55-
Console.WriteLine("The {0}-th example with features [{1}] is an inlier with a score of being inlier {2}",
56-
i, featuresInText, result.Score);
61+
Console.WriteLine("The {0}-th example with features [{1}]" +
62+
"is an inlier with a score of being inlier {2}", i,
63+
featuresInText, result.Score);
5764
else
5865
// The i-th sample is predicted as an outlier.
59-
Console.WriteLine("The {0}-th example with features [{1}] is an outlier with a score of being inlier {2}",
60-
i, featuresInText, result.Score);
66+
Console.WriteLine("The {0}-th example with features [{1}] is" +
67+
"an outlier with a score of being inlier {2}", i,
68+
featuresInText, result.Score);
6169
}
6270
// Lines printed out should be
6371
// The 0 - th example with features[1, 0, 0] is an inlier with a score of being inlier 0.7453707
@@ -68,7 +76,8 @@ public static void Example()
6876
// The 5 - th example with features[-100, 50, -100] is an outlier with a score of being inlier 0
6977
}
7078

71-
// Example with 3 feature values. A training data set is a collection of such examples.
79+
// Example with 3 feature values. A training data set is a collection of
80+
// such examples.
7281
private class DataPoint
7382
{
7483
[VectorType(3)]

0 commit comments

Comments
 (0)