|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | + |
| 6 | +//================================================================================================= |
| 7 | +// This test can be run either as a compiled test with .NET Core (on any platform) or |
| 8 | +// manually in script form (to help debug it and also check that F# scripting works with ML.NET). |
| 9 | +// Running as a script requires using F# Interactive on Windows, and the explicit references below. |
| 10 | +// The references would normally be created by a package loader for the scripting |
| 11 | +// environment, e.g. see https://github.com/isaacabraham/ml-test-experiment/, but |
| 12 | +// here we list them explicitly to avoid the dependency on a package loader, |
| 13 | +// |
| 14 | +// You should build Microsoft.ML.FSharp.Tests in Debug mode for framework net461 |
| 15 | +// before running this as a script with F# Interactive by editing the project |
| 16 | +// file to have: |
| 17 | +// <TargetFrameworks>netcoreapp2.0; net461</TargetFrameworks> |
| 18 | + |
| 19 | +#if INTERACTIVE |
| 20 | +#r "netstandard" |
| 21 | +#r @"../../bin/AnyCPU.Debug/Microsoft.ML.FSharp.Tests/net461/Microsoft.ML.Core.dll" |
| 22 | +#r @"../../bin/AnyCPU.Debug/Microsoft.ML.FSharp.Tests/net461/Google.Protobuf.dll" |
| 23 | +#r @"../../bin/AnyCPU.Debug/Microsoft.ML.FSharp.Tests/net461/Newtonsoft.Json.dll" |
| 24 | +#r @"../../bin/AnyCPU.Debug/Microsoft.ML.FSharp.Tests/net461/System.CodeDom.dll" |
| 25 | +#r @"../../bin/AnyCPU.Debug/Microsoft.ML.FSharp.Tests/net461/System.Threading.Tasks.Dataflow.dll" |
| 26 | +#r @"../../bin/AnyCPU.Debug/Microsoft.ML.FSharp.Tests/net461/Microsoft.ML.CpuMath.dll" |
| 27 | +#r @"../../bin/AnyCPU.Debug/Microsoft.ML.FSharp.Tests/net461/Microsoft.ML.Data.dll" |
| 28 | +#r @"../../bin/AnyCPU.Debug/Microsoft.ML.FSharp.Tests/net461/Microsoft.ML.Transforms.dll" |
| 29 | +#r @"../../bin/AnyCPU.Debug/Microsoft.ML.FSharp.Tests/net461/Microsoft.ML.ResultProcessor.dll" |
| 30 | +#r @"../../bin/AnyCPU.Debug/Microsoft.ML.FSharp.Tests/net461/Microsoft.ML.PCA.dll" |
| 31 | +#r @"../../bin/AnyCPU.Debug/Microsoft.ML.FSharp.Tests/net461/Microsoft.ML.KMeansClustering.dll" |
| 32 | +#r @"../../bin/AnyCPU.Debug/Microsoft.ML.FSharp.Tests/net461/Microsoft.ML.FastTree.dll" |
| 33 | +#r @"../../bin/AnyCPU.Debug/Microsoft.ML.FSharp.Tests/net461/Microsoft.ML.Api.dll" |
| 34 | +#r @"../../bin/AnyCPU.Debug/Microsoft.ML.FSharp.Tests/net461/Microsoft.ML.Sweeper.dll" |
| 35 | +#r @"../../bin/AnyCPU.Debug/Microsoft.ML.FSharp.Tests/net461/Microsoft.ML.dll" |
| 36 | +#r @"../../bin/AnyCPU.Debug/Microsoft.ML.FSharp.Tests/net461/Microsoft.ML.StandardLearners.dll" |
| 37 | +#r @"../../bin/AnyCPU.Debug/Microsoft.ML.FSharp.Tests/net461/Microsoft.ML.PipelineInference.dll" |
| 38 | +#r @"../../bin/AnyCPU.Debug/Microsoft.ML.FSharp.Tests/net461/xunit.core.dll" |
| 39 | +#r @"../../bin/AnyCPU.Debug/Microsoft.ML.FSharp.Tests/net461/xunit.assert.dll" |
| 40 | +#r "System" |
| 41 | +#r "System.ComponentModel.Composition" |
| 42 | +#r "System.Core" |
| 43 | +#r "System.Xml.Linq" |
| 44 | + |
| 45 | +// Later tests will add data import using F# type providers: |
| 46 | +//#r @"../../packages/fsharp.data/3.0.0-beta4/lib/netstandard2.0/FSharp.Data.dll" // this must be referenced from its package location |
| 47 | + |
| 48 | +let _load = |
| 49 | + // See https://github.com/dotnet/machinelearning/issues/401: forces the loading of ML.NET assemblies |
| 50 | + [ typeof<Microsoft.ML.Runtime.Transforms.TextAnalytics>; |
| 51 | + typeof<Microsoft.ML.Runtime.FastTree.FastTree> ] |
| 52 | + |
| 53 | +#endif |
| 54 | + |
| 55 | +//================================================================================ |
| 56 | +// The tests proper start here |
| 57 | + |
| 58 | +#if !INTERACTIVE |
| 59 | +namespace Microsoft.ML.FSharp.Tests |
| 60 | +#endif |
| 61 | + |
| 62 | +open System |
| 63 | +open Microsoft.ML |
| 64 | +open Microsoft.ML.Data |
| 65 | +open Microsoft.ML.Transforms |
| 66 | +open Microsoft.ML.Trainers |
| 67 | +open Microsoft.ML.Runtime.Api |
| 68 | +open Xunit |
| 69 | + |
| 70 | +module SmokeTest1 = |
| 71 | + |
| 72 | + type SentimentData() = |
| 73 | + [<Column(ordinal = "0"); DefaultValue>] |
| 74 | + val mutable SentimentText : string |
| 75 | + [<Column(ordinal = "1", name = "Label"); DefaultValue>] |
| 76 | + val mutable Sentiment : float32 |
| 77 | + |
| 78 | + type SentimentPrediction() = |
| 79 | + [<ColumnName "PredictedLabel"; DefaultValue>] |
| 80 | + val mutable Sentiment : bool |
| 81 | + |
| 82 | + [<Fact>] |
| 83 | + let ``FSharp-Sentiment-Smoke-Test`` () = |
| 84 | + |
| 85 | + let testDataPath = __SOURCE_DIRECTORY__ + @"/../data/wikipedia-detox-250-line-data.tsv" |
| 86 | + |
| 87 | + let pipeline = LearningPipeline() |
| 88 | + |
| 89 | + pipeline.Add( |
| 90 | + TextLoader(testDataPath).CreateFrom<SentimentData>( |
| 91 | + Arguments = |
| 92 | + TextLoaderArguments( |
| 93 | + HasHeader = true, |
| 94 | + Column = [| TextLoaderColumn(Name = "Label", Source = [| TextLoaderRange(0) |], Type = Nullable (Data.DataKind.Num)) |
| 95 | + TextLoaderColumn(Name = "SentimentText", Source = [| TextLoaderRange(1) |], Type = Nullable (Data.DataKind.Text)) |] |
| 96 | + ))) |
| 97 | + |
| 98 | + pipeline.Add( |
| 99 | + TextFeaturizer( |
| 100 | + "Features", [| "SentimentText" |], |
| 101 | + KeepDiacritics = false, |
| 102 | + KeepPunctuations = false, |
| 103 | + TextCase = TextNormalizerTransformCaseNormalizationMode.Lower, |
| 104 | + OutputTokens = true, |
| 105 | + VectorNormalizer = TextTransformTextNormKind.L2 |
| 106 | + )) |
| 107 | + |
| 108 | + pipeline.Add( |
| 109 | + FastTreeBinaryClassifier( |
| 110 | + NumLeaves = 5, |
| 111 | + NumTrees = 5, |
| 112 | + MinDocumentsInLeafs = 2 |
| 113 | + )) |
| 114 | + |
| 115 | + let model = pipeline.Train<SentimentData, SentimentPrediction>() |
| 116 | + |
| 117 | + let predictions = |
| 118 | + [ SentimentData(SentimentText = "This is a gross exaggeration. Nobody is setting a kangaroo court. There was a simple addition.") |
| 119 | + SentimentData(SentimentText = "Sort of ok") |
| 120 | + SentimentData(SentimentText = "Joe versus the Volcano Coffee Company is a great film.") ] |
| 121 | + |> model.Predict |
| 122 | + |
| 123 | + let predictionResults = [ for p in predictions -> p.Sentiment ] |
| 124 | + Assert.Equal<bool list>(predictionResults, [ false; true; true ]) |
| 125 | + |
| 126 | +#if NETCOREAPP2_0 |
| 127 | +module Program = |
| 128 | + |
| 129 | + [<EntryPoint>] |
| 130 | + let main _ = 0 |
| 131 | +#endif |
| 132 | + |
0 commit comments