Skip to content

Instantly share code, notes, and snippets.

@mathias-brandewinder
Last active December 6, 2019 23:37
Show Gist options
  • Save mathias-brandewinder/91bd1e9652d57f94150f0fdbae0b2045 to your computer and use it in GitHub Desktop.
Save mathias-brandewinder/91bd1e9652d57f94150f0fdbae0b2045 to your computer and use it in GitHub Desktop.
ML.NET in FSI / dotnetcore

Setting up ML.NET to run in FSI from VS Code

Note: this currently doesn't work 100% right. The code runs, but there is no code completion on any of the types coming from ML.NET.

Machine / setup

  1. Win10 x64, dotnet core 3.1, VS Code, Ionide
  2. Using Paket from dotnet core: https://fsprojects.github.io/Paket/get-started.html#NET-Core-preferred

paket.dependencies

source https://api.nuget.org/v3/index.json

storage: packages
framework: netcore3.0, netstandard2.0, netstandard2.1
nuget Microsoft.ML

Resulting paket.lock:

STORAGE: PACKAGES
RESTRICTION: || (== netcoreapp3.0) (== netstandard2.0) (== netstandard2.1)
NUGET
  remote: https://api.nuget.org/v3/index.json
    Microsoft.ML (1.4)
      Microsoft.ML.CpuMath (>= 1.4)
      Microsoft.ML.DataView (>= 1.4)
      Newtonsoft.Json (>= 10.0.3)
      System.CodeDom (>= 4.4)
      System.Collections.Immutable (>= 1.5)
      System.Memory (>= 4.5.1)
      System.Reflection.Emit.Lightweight (>= 4.3)
      System.Threading.Tasks.Dataflow (>= 4.8)
    Microsoft.ML.CpuMath (1.4)
      System.Memory (>= 4.5.1) - restriction: || (== netstandard2.0) (== netstandard2.1)
    Microsoft.ML.DataView (1.4)
      System.Memory (>= 4.5.1)
    Newtonsoft.Json (12.0.3)
    System.Buffers (4.5) - restriction: || (&& (== netcoreapp3.0) (>= monotouch)) (&& (== netcoreapp3.0) (>= net461)) (&& (== netcoreapp3.0) (< netcoreapp2.0)) (&& (== netcoreapp3.0) (< netstandard1.1)) (&& (== netcoreapp3.0) (< netstandard2.0)) (&& (== netcoreapp3.0) (>= xamarinios)) (&& (== netcoreapp3.0) (>= xamarinmac)) (&& (== netcoreapp3.0) (>= xamarintvos)) (&& (== netcoreapp3.0) (>= xamarinwatchos)) (== netstandard2.0) (== netstandard2.1)
    System.CodeDom (4.7)
    System.Collections.Immutable (1.7)
      System.Memory (>= 4.5.3) - restriction: || (&& (== netcoreapp3.0) (>= net46)) (&& (== netcoreapp3.0) (< netcoreapp2.1)) (&& (== netcoreapp3.0) (< netstandard2.0)) (&& (== netcoreapp3.0) (>= uap10.1)) (== netstandard2.0) (== netstandard2.1)
    System.Memory (4.5.3)
      System.Buffers (>= 4.4) - restriction: || (&& (== netcoreapp3.0) (>= monotouch)) (&& (== netcoreapp3.0) (>= net461)) (&& (== netcoreapp3.0) (< netcoreapp2.0)) (&& (== netcoreapp3.0) (< netstandard1.1)) (&& (== netcoreapp3.0) (< netstandard2.0)) (&& (== netcoreapp3.0) (>= xamarinios)) (&& (== netcoreapp3.0) (>= xamarinmac)) (&& (== netcoreapp3.0) (>= xamarintvos)) (&& (== netcoreapp3.0) (>= xamarinwatchos)) (== netstandard2.0) (== netstandard2.1)
      System.Numerics.Vectors (>= 4.4) - restriction: || (&& (== netcoreapp3.0) (>= net461)) (&& (== netcoreapp3.0) (< netcoreapp2.0)) (== netstandard2.0) (== netstandard2.1)
      System.Runtime.CompilerServices.Unsafe (>= 4.5.2) - restriction: || (&& (== netcoreapp3.0) (>= monotouch)) (&& (== netcoreapp3.0) (>= net461)) (&& (== netcoreapp3.0) (< netcoreapp2.0)) (&& (== netcoreapp3.0) (< netcoreapp2.1)) (&& (== netcoreapp3.0) (< netstandard1.1)) (&& (== netcoreapp3.0) (< netstandard2.0)) (&& (== netcoreapp3.0) (>= uap10.1)) (&& (== netcoreapp3.0) (>= xamarinios)) (&& (== netcoreapp3.0) (>= xamarinmac)) (&& (== netcoreapp3.0) (>= xamarintvos)) (&& (== netcoreapp3.0) (>= xamarinwatchos)) (== netstandard2.0) (== netstandard2.1)
    System.Numerics.Vectors (4.5) - restriction: || (&& (== netcoreapp3.0) (>= net461)) (&& (== netcoreapp3.0) (< netcoreapp2.0)) (== netstandard2.0) (== netstandard2.1)
    System.Reflection.Emit.ILGeneration (4.7) - restriction: || (&& (== netcoreapp3.0) (< netcoreapp2.0) (< netstandard2.1)) (&& (== netcoreapp3.0) (< netstandard2.0)) (&& (== netcoreapp3.0) (< portable-net45+wp8)) (&& (== netcoreapp3.0) (>= uap10.1)) (== netstandard2.0) (&& (== netstandard2.1) (< netstandard2.0)) (&& (== netstandard2.1) (< portable-net45+wp8)) (&& (== netstandard2.1) (>= uap10.1))
    System.Reflection.Emit.Lightweight (4.7)
      System.Reflection.Emit.ILGeneration (>= 4.7) - restriction: || (&& (== netcoreapp3.0) (< netcoreapp2.0) (< netstandard2.1)) (&& (== netcoreapp3.0) (< netstandard2.0)) (&& (== netcoreapp3.0) (< portable-net45+wp8)) (&& (== netcoreapp3.0) (>= uap10.1)) (== netstandard2.0) (&& (== netstandard2.1) (< netstandard2.0)) (&& (== netstandard2.1) (< portable-net45+wp8)) (&& (== netstandard2.1) (>= uap10.1))
    System.Runtime.CompilerServices.Unsafe (4.7) - restriction: || (&& (== netcoreapp3.0) (>= monotouch)) (&& (== netcoreapp3.0) (>= net461)) (&& (== netcoreapp3.0) (< netcoreapp2.0)) (&& (== netcoreapp3.0) (< netcoreapp2.1)) (&& (== netcoreapp3.0) (< netstandard1.1)) (&& (== netcoreapp3.0) (< netstandard2.0)) (&& (== netcoreapp3.0) (>= uap10.1)) (&& (== netcoreapp3.0) (>= xamarinios)) (&& (== netcoreapp3.0) (>= xamarinmac)) (&& (== netcoreapp3.0) (>= xamarintvos)) (&& (== netcoreapp3.0) (>= xamarinwatchos)) (== netstandard2.0) (== netstandard2.1)
    System.Threading.Tasks.Dataflow (4.11)
instant dteday season yr mnth holiday weekday workingday weathersit temp atemp hum windspeed casual registered cnt
1 2011-01-01 1 0 1 0 6 0 2 0.344167 0.363625 0.805833 0.160446 331 654 985
2 2011-01-02 1 0 1 0 0 0 2 0.363478 0.353739 0.696087 0.248539 131 670 801
3 2011-01-03 1 0 1 0 1 1 1 0.196364 0.189405 0.437273 0.248309 120 1229 1349
4 2011-01-04 1 0 1 0 2 1 1 0.2 0.212122 0.590435 0.160296 108 1454 1562
5 2011-01-05 1 0 1 0 3 1 1 0.226957 0.22927 0.436957 0.1869 82 1518 1600
6 2011-01-06 1 0 1 0 4 1 1 0.204348 0.233209 0.518261 0.0895652 88 1518 1606
7 2011-01-07 1 0 1 0 5 1 2 0.196522 0.208839 0.498696 0.168726 148 1362 1510
8 2011-01-08 1 0 1 0 6 0 2 0.165 0.162254 0.535833 0.266804 68 891 959
9 2011-01-09 1 0 1 0 0 0 1 0.138333 0.116175 0.434167 0.36195 54 768 822
10 2011-01-10 1 0 1 0 1 1 1 0.150833 0.150888 0.482917 0.223267 41 1280 1321
11 2011-01-11 1 0 1 0 2 1 2 0.169091 0.191464 0.686364 0.122132 43 1220 1263
12 2011-01-12 1 0 1 0 3 1 1 0.172727 0.160473 0.599545 0.304627 25 1137 1162
13 2011-01-13 1 0 1 0 4 1 1 0.165 0.150883 0.470417 0.301 38 1368 1406
14 2011-01-14 1 0 1 0 5 1 1 0.16087 0.188413 0.537826 0.126548 54 1367 1421
15 2011-01-15 1 0 1 0 6 0 2 0.233333 0.248112 0.49875 0.157963 222 1026 1248
16 2011-01-16 1 0 1 0 0 0 1 0.231667 0.234217 0.48375 0.188433 251 953 1204
17 2011-01-17 1 0 1 1 1 0 2 0.175833 0.176771 0.5375 0.194017 117 883 1000
18 2011-01-18 1 0 1 0 2 1 2 0.216667 0.232333 0.861667 0.146775 9 674 683
19 2011-01-19 1 0 1 0 3 1 2 0.292174 0.298422 0.741739 0.208317 78 1572 1650
20 2011-01-20 1 0 1 0 4 1 2 0.261667 0.25505 0.538333 0.195904 83 1844 1927
21 2011-01-21 1 0 1 0 5 1 1 0.1775 0.157833 0.457083 0.353242 75 1468 1543
22 2011-01-22 1 0 1 0 6 0 1 0.0591304 0.0790696 0.4 0.17197 93 888 981
23 2011-01-23 1 0 1 0 0 0 1 0.0965217 0.0988391 0.436522 0.2466 150 836 986
24 2011-01-24 1 0 1 0 1 1 1 0.0973913 0.11793 0.491739 0.15833 86 1330 1416
25 2011-01-25 1 0 1 0 2 1 2 0.223478 0.234526 0.616957 0.129796 186 1799 1985
26 2011-01-26 1 0 1 0 3 1 3 0.2175 0.2036 0.8625 0.29385 34 472 506
27 2011-01-27 1 0 1 0 4 1 1 0.195 0.2197 0.6875 0.113837 15 416 431
28 2011-01-28 1 0 1 0 5 1 2 0.203478 0.223317 0.793043 0.1233 38 1129 1167
29 2011-01-29 1 0 1 0 6 0 1 0.196522 0.212126 0.651739 0.145365 123 975 1098
30 2011-01-30 1 0 1 0 0 0 1 0.216522 0.250322 0.722174 0.0739826 140 956 1096
31 2011-01-31 1 0 1 0 1 1 2 0.180833 0.18625 0.60375 0.187192 42 1459 1501
32 2011-02-01 1 0 2 0 2 1 2 0.192174 0.23453 0.829565 0.053213 47 1313 1360
33 2011-02-02 1 0 2 0 3 1 2 0.26 0.254417 0.775417 0.264308 72 1454 1526
34 2011-02-03 1 0 2 0 4 1 1 0.186957 0.177878 0.437826 0.277752 61 1489 1550
35 2011-02-04 1 0 2 0 5 1 2 0.211304 0.228587 0.585217 0.127839 88 1620 1708
36 2011-02-05 1 0 2 0 6 0 2 0.233333 0.243058 0.929167 0.161079 100 905 1005
37 2011-02-06 1 0 2 0 0 0 1 0.285833 0.291671 0.568333 0.1418 354 1269 1623
38 2011-02-07 1 0 2 0 1 1 1 0.271667 0.303658 0.738333 0.0454083 120 1592 1712
39 2011-02-08 1 0 2 0 2 1 1 0.220833 0.198246 0.537917 0.36195 64 1466 1530
40 2011-02-09 1 0 2 0 3 1 2 0.134783 0.144283 0.494783 0.188839 53 1552 1605
41 2011-02-10 1 0 2 0 4 1 1 0.144348 0.149548 0.437391 0.221935 47 1491 1538
42 2011-02-11 1 0 2 0 5 1 1 0.189091 0.213509 0.506364 0.10855 149 1597 1746
43 2011-02-12 1 0 2 0 6 0 1 0.2225 0.232954 0.544167 0.203367 288 1184 1472
44 2011-02-13 1 0 2 0 0 0 1 0.316522 0.324113 0.457391 0.260883 397 1192 1589
45 2011-02-14 1 0 2 0 1 1 1 0.415 0.39835 0.375833 0.417908 208 1705 1913
46 2011-02-15 1 0 2 0 2 1 1 0.266087 0.254274 0.314348 0.291374 140 1675 1815
47 2011-02-16 1 0 2 0 3 1 1 0.318261 0.3162 0.423478 0.251791 218 1897 2115
48 2011-02-17 1 0 2 0 4 1 1 0.435833 0.428658 0.505 0.230104 259 2216 2475
49 2011-02-18 1 0 2 0 5 1 1 0.521667 0.511983 0.516667 0.264925 579 2348 2927
50 2011-02-19 1 0 2 0 6 0 1 0.399167 0.391404 0.187917 0.507463 532 1103 1635
#I "./packages/Microsoft.ML/lib/netstandard2.0/"
#I "./packages/Microsoft.ML.DataView/lib/netstandard2.0/"
#I "./packages/Microsoft.ML.CpuMath/lib/netstandard2.0/"
#I "./packages/Microsoft.ML.DataView/lib/netstandard2.0/"
#I "./packages/Newtonsoft.Json/lib/netstandard2.0/"
#r "./packages/System.Buffers/lib/netstandard2.0/System.Buffers.dll"
#r "./packages/System.CodeDom/lib/netstandard2.0/System.CodeDom.dll"
#r "./packages/System.Collections.Immutable/lib/netstandard2.0/System.Collections.Immutable.dll"
#r "./packages/System.Memory/lib/netstandard2.0/System.Memory.dll"
#r "./packages/System.Numerics.Vectors/lib/netstandard2.0/System.Numerics.Vectors.dll"
#r "./packages/System.Reflection.Emit.ILGeneration/lib/netstandard2.0/System.Reflection.Emit.ILGeneration.dll"
#r "./packages/System.Reflection.Emit.Lightweight/lib/netstandard2.0/System.Reflection.Emit.Lightweight.dll"
#r "./packages/System.Runtime.CompilerServices.Unsafe/lib/netstandard2.0/System.Runtime.CompilerServices.Unsafe.dll"
#r "./packages/System.Threading.Tasks.Dataflow/lib/netstandard2.0/System.Threading.Tasks.Dataflow.dll"
#r "./packages/Newtonsoft.Json/lib/netstandard2.0/Newtonsoft.Json.dll"
#r "./packages/Microsoft.ML/lib/netstandard2.0/Microsoft.ML.Core.dll"
#r "./packages/Microsoft.ML/lib/netstandard2.0/Microsoft.ML.Data.dll"
#r "./packages/Microsoft.ML/lib/netstandard2.0/Microsoft.ML.KMeansClustering.dll"
#r "./packages/Microsoft.ML/lib/netstandard2.0/Microsoft.ML.PCA.dll"
#r "./packages/Microsoft.ML/lib/netstandard2.0/Microsoft.ML.StandardTrainers.dll"
#r "./packages/Microsoft.ML/lib/netstandard2.0/Microsoft.ML.Transforms.dll"
#r "./packages/Microsoft.ML.DataView/lib/netstandard2.0/Microsoft.ML.DataView.dll"
#r "./packages/Microsoft.ML.CpuMath/lib/netstandard2.0/Microsoft.ML.CpuMath.dll"
open System
open System.IO
let path = Environment.GetEnvironmentVariable("PATH")
let additionalPathReferences =
[
"Microsoft.ML/runtimes/win-x64/native"
"Microsoft.ML.CpuMath/runtimes/win-x64/nativeassets/netstandard2.0"
]
|> List.map (fun x ->
Path.Combine(__SOURCE_DIRECTORY__, "packages", x)
|> Path.GetFullPath
)
path :: additionalPathReferences
|> String.concat ";"
|> fun refs -> Environment.SetEnvironmentVariable("PATH", refs)
open Microsoft.ML
open Microsoft.ML.Data
[<CLIMutable>]
type Example = {
[<LoadColumn(0)>]
Day: float32
[<LoadColumn(6)>]
WeekDay: int
[<LoadColumn(8)>]
WeatherSituation: int
[<LoadColumn(9)>]
Temperature: float32
[<LoadColumn(12)>]
WindSpeed: float32
[<LoadColumn(15)>]
Count: float32
}
let seed = Nullable<int>(0)
let context = MLContext seed
let dataPath = Path.Combine(__SOURCE_DIRECTORY__, "day.csv")
let dataView =
context.Data.LoadFromTextFile<Example>(
path = dataPath,
hasHeader = true,
separatorChar = ','
)
let downcastPipeline (x : IEstimator<_>) =
match x with
| :? IEstimator<ITransformer> as y -> y
| _ -> failwith "downcastPipeline: expecting a IEstimator<ITransformer>"
let pipeline =
EstimatorChain()
.Append(context.Transforms.CopyColumns("Label", "Count"))
.Append(context.Transforms.NormalizeMinMax("Day", "Day"))
.Append(context.Transforms.Categorical.OneHotEncoding("WeekDay"))
.Append(context.Transforms.Categorical.OneHotEncoding("WeatherSituation"))
.Append(
context.Transforms.Concatenate(
"Features",
"Day",
"WeekDay",
"WeatherSituation",
"Temperature",
"WindSpeed"
)
)
.AppendCacheCheckpoint(context)
|> downcastPipeline
let trainer =
context.Regression.Trainers.Sdca(
labelColumnName = "Label",
featureColumnName = "Features"
)
let testTrain =
context.Data.TrainTestSplit(
data = dataView,
testFraction = 0.25,
seed = Nullable 0
)
let train = testTrain.TrainSet
let test = testTrain.TestSet
let modelBuilder = pipeline.Append trainer
let trainedModel = modelBuilder.Fit train
// measure model quality
// on training set
let metrics =
let predictions = trainedModel.Transform train
context.Regression.Evaluate(predictions, "Label", "Score")
metrics.RootMeanSquaredError |> printfn "RMSE: %.2f"
metrics.MeanAbsoluteError |> printfn "MAE: %.2f"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment