Skip to content

Commit fb536fd

Browse files
committed
add F# smoke test
1 parent 0f94a3b commit fb536fd

File tree

3 files changed

+196
-0
lines changed

3 files changed

+196
-0
lines changed

Microsoft.ML.sln

+11
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.ML.CodeAnalyzer",
9797
EndProject
9898
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.ML.CodeAnalyzer.Tests", "test\Microsoft.ML.CodeAnalyzer.Tests\Microsoft.ML.CodeAnalyzer.Tests.csproj", "{3E4ABF07-7970-4BE6-B45B-A13D3C397545}"
9999
EndProject
100+
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Microsoft.ML.FSharp.Tests", "test\Microsoft.ML.FSharp.Tests\Microsoft.ML.FSharp.Tests.fsproj", "{802233D6-8CC0-46AD-9F23-FEE1E9AED9B3}"
101+
EndProject
100102
Global
101103
GlobalSection(SolutionConfigurationPlatforms) = preSolution
102104
Debug|Any CPU = Debug|Any CPU
@@ -329,6 +331,14 @@ Global
329331
{3E4ABF07-7970-4BE6-B45B-A13D3C397545}.Release|Any CPU.Build.0 = Release|Any CPU
330332
{3E4ABF07-7970-4BE6-B45B-A13D3C397545}.Release-Intrinsics|Any CPU.ActiveCfg = Release|Any CPU
331333
{3E4ABF07-7970-4BE6-B45B-A13D3C397545}.Release-Intrinsics|Any CPU.Build.0 = Release|Any CPU
334+
{802233D6-8CC0-46AD-9F23-FEE1E9AED9B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
335+
{802233D6-8CC0-46AD-9F23-FEE1E9AED9B3}.Debug|Any CPU.Build.0 = Debug|Any CPU
336+
{802233D6-8CC0-46AD-9F23-FEE1E9AED9B3}.Debug-Intrinsics|Any CPU.ActiveCfg = Debug|Any CPU
337+
{802233D6-8CC0-46AD-9F23-FEE1E9AED9B3}.Debug-Intrinsics|Any CPU.Build.0 = Debug|Any CPU
338+
{802233D6-8CC0-46AD-9F23-FEE1E9AED9B3}.Release|Any CPU.ActiveCfg = Release|Any CPU
339+
{802233D6-8CC0-46AD-9F23-FEE1E9AED9B3}.Release|Any CPU.Build.0 = Release|Any CPU
340+
{802233D6-8CC0-46AD-9F23-FEE1E9AED9B3}.Release-Intrinsics|Any CPU.ActiveCfg = Release|Any CPU
341+
{802233D6-8CC0-46AD-9F23-FEE1E9AED9B3}.Release-Intrinsics|Any CPU.Build.0 = Release|Any CPU
332342
EndGlobalSection
333343
GlobalSection(SolutionProperties) = preSolution
334344
HideSolutionNode = FALSE
@@ -367,6 +377,7 @@ Global
367377
{BF66A305-DF10-47E4-8D81-42049B149D2B} = {D3D38B03-B557-484D-8348-8BADEE4DF592}
368378
{B4E55B2D-2A92-46E7-B72F-E76D6FD83440} = {7F13E156-3EBA-4021-84A5-CD56BA72F99E}
369379
{3E4ABF07-7970-4BE6-B45B-A13D3C397545} = {AED9C836-31E3-4F3F-8ABC-929555D3F3C4}
380+
{802233D6-8CC0-46AD-9F23-FEE1E9AED9B3} = {AED9C836-31E3-4F3F-8ABC-929555D3F3C4}
370381
EndGlobalSection
371382
GlobalSection(ExtensibilityGlobals) = postSolution
372383
SolutionGuid = {41165AF1-35BB-4832-A189-73060F82B01D}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>netcoreapp2.0</TargetFrameworks>
5+
<NoWarn>2003;$(NoWarn)</NoWarn>
6+
<TargetFrameworks Condition="'$(OS)' != 'Unix'">$(TargetFrameworks); net461</TargetFrameworks>
7+
<PublicSign>false</PublicSign>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<Compile Include="SmokeTests.fs" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0" />
16+
<PackageReference Include="xunit" Version="2.3.1" />
17+
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
18+
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
19+
<!-- Future updates to this test will check use with F# type providers, so -->
20+
<!-- leaving this here for now. -->
21+
<!-- <PackageReference Include="FSharp.Data" Version="3.0.0-beta4" /> -->
22+
</ItemGroup>
23+
24+
<ItemGroup>
25+
<!-- More projects are referenced than are currently tested. Future updates to -->
26+
<!-- these tests will test more of the surface area from F#, so leaving these references -->
27+
<!-- here for now. -->
28+
<ProjectReference Include="..\..\src\Microsoft.ML.Api\Microsoft.ML.Api.csproj" />
29+
<ProjectReference Include="..\..\src\Microsoft.ML.Core\Microsoft.ML.Core.csproj" />
30+
<ProjectReference Include="..\..\src\Microsoft.ML.CpuMath\Microsoft.ML.CpuMath.csproj" />
31+
<ProjectReference Include="..\..\src\Microsoft.ML.Data\Microsoft.ML.Data.csproj" />
32+
<ProjectReference Include="..\..\src\Microsoft.ML.Ensemble\Microsoft.ML.Ensemble.csproj" />
33+
<ProjectReference Include="..\..\src\Microsoft.ML.FastTree\Microsoft.ML.FastTree.csproj" />
34+
<ProjectReference Include="..\..\src\Microsoft.ML.KMeansClustering\Microsoft.ML.KMeansClustering.csproj" />
35+
<ProjectReference Include="..\..\src\Microsoft.ML.LightGBM\Microsoft.ML.LightGBM.csproj" />
36+
<ProjectReference Include="..\..\src\Microsoft.ML.Maml\Microsoft.ML.Maml.csproj" />
37+
<ProjectReference Include="..\..\src\Microsoft.ML.Onnx\Microsoft.ML.Onnx.csproj" />
38+
<ProjectReference Include="..\..\src\Microsoft.ML.Parquet\Microsoft.ML.Parquet.csproj" />
39+
<ProjectReference Include="..\..\src\Microsoft.ML.PCA\Microsoft.ML.PCA.csproj" />
40+
<ProjectReference Include="..\..\src\Microsoft.ML.PipelineInference\Microsoft.ML.PipelineInference.csproj" />
41+
<ProjectReference Include="..\..\src\Microsoft.ML.ResultProcessor\Microsoft.ML.ResultProcessor.csproj" />
42+
<ProjectReference Include="..\..\src\Microsoft.ML.StandardLearners\Microsoft.ML.StandardLearners.csproj" />
43+
<ProjectReference Include="..\..\src\Microsoft.ML.Sweeper\Microsoft.ML.Sweeper.csproj" />
44+
<ProjectReference Include="..\..\src\Microsoft.ML.Transforms\Microsoft.ML.Transforms.csproj" />
45+
<ProjectReference Include="..\..\src\Microsoft.ML\Microsoft.ML.csproj" />
46+
</ItemGroup>
47+
<ItemGroup>
48+
<NativeAssemblyReference Include="FastTreeNative" />
49+
<NativeAssemblyReference Include="CpuMathNative" />
50+
<NativeAssemblyReference Include="FactorizationMachineNative" />
51+
</ItemGroup>
52+
53+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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

Comments
 (0)