Skip to content

Commit 1011711

Browse files
kevmalCESARDELATORRE
authored andcommitted
migrate F# samples to 0.10 (dotnet#244)
* migrate F# samples to 0.10 * post merge updates
1 parent 17b7a4a commit 1011711

File tree

35 files changed

+125
-105
lines changed

35 files changed

+125
-105
lines changed

samples/fsharp/common_v0.9/ConsoleHelper.fs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ module ConsoleHelper =
44
open System
55
open Microsoft.ML
66
open Microsoft.ML.Data
7-
open Microsoft.ML.Data
87
open Microsoft.ML.Core.Data
98
//open Microsoft.ML.Api
109
open System.Reflection
10+
open Microsoft.Data.DataView
1111

1212
let printPrediction prediction =
1313
printfn "*************************************************"
@@ -139,7 +139,7 @@ module ConsoleHelper =
139139
// 'transformedData' is a 'promise' of data, lazy-loading. Let's actually read it.
140140
// Convert to an enumerable of user-defined type.
141141
let someRows =
142-
transformedData.AsEnumerable<'TObservation>(mlContext, reuseRowObject = false)
142+
mlContext.CreateEnumerable<'TObservation>(transformedData, reuseRowObject = false)
143143
// Take the specified number of rows
144144
|> Seq.take numberOfRows
145145
// Convert to List

samples/fsharp/common_v0.9/ModelBuilder.fs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ open Microsoft.ML.Transforms
77
open Microsoft.ML.Data
88

99
module ModelBuilder =
10+
open Microsoft.Data.DataView
1011

1112
let create (mlContext : MLContext) (pipeline : IEstimator<ITransformer>) =
1213
(mlContext, pipeline)

samples/fsharp/getting-started/CreditCardFraudDetection/CreditCardFraudDetection/CreditCardFraudDetection.fsproj renamed to samples/fsharp/getting-started/BinaryClassification_CreditCardFraudDetection/CreditCardFraudDetection/CreditCardFraudDetection.fsproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</ItemGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="Microsoft.ML" Version="0.9.0" />
13+
<PackageReference Include="Microsoft.ML" Version="0.10.0" />
1414
</ItemGroup>
1515

1616
</Project>

samples/fsharp/getting-started/CreditCardFraudDetection/CreditCardFraudDetection/Program.fs renamed to samples/fsharp/getting-started/BinaryClassification_CreditCardFraudDetection/CreditCardFraudDetection/Program.fs

+6-6
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,11 @@ let main _ =
123123
let loaderArgs = TextLoader.Arguments()
124124
loaderArgs.Column <- columns
125125
loaderArgs.HasHeader <- true
126-
loaderArgs.Separator <- ","
126+
loaderArgs.Separators <- [| ',' |]
127127

128128
let reader = TextLoader (mlContext, loaderArgs)
129129

130-
let classification = BinaryClassificationContext mlContext
130+
let classification = BinaryClassificationCatalog mlContext
131131

132132
(*
133133
Split the data 80:20 into train and test files,
@@ -207,15 +207,15 @@ let main _ =
207207
trainFile,
208208
columnsPlus,
209209
loaderArgs.HasHeader,
210-
loaderArgs.Separator.ToCharArray().[0]
210+
loaderArgs.Separators.[0]
211211
)
212212

213213
let testData =
214214
mlContext.Data.ReadFromTextFile(
215215
testFile,
216216
columnsPlus,
217217
loaderArgs.HasHeader,
218-
loaderArgs.Separator.ToCharArray().[0]
218+
loaderArgs.Separators.[0]
219219
)
220220

221221
trainData, testData
@@ -237,8 +237,8 @@ let main _ =
237237
|> fun x ->
238238
x.Append (
239239
mlContext.Transforms.Normalize (
240-
"Features",
241240
"FeaturesNormalizedByMeanVar",
241+
"Features",
242242
NormalizingEstimator.NormalizerMode.MeanVariance
243243
)
244244
)
@@ -279,7 +279,7 @@ let main _ =
279279
let testData = mlContext.Data.ReadFromTextFile (testFile, columnsPlus, hasHeader = true, separatorChar = ',')
280280

281281
printfn "Making predictions"
282-
testData.AsEnumerable<TransactionObservation>(mlContext, reuseRowObject = false)
282+
mlContext.CreateEnumerable<TransactionObservation>(testData, reuseRowObject = false)
283283
|> Seq.filter (fun x -> x.Label = true)
284284
// use 5 observations from the test data
285285
|> Seq.take 5

samples/fsharp/getting-started/BinaryClassification_SentimentAnalysis/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
| ML.NET version | API type | Status | App Type | Data type | Scenario | ML Task | Algorithms |
44
|----------------|-------------------|-------------------------------|-------------|-----------|---------------------|---------------------------|-----------------------------|
5-
| v0.9 | Dynamic API | README.md updated | Console app | .tsv files | Sentiment Analysis | Two-class classification | Linear Classification |
5+
| v0.10 | Dynamic API | README.md updated | Console app | .tsv files | Sentiment Analysis | Two-class classification | Linear Classification |
66

77
------------------------------------
88

@@ -45,7 +45,7 @@ The initial code is similar to the following:
4545
```fsharp
4646
// STEP 1: Common data loading configuration
4747
let textLoader =
48-
mlContext.Data.CreateTextReader (
48+
mlContext.Data.CreateTextLoader(
4949
columns =
5050
[|
5151
TextLoader.Column("Label", Nullable DataKind.Bool, 0)
@@ -60,7 +60,7 @@ The initial code is similar to the following:
6060
6161
6262
// STEP 2: Common data process configuration with pipeline data transformations
63-
let dataProcessPipeline = mlContext.Transforms.Text.FeaturizeText("Text", "Features")
63+
let dataProcessPipeline = mlContext.Transforms.Text.FeaturizeText("Features", "Text")
6464
6565
// STEP 3: Set the training algorithm, then create and config the modelBuilder
6666
let trainer = mlContext.BinaryClassification.Trainers.FastTree(labelColumn = "Label", featureColumn = "Features")

samples/fsharp/getting-started/BinaryClassification_SentimentAnalysis/SentimentAnalysis/SentimentAnalysisConsoleApp/Program.fs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ let buildTrainEvaluateAndSaveModel (mlContext : MLContext) =
2727
let testDataView = mlContext.Data.ReadFromTextFile<SentimentIssue>(testDataPath, hasHeader = true)
2828

2929
// STEP 2: Common data process configuration with pipeline data transformations
30-
let dataProcessPipeline = mlContext.Transforms.Text.FeaturizeText("Text", "Features")
30+
let dataProcessPipeline = mlContext.Transforms.Text.FeaturizeText("Features", "Text")
3131

3232
// (OPTIONAL) Peek data (such as 2 records) in training DataView after applying the ProcessPipeline's transformations into "Features"
3333
Common.ConsoleHelper.peekDataViewInConsole<SentimentIssue> mlContext trainingDataView dataProcessPipeline 2 |> ignore

samples/fsharp/getting-started/BinaryClassification_SentimentAnalysis/SentimentAnalysis/SentimentAnalysisConsoleApp/SentimentAnalysisConsoleApp.fsproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</ItemGroup>
1919

2020
<ItemGroup>
21-
<PackageReference Include="Microsoft.ML" Version="$(MicrosoftMLVersion)" />
21+
<PackageReference Include="Microsoft.ML" Version="0.10.0" />
2222
</ItemGroup>
2323

2424
<ItemGroup>

samples/fsharp/getting-started/BinaryClassification_SpamDetection/SpamDetectionConsoleApp/Program.fs

+4-5
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ let main _argv =
4747
// Set up the MLContext, which is a catalog of components in ML.NET.
4848
let mlContext = MLContext(seed = Nullable 1)
4949
let reader =
50-
mlContext.Data.CreateTextReader(
50+
mlContext.Data.CreateTextLoader(
5151
columns =
5252
[|
5353
TextLoader.Column("LabelText" , Nullable DataKind.Text, 0)
@@ -58,12 +58,11 @@ let main _argv =
5858

5959
let data = reader.Read(trainDataPath)
6060

61-
// Create the estimator which converts the text label to a key sorted by value (with ham < spam we get ham -> false and spam -> true)
62-
// then featurizes the text, and add a linear trainer.
61+
// Create the estimator which converts the text label to a bool then featurizes the text, and add a linear trainer.
6362
let estimator =
6463
EstimatorChain()
65-
.Append(mlContext.Transforms.Conversion.MapValueToKey("LabelText", "Label", sort = ValueToKeyMappingTransformer.SortOrder.Value))
66-
.Append(mlContext.Transforms.Text.FeaturizeText("Message","Features"))
64+
.Append(mlContext.Transforms.Conversion.ValueMap(["ham"; "spam"], [false; true],[| struct ("Label", "LabelText") |]))
65+
.Append(mlContext.Transforms.Text.FeaturizeText("Features", "Message"))
6766
.AppendCacheCheckpoint(mlContext)
6867
.Append(mlContext.BinaryClassification.Trainers.StochasticDualCoordinateAscent("Label", "Features"))
6968

samples/fsharp/getting-started/BinaryClassification_SpamDetection/SpamDetectionConsoleApp/SpamDetectionConsoleApp.fsproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</ItemGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="Microsoft.ML" Version="0.9.0" />
13+
<PackageReference Include="Microsoft.ML" Version="0.10.0" />
1414
</ItemGroup>
1515

1616
</Project>

samples/fsharp/getting-started/Clustering_CustomerSegmentation/CustomerSegmentation.Predict/CustomerSegmentation.Predict.fsproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</ItemGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="Microsoft.ML" Version="0.9.0" />
13+
<PackageReference Include="Microsoft.ML" Version="0.10.0" />
1414
<PackageReference Include="OxyPlot.Core" Version="1.0.0" />
1515
</ItemGroup>
1616

samples/fsharp/getting-started/Clustering_CustomerSegmentation/CustomerSegmentation.Predict/Program.fs

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ let main _argv =
111111
mlContext.Model.Load(f)
112112

113113
let reader =
114-
mlContext.Data.CreateTextReader(
114+
mlContext.Data.CreateTextLoader(
115115
columns =
116116
[|
117117
TextLoader.Column("Features", Nullable DataKind.R4, [| TextLoader.Range(0, Nullable 31) |])
@@ -123,7 +123,7 @@ let main _argv =
123123
let data = reader.Read(pivotCsv)
124124

125125
//Apply data transformation to create predictions/clustering
126-
let predictions = model.Transform(data).AsEnumerable<ClusteringPrediction>(mlContext, false) |> Seq.toArray
126+
let predictions = mlContext.CreateEnumerable<ClusteringPrediction>(model.Transform(data),false) |> Seq.toArray
127127

128128
//Generate data files with customer data grouped by clusters
129129
printHeader ["CSV Customer Segmentation"]

samples/fsharp/getting-started/Clustering_CustomerSegmentation/CustomerSegmentation.Train/CustomerSegmentation.Train.fsproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
</ItemGroup>
2222

2323
<ItemGroup>
24-
<PackageReference Include="Microsoft.ML" Version="0.9.0" />
24+
<PackageReference Include="Microsoft.ML" Version="0.10.0" />
2525
</ItemGroup>
2626

2727
</Project>

samples/fsharp/getting-started/Clustering_CustomerSegmentation/CustomerSegmentation.Train/Program.fs

+3-3
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ let main _argv =
7777
let mlContext = MLContext(seed = Nullable 1); //Seed set to any number so you have a deterministic environment
7878
// STEP 1: Common data loading configuration
7979
let textLoader =
80-
mlContext.Data.CreateTextReader(
80+
mlContext.Data.CreateTextLoader(
8181
columns =
8282
[|
8383
TextLoader.Column("Features", Nullable DataKind.R4, [| TextLoader.Range(0, Nullable 31) |])
@@ -91,8 +91,8 @@ let main _argv =
9191
//STEP 2: Configure data transformations in pipeline
9292
let dataProcessPipeline =
9393
EstimatorChain()
94-
.Append(mlContext.Transforms.Projection.ProjectToPrincipalComponents("Features", "PCAFeatures", rank = 2))
95-
.Append(mlContext.Transforms.Categorical.OneHotEncoding([| OneHotEncodingEstimator.ColumnInfo("LastName", "LastNameKey", OneHotEncodingTransformer.OutputKind.Ind) |]))
94+
.Append(mlContext.Transforms.Projection.ProjectToPrincipalComponents("PCAFeatures", "Features", rank = 2))
95+
.Append(mlContext.Transforms.Categorical.OneHotEncoding([| OneHotEncodingEstimator.ColumnInfo("LastNameKey", "LastName", OneHotEncodingTransformer.OutputKind.Ind) |]))
9696

9797
// (Optional) Peek data in training DataView after applying the ProcessPipeline's transformations
9898
Common.ConsoleHelper.peekDataViewInConsole<PivotObservation> mlContext pivotDataView (ConsoleHelper.downcastPipeline dataProcessPipeline) 10 |> ignore

samples/fsharp/getting-started/Clustering_CustomerSegmentation/README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
| ML.NET version | API type | Status | App Type | Data type | Scenario | ML Task | Algorithms |
44
|----------------|-------------------|-------------------------------|-------------|-----------|---------------------|---------------------------|-----------------------------|
5-
| v0.9 | Dynamic API | Up-to-date | Console app | .csv files | Customer segmentation | Clustering | K-means++ |
5+
| v0.10 | Dynamic API | Up-to-date | Console app | .csv files | Customer segmentation | Clustering | K-means++ |
66

77
## Problem
88

@@ -106,7 +106,7 @@ Here's the code which will be used to build the model:
106106
let mlContext = MLContext(seed = Nullable 1); //Seed set to any number so you have a deterministic environment
107107
// STEP 1: Common data loading configuration
108108
let textLoader =
109-
mlContext.Data.CreateTextReader(
109+
mlContext.Data.CreateTextLoader(
110110
columns =
111111
[|
112112
TextLoader.Column("Features", Nullable DataKind.R4, [| TextLoader.Range(0, Nullable 31) |])
@@ -120,8 +120,8 @@ let pivotDataView = textLoader.Read(pivotCsv)
120120
//STEP 2: Configure data transformations in pipeline
121121
let dataProcessPipeline =
122122
EstimatorChain()
123-
.Append(mlContext.Transforms.Projection.ProjectToPrincipalComponents("Features", "PCAFeatures", rank = 2))
124-
.Append(mlContext.Transforms.Categorical.OneHotEncoding([| OneHotEncodingEstimator.ColumnInfo("LastName", "LastNameKey", OneHotEncodingTransformer.OutputKind.Ind) |]))
123+
.Append(mlContext.Transforms.Projection.ProjectToPrincipalComponents("PCAFeatures", "Features", rank = 2))
124+
.Append(mlContext.Transforms.Categorical.OneHotEncoding([| OneHotEncodingEstimator.ColumnInfo("LastNameKey", "LastName", OneHotEncodingTransformer.OutputKind.Ind) |]))
125125
126126
// (Optional) Peek data in training DataView after applying the ProcessPipeline's transformations
127127
Common.ConsoleHelper.peekDataViewInConsole<PivotObservation> mlContext pivotDataView (ConsoleHelper.downcastPipeline dataProcessPipeline) 10 |> ignore
@@ -135,7 +135,7 @@ let trainingPipeline = dataProcessPipeline.Append(trainer)
135135
In this case, `TextLoader` doesn't define explicitly each column, but declares a `Features` property made by the first 32 columns of the file; also declares the property `LastName` to the value of the last column.
136136

137137
Then, you need to apply some transformations to the data:
138-
1) Add a PCA column, using the `mlContext.Transforms.Projection.ProjectToPrincipalComponents("Features", "PCAFeatures", rank = 2)` Estimator, passing as parameter `rank = 2`, which means that we are reducing the features from 32 to 2 dimensions (*x* and *y*)
138+
1) Add a PCA column, using the `mlContext.Transforms.Projection.ProjectToPrincipalComponents("PCAFeatures", "Features", rank = 2)` Estimator, passing as parameter `rank = 2`, which means that we are reducing the features from 32 to 2 dimensions (*x* and *y*)
139139

140140
2) Transform LastName using `OneHotEncodingEstimator`
141141

samples/fsharp/getting-started/Clustering_Iris/IrisClustering/IrisClusteringConsoleApp/Clustering_Iris.fsproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
</ItemGroup>
1616

1717
<ItemGroup>
18-
<PackageReference Include="Microsoft.ML" Version="$(MicrosoftMLVersion)" />
18+
<PackageReference Include="Microsoft.ML" Version="0.10.0" />
1919
</ItemGroup>
2020

2121
<ItemGroup>

samples/fsharp/getting-started/Clustering_Iris/IrisClustering/IrisClusteringConsoleApp/Program.fs

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ open Microsoft.ML
66
open Microsoft.ML.Data
77
open Clustering_Iris.DataStructures
88
open DataStructures
9+
open Microsoft.Data.DataView
910

1011
let appPath = Path.GetDirectoryName(Environment.GetCommandLineArgs().[0])
1112

@@ -23,7 +24,7 @@ let main argv =
2324

2425
// STEP 1: Common data loading configuration
2526
let textLoader =
26-
mlContext.Data.CreateTextReader(
27+
mlContext.Data.CreateTextLoader(
2728
hasHeader = true,
2829
separatorChar = '\t',
2930
columns =
@@ -50,7 +51,7 @@ let main argv =
5051
Common.ConsoleHelper.peekVectorColumnDataInConsole mlContext "Features" trainingDataView dataProcessPipeline 10 |> ignore
5152

5253
// STEP 3: Create and train the model
53-
let trainer = mlContext.Clustering.Trainers.KMeans(features = "Features", clustersCount = 3)
54+
let trainer = mlContext.Clustering.Trainers.KMeans(featureColumn = "Features", clustersCount = 3)
5455
let trainingPipeline = dataProcessPipeline.Append(trainer)
5556
let trainedModel = trainingPipeline.Fit(trainingDataView)
5657

samples/fsharp/getting-started/Clustering_Iris/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
| ML.NET version | API type | Status | App Type | Data type | Scenario | ML Task | Algorithms |
44
|----------------|-------------------|-------------------------------|-------------|-----------|---------------------|---------------------------|-----------------------------|
5-
| v0.9 | Dynamic API | Up-to-date | Console app | .txt file | Clustering Iris flowers | Clustering | K-means++ |
5+
| v0.10 | Dynamic API | Up-to-date | Console app | .txt file | Clustering Iris flowers | Clustering | K-means++ |
66

77
In this introductory sample, you'll see how to use [ML.NET](https://www.microsoft.com/net/learn/apps/machine-learning-and-ai/ml-dotnet) to divide iris flowers into different groups that correspond to different types of iris. In the world of machine learning, this task is known as **clustering**.
88

@@ -35,7 +35,7 @@ Building a model includes: uploading data (`iris-full.txt` with `TextLoader`), t
3535
```fsharp
3636
// STEP 1: Common data loading configuration
3737
let textLoader =
38-
mlContext.Data.CreateTextReader(
38+
mlContext.Data.CreateTextLoader(
3939
hasHeader = true,
4040
separatorChar = '\t',
4141
columns =
@@ -61,7 +61,7 @@ Building a model includes: uploading data (`iris-full.txt` with `TextLoader`), t
6161
Common.ConsoleHelper.peekVectorColumnDataInConsole mlContext "Features" trainingDataView dataProcessPipeline 10 |> ignore
6262
6363
// STEP 3: Create and train the model
64-
let trainer = mlContext.Clustering.Trainers.KMeans(features = "Features", clustersCount = 3)
64+
let trainer = mlContext.Clustering.Trainers.KMeans(featureColumn = "Features", clustersCount = 3)
6565
6666
let modelBuilder =
6767
Common.ModelBuilder.create mlContext dataProcessPipeline

samples/fsharp/getting-started/DeepLearning_ImageClassification_TensorFlow/ImageClassification/ImageClassification.Score.fsproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
</ItemGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="Microsoft.ML" Version="0.9.0" />
14-
<PackageReference Include="Microsoft.ML.ImageAnalytics" Version="0.9.0" />
15-
<PackageReference Include="Microsoft.ML.TensorFlow" Version="0.9.0" />
13+
<PackageReference Include="Microsoft.ML" Version="0.10.0" />
14+
<PackageReference Include="Microsoft.ML.ImageAnalytics" Version="0.10.0" />
15+
<PackageReference Include="Microsoft.ML.TensorFlow" Version="0.10.0" />
1616
</ItemGroup>
1717

1818
</Project>

samples/fsharp/getting-started/DeepLearning_ImageClassification_TensorFlow/ImageClassification/Program.fs

+4-4
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,10 @@ let score dataLocation imagesFolder inputModelLocation labelsTxt =
126126
let data = mlContext.Data.ReadFromTextFile<ImageNetData>(dataLocation, hasHeader = false)
127127
let pipeline =
128128
EstimatorChain()
129-
.Append(mlContext.Transforms.LoadImages(imageFolder = imagesFolder, columns = [|struct("ImagePath", "ImageReal")|]))
130-
.Append(mlContext.Transforms.Resize("ImageReal", "ImageReal", imageHeight, imageWidth))
131-
.Append(mlContext.Transforms.ExtractPixels([| ImagePixelExtractorTransform.ColumnInfo("ImageReal", "input", interleave = channelsLast, offset = float32 mean) |]))
132-
.Append(mlContext.Transforms.ScoreTensorFlowModel(inputModelLocation, [| "input" |], [| "softmax2" |]))
129+
.Append(mlContext.Transforms.LoadImages(imageFolder = imagesFolder, columns = [|struct("ImageReal", "ImagePath")|]))
130+
.Append(mlContext.Transforms.Resize("ImageReal", imageWidth, imageHeight, inputColumnName = "ImageReal"))
131+
.Append(mlContext.Transforms.ExtractPixels([| ImagePixelExtractorTransformer.ColumnInfo("input", "ImageReal", interleave = channelsLast, offset = float32 mean) |]))
132+
.Append(mlContext.Transforms.ScoreTensorFlowModel(inputModelLocation, [| "softmax2" |], [| "input" |]))
133133
let model = pipeline.Fit(data)
134134
let predictionEngine = model.CreatePredictionEngine<ImageNetData, ImageNetPrediction>(mlContext)
135135

0 commit comments

Comments
 (0)