Ported tutorial code to Python (#264)

* Added TestPython project.
* TestFSharp exposes xUnit tests.
* Added Invoker.InvokeInstance.  Invoker favors overloads with higher array indexing depth.
* Added EpTests.LinearProgrammingTest.
* Updated FSharp.Core, Msagl
* pr-netcore.yml uses latest version of .NET 3.1 instead of exact version
* UseDotNet task
* Updated versions of YAML tasks
* netcoretest.sh uses "dotnet test"
This commit is contained in:
Tom Minka 2020-07-26 07:08:10 +01:00 коммит произвёл GitHub
Родитель 032372ce36
Коммит 82bfc893ed
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
34 изменённых файлов: 541 добавлений и 120 удалений

7
.gitignore поставляемый
Просмотреть файл

@ -258,8 +258,5 @@ InferNet_Copy_Temp/
apiguide-tmp/
_site/
#code generated by iron python examples
src/IronPythonWrapper/InferNetExamples/InferNetExamples/GeneratedSource/
#Compiler for IronPython
src/IronPythonWrapper/Compiler
**/GeneratedSource
**/__pycache__

Просмотреть файл

@ -55,9 +55,10 @@ When not using Windows, expect build failure messages about examples that use WP
### Run unit tests
Unit tests are written using the [XUnit](https://xunit.github.io/) framework.
In order to run unit tests, build the test project and execute one of the following commands:
The easiest way to run all tests is to execute the script `netcoretest.sh` or `monotest.sh`.
To run unit tests manually, build the test project and execute one of the following commands:
```bash
dotnet ~/.nuget/packages/xunit.runner.console/2.3.1/tools/netcoreapp2.0/xunit.console.dll <path to netcoreapp3.1 assembly with tests> <filter>
dotnet test Tests -c <configuration> <filter>
```
```bash
mono ~/.nuget/packages/xunit.runner.console/2.3.1/tools/net452/xunit.console.exe <path to net461 assembly with tests> <filter>
@ -92,23 +93,7 @@ testing you must filter these out by `-notrait`.
_BadTest_ is a category of tests that must fail.
_OpenBug_ is a category of tests that can fail.
An example of quick testing of `Microsoft.ML.Probabilistic.Tests.dll` in `Debug` configuration after changing working directory to
the `Tests` project looks like:
```bash
dotnet ~/.nuget/packages/xunit.runner.console/2.3.1/tools/netcoreapp2.0/xunit.console.dll bin/DebugCore/netcoreapp3.1/Microsoft.ML.Probabilistic.Tests.dll -notrait Category=OpenBug -notrait Category=BadTest -notrait Category=CompilerOptionsTest -notrait Category=Platform -notrait Category=CsoftModel -notrait Category=ModifiesGlobals -notrait Category=DistributedTest -notrait Category=Performance
dotnet ~/.nuget/packages/xunit.runner.console/2.3.1/tools/netcoreapp2.0/xunit.console.dll bin/DebugCore/netcoreapp3.1/Microsoft.ML.Probabilistic.Tests.dll -trait Category=CsoftModel -trait Category=ModifiesGlobals -trait Category=DistributedTests -trait Category=Performance -notrait Category=OpenBug -notrait Category=BadTest -notrait Category=CompilerOptionsTest -notrait Category=Platform -parallel none
```
To run the same set of tests on Mono:
```bash
mono ~/.nuget/packages/xunit.runner.console/2.3.1/tools/net452/xunit.console.exe bin/DebugFull/net461/Microsoft.ML.Probabilistic.Tests.dll -notrait Category=OpenBug -notrait Category=BadTest -notrait Category=CompilerOptionsTest -notrait Category=Platform -notrait Category=CsoftModel -notrait Category=ModifiesGlobals -notrait Category=DistributedTest -notrait Category=Performance
mono ~/.nuget/packages/xunit.runner.console/2.3.1/tools/net452/xunit.console.exe bin/DebugFull/net461/Microsoft.ML.Probabilistic.Tests.dll -trait Category=CsoftModel -trait Category=ModifiesGlobals -trait Category=DistributedTests -trait Category=Performance -notrait Category=OpenBug -notrait Category=BadTest -notrait Category=CompilerOptionsTest -notrait Category=Platform -parallel none
```
Helper scripts `netcoretest.sh` and `monotest.sh` for running unit tests on .NET Core and Mono respectively are located in the `test` folder.
See the scripts for example filters.
## Fast matrix operations with Intel MKL
Matrix operations in Infer.NET can be significantly accelerated by building with Intel MKL support.

Просмотреть файл

@ -4,7 +4,9 @@ VisualStudioVersion = 16.0.29020.237
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{A181C943-2E01-454D-9008-2E3C53AA09CC}"
ProjectSection(SolutionItems) = preProject
BUILDING.md = BUILDING.md
Infer.snk = Infer.snk
README.md = README.md
TestRunConfig1.testrunconfig = TestRunConfig1.testrunconfig
EndProjectSection
ProjectSection(FolderStartupServices) = postProject
@ -98,6 +100,30 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LearnersNuGet", "src\Learne
EndProject
Project("{888888A0-9F3D-457C-B088-3A5042F75D52}") = "Tools.PythonScripts", "src\Tools\PythonScripts\Tools.PythonScripts.pyproj", "{D7562CC3-D48A-481D-B40C-07EE51EBBF50}"
EndProject
Project("{888888A0-9F3D-457C-B088-3A5042F75D52}") = "TestPython", "test\TestPython\TestPython.pyproj", "{79B39D08-DF06-47B8-B7EE-15E30EB9A209}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{5759CC85-729E-479A-BF7E-8A0B8937B123}"
ProjectSection(SolutionItems) = preProject
build\common.props = build\common.props
build\copyassemblies.sh = build\copyassemblies.sh
build\evaluator-netcore.yml = build\evaluator-netcore.yml
build\evaluator-netframework.yml = build\evaluator-netframework.yml
test\monotest.sh = test\monotest.sh
build\netcoretest-fast.yml = build\netcoretest-fast.yml
test\netcoretest.sh = test\netcoretest.sh
build\nightly-netcore.yml = build\nightly-netcore.yml
build\nightly-windows.yml = build\nightly-windows.yml
build\nuget-properties.props = build\nuget-properties.props
build\pr-factordocs.yml = build\pr-factordocs.yml
build\pr-msbuild.yml = build\pr-msbuild.yml
build\pr-netcore.yml = build\pr-netcore.yml
build\release.yml = build\release.yml
build\sign.proj = build\sign.proj
build\updateversion.sh = build\updateversion.sh
build\vstest-fast.yml = build\vstest-fast.yml
build\windows-msbuild.yml = build\windows-msbuild.yml
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -512,6 +538,12 @@ Global
{D7562CC3-D48A-481D-B40C-07EE51EBBF50}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D7562CC3-D48A-481D-B40C-07EE51EBBF50}.ReleaseCore|Any CPU.ActiveCfg = Release|Any CPU
{D7562CC3-D48A-481D-B40C-07EE51EBBF50}.ReleaseFull|Any CPU.ActiveCfg = Release|Any CPU
{79B39D08-DF06-47B8-B7EE-15E30EB9A209}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{79B39D08-DF06-47B8-B7EE-15E30EB9A209}.DebugCore|Any CPU.ActiveCfg = Debug|Any CPU
{79B39D08-DF06-47B8-B7EE-15E30EB9A209}.DebugFull|Any CPU.ActiveCfg = Debug|Any CPU
{79B39D08-DF06-47B8-B7EE-15E30EB9A209}.Release|Any CPU.ActiveCfg = Release|Any CPU
{79B39D08-DF06-47B8-B7EE-15E30EB9A209}.ReleaseCore|Any CPU.ActiveCfg = Release|Any CPU
{79B39D08-DF06-47B8-B7EE-15E30EB9A209}.ReleaseFull|Any CPU.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

Просмотреть файл

@ -8,6 +8,7 @@ parameters:
Configuration: 'ReleaseCore'
steps:
# TODO: Use .NET Core CLI Task instead: https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/build/dotnet-core-cli?view=azure-devops
- task: Bash@3
displayName: 'Running tests'
inputs:
@ -17,8 +18,9 @@ steps:
displayName: Run tests
continueOnError: true
# https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/test/publish-test-results?view=azure-devops&tabs=yaml
- task: PublishTestResults@2
displayName: 'Publishing test results'
inputs:
testRunner: XUnit
testResultsFiles: 'test/netcoretest-result*.xml'
testResultsFormat: vstest
testResultsFiles: 'test/**/netcoretest-result*.trx'

Просмотреть файл

@ -16,10 +16,10 @@ queue:
trigger: none # disable CI build
steps:
- task: DotNetCoreInstaller@0
- task: UseDotNet@2
inputs:
packageType: 'sdk'
version: '3.1.201'
version: '3.x'
- script: |
dotnet build /p:DisableImplicitNuGetFallbackFolder=true --configuration $(BuildConfiguration)Core Infer.sln

Просмотреть файл

@ -11,10 +11,10 @@ resources:
trigger: none # disable CI build
steps:
- task: DotNetCoreInstaller@0
- task: UseDotNet@2
inputs:
packageType: 'sdk'
version: '3.1.201'
version: '3.x'
- script: |
echo Checking $(Build.Repository.LocalPath)\src\Runtime\Factors\FactorDocs.xml...

Просмотреть файл

@ -19,9 +19,9 @@ jobs:
pool:
vmImage: windows-2019
steps:
- task: NuGetToolInstaller@0
- task: NuGetToolInstaller@1
inputs:
versionSpec: '5.5.1'
versionSpec: '>=5.5.1'
- task: NuGetCommand@2
inputs:
@ -43,9 +43,9 @@ jobs:
pool:
vmImage: windows-2019
steps:
- task: NuGetToolInstaller@0
- task: NuGetToolInstaller@1
inputs:
versionSpec: '5.5.1'
versionSpec: '>=5.5.1'
- task: NuGetCommand@2
inputs:

Просмотреть файл

@ -17,10 +17,10 @@ variables:
buildConfiguration: 'Release'
steps:
- task: DotNetCoreInstaller@0
- task: UseDotNet@2
inputs:
packageType: 'sdk'
version: '3.1.201'
version: '3.x'
- script: |
dotnet build /p:DisableImplicitNuGetFallbackFolder=true --configuration $(buildConfiguration)Core Infer.sln

Просмотреть файл

@ -19,10 +19,10 @@ jobs:
pool:
vmImage: macOS-10.14
steps:
- task: DotNetCoreInstaller@0
- task: UseDotNet@2
inputs:
packageType: 'sdk'
version: '3.1.201'
version: '3.x'
- script: |
dotnet build --configuration $(BuildConfiguration)Core Infer.sln
@ -34,10 +34,10 @@ jobs:
pool:
vmImage: ubuntu-16.04
steps:
- task: DotNetCoreInstaller@0
- task: UseDotNet@2
inputs:
packageType: 'sdk'
version: '3.1.201'
version: '3.x'
- script: |
dotnet build --configuration $(BuildConfiguration)Core Infer.sln
@ -57,9 +57,9 @@ jobs:
workingDirectory: build
arguments: $(Build.BuildNumber)
- task: NuGetToolInstaller@0
- task: NuGetToolInstaller@1
inputs:
versionSpec: '5.5.1'
versionSpec: '>=5.5.1'
- task: NuGetCommand@2
displayName: 'Restoring NuGet packages'

Просмотреть файл

@ -17,9 +17,9 @@ steps:
workingDirectory: build
arguments: ${{ parameters.BuildNumber }}
- task: NuGetToolInstaller@0
- task: NuGetToolInstaller@1
inputs:
versionSpec: '5.5.1'
versionSpec: '>=5.5.1'
- task: NuGetCommand@2
displayName: 'Restoring NuGet packages'

Просмотреть файл

@ -45,10 +45,16 @@ namespace Microsoft.ML.Probabilistic.Compiler.Reflection
/// </summary>
public Conversion[] Conversions;
/// <summary>
/// Array indexing depth needed to obtain a match.
/// </summary>
public uint Depth;
public void SetTo(Binding info)
{
foreach (KeyValuePair<Type, Type> entry in info.Types) Types[entry.Key] = entry.Value;
info.Conversions.CopyTo(Conversions, 0);
Depth = info.Depth;
}
/// <summary>
@ -91,6 +97,7 @@ namespace Microsoft.ML.Probabilistic.Compiler.Reflection
//if (a.Types.Count < b.Types.Count) return true;
float aWeight = Conversion.GetWeight(a.Conversions);
float bWeight = Conversion.GetWeight(b.Conversions);
if (aWeight == bWeight) return a.Depth > b.Depth;
return (aWeight < bWeight);
}
@ -584,15 +591,15 @@ namespace Microsoft.ML.Probabilistic.Compiler.Reflection
}
Type formalElement = formal.GetElementType();
Type actualElement = actual.GetElementType();
binding.Depth++;
IEnumerator<Binding> iter3 = InferGenericParameters(formalElement, actualElement, binding, errors, position, allowSubtype, conversionOptions);
while (iter3.MoveNext())
{
binding = iter3.Current;
Conversion oldConversion = new Conversion();
Conversion elementConversion = default(Conversion);
if (position >= 0)
{
oldConversion = binding.Conversions[position];
Conversion elementConversion = binding.Conversions[position];
elementConversion = binding.Conversions[position];
if (elementConversion.Converter != null)
{
// if there is an element conversion, promote it to an array conversion
@ -605,8 +612,9 @@ namespace Microsoft.ML.Probabilistic.Compiler.Reflection
}
yield return binding;
// restore previous state
if (position >= 0) binding.Conversions[position] = oldConversion;
if (position >= 0) binding.Conversions[position] = elementConversion;
}
binding.Depth--;
yield break;
}
else
@ -628,6 +636,7 @@ namespace Microsoft.ML.Probabilistic.Compiler.Reflection
int subclassCount = 0;
bool anyMatch = false;
List<Exception> innerErrors = new List<Exception>();
binding.Depth++;
foreach (Type parent in choices)
{
if (!parent.IsGenericType || !formalGenericTypeDefinition.Equals(parent.GetGenericTypeDefinition()))
@ -651,6 +660,7 @@ namespace Microsoft.ML.Probabilistic.Compiler.Reflection
}
subclassCount++;
}
binding.Depth--;
if (!anyMatch || innerErrors.Count > 0)
{
string formalString = StringUtil.TypeToString(formal);
@ -853,7 +863,7 @@ namespace Microsoft.ML.Probabilistic.Compiler.Reflection
public override string ToString()
{
return StringUtil.DictionaryToString(Types, Environment.NewLine);
return $"Binding({StringUtil.DictionaryToString(Types, Environment.NewLine)}, Conversions={StringUtil.ArrayToString(Conversions)}, Depth={Depth})";
}
/// <summary>

Просмотреть файл

@ -70,6 +70,20 @@ namespace Microsoft.ML.Probabilistic.Compiler.Reflection
}
}
/// <summary>
/// Invoke the instance member which best matches the argument types.
/// </summary>
/// <param name="methodName"></param>
/// <param name="target"></param>
/// <param name="args"></param>
/// <returns></returns>
public static object InvokeInstance(string methodName, object target, params object[] args)
{
return InvokeMember(target.GetType(), methodName,
BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetField | BindingFlags.GetProperty | BindingFlags.InvokeMethod |
BindingFlags.FlattenHierarchy, target, args);
}
/// <summary>
/// Get an element from an array or collection, or invoke a delegate.
/// </summary>

Просмотреть файл

@ -8,7 +8,7 @@
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\Microsoft.ML.Probabilistic.FSharp.XML</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FSharp.Core" Version="4.3.4" />
<PackageReference Include="FSharp.Core" Version="4.7.2" />
<PackageReference Update="System.ValueTuple" Version="4.5.0" />
</ItemGroup>
<ItemGroup>

Просмотреть файл

@ -58,10 +58,10 @@ namespace Microsoft.ML.Probabilistic.Tutorials
z[n].InitialiseTo(zInit[n]);
// The inference
InferenceEngine ie = new InferenceEngine();
Console.WriteLine("Dist over pi=" + ie.Infer(weights));
Console.WriteLine("Dist over means=\n" + ie.Infer(means));
Console.WriteLine("Dist over precs=\n" + ie.Infer(precs));
InferenceEngine engine = new InferenceEngine();
Console.WriteLine("Dist over pi=" + engine.Infer(weights));
Console.WriteLine("Dist over means=\n" + engine.Infer(means));
Console.WriteLine("Dist over precs=\n" + engine.Infer(precs));
}
/// <summary>

Просмотреть файл

@ -26,9 +26,9 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Msagl" Version="1.1.1" />
<PackageReference Include="Microsoft.Msagl.Drawing" Version="1.1.1" />
<PackageReference Include="Microsoft.Msagl.GraphViewerGdi" Version="1.1.1" />
<PackageReference Include="Microsoft.Msagl" Version="1.1.3" />
<PackageReference Include="Microsoft.Msagl.Drawing" Version="1.1.3" />
<PackageReference Include="Microsoft.Msagl.GraphViewerGdi" Version="1.1.3" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<EmbeddedResource Update="*.resx">

Просмотреть файл

@ -4,7 +4,7 @@
#light
namespace BayesPointTutorial
namespace TestFSharp
open System
open Microsoft.ML.Probabilistic.Distributions
@ -18,7 +18,7 @@ open Microsoft.ML.Probabilistic.FSharp
module bayes =
// The training model
let bayesTestFunc() =
let BayesPointMachineExample() =
Console.WriteLine("\n=====================Running Bayes Point Machine Tutorial====================\n");
let noise = 0.1
let len = Variable.New<int>()

Просмотреть файл

@ -4,7 +4,7 @@
#light
namespace ClinicalTrialTutorial
namespace TestFSharp
open System
open Microsoft.ML.Probabilistic.Models
@ -51,7 +51,7 @@ module clinical =
Variable.IfBlock isEffective f1 f2
()
let clinicalTestFunc() =
let clinicalTrialTutorial() =
Console.WriteLine("\n====================Running Clinical Trial Tutorial==========================\n");
// The inference

Просмотреть файл

@ -1,4 +1,4 @@
namespace DifficultyAbilityExample
namespace TestFSharp
open System.Collections.Generic
open Microsoft.ML.Probabilistic

Просмотреть файл

@ -4,7 +4,7 @@
#light
namespace GaussianRangesTutorial
namespace TestFSharp
open System
open Microsoft.ML.Probabilistic
@ -19,7 +19,7 @@ open Microsoft.ML.Probabilistic.FSharp
// The model
module ranges =
let rangesTestFunc() =
let learningAGaussianWithRanges() =
Console.WriteLine("\n======================Running Gaussian Ranges Tutorial=======================\n");
// The model
let len = Variable.New<int>()

Просмотреть файл

@ -4,7 +4,7 @@
#light
namespace MixtureGaussiansTutorial
namespace TestFSharp
open System
open Microsoft.ML.Probabilistic
@ -35,7 +35,7 @@ module mixture =
//-----------------------------------
// The model
//----------------------------------
let mixtureTestFunc() =
let mixtureOfGaussiansTutorial() =
Console.WriteLine("\n=================Running Mixture of Gaussians Tutorial=======================\n");
// Define a range for the number of mixture components
let k = Range(2)

Просмотреть файл

@ -1,22 +0,0 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#light
open System
open System.Diagnostics
let coreAssemblyInfo = FileVersionInfo.GetVersionInfo(typeof<Object>.Assembly.Location)
printfn "%s .NET version %s mscorlib %s" (if Environment.Is64BitProcess then "64-bit" else "32-bit") (Environment.Version.ToString ()) coreAssemblyInfo.ProductVersion
//main Smoke Test .............................................
let _ = TwoCoinsTutorial.coins.twoCoinsTestFunc()
let _ = TruncatedGaussianTutorial.truncated.truncatedTestFunc()
let _ = GaussianRangesTutorial.ranges.rangesTestFunc()
let _ = ClinicalTrialTutorial.clinical.clinicalTestFunc()
let _ = BayesPointTutorial.bayes.bayesTestFunc()
let _ = MixtureGaussiansTutorial.mixture.mixtureTestFunc()
let _ = DifficultyAbilityExample.DifficultyAbility.main()
Console.ReadLine() |> ignore

Просмотреть файл

@ -2,7 +2,9 @@
<Import Project="$(MSBuildThisFileDirectory)..\..\build\common.props" />
<PropertyGroup>
<OutputType>Exe</OutputType>
<AssemblyName>TestFSharp</AssemblyName>
<RootNamespace>TestFSharp</RootNamespace>
<OutputType>Exe</OutputType>
<PlatformTarget>AnyCPU</PlatformTarget>
<Configurations>Debug;Release;DebugFull;DebugCore;ReleaseFull;ReleaseCore</Configurations>
</PropertyGroup>
@ -26,21 +28,49 @@
</PropertyGroup>
</Otherwise>
</Choose>
<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.1'">
<DefineConstants>$(DefineConstants);NETCORE;NETSTANDARD;NETSTANDARD2_0</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(TargetFramework)' == 'net461'">
<DefineConstants>$(DefineConstants);NET45;NETFULL</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU' OR '$(Configuration)|$(Platform)'=='DebugFull|AnyCPU' OR '$(Configuration)|$(Platform)'=='DebugCore|AnyCPU'">
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
<DefineConstants>$(DefineConstants);DEBUG</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU' OR '$(Configuration)|$(Platform)'=='ReleaseFull|AnyCPU' OR '$(Configuration)|$(Platform)'=='ReleaseCore|AnyCPU'">
<DebugType>portable</DebugType>
<DebugSymbols>true</DebugSymbols>
<Optimize>true</Optimize>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FSharp.Core" Version="4.3.4" />
<PackageReference Update="System.ValueTuple" Version="4.5.0" />
<Compile Include="..\..\src\Shared\SharedAssemblyFileVersion.fs">
<Link>SharedAssemblyFileVersion.fs</Link>
</Compile>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Runtime\Runtime.csproj" />
<ProjectReference Include="..\..\src\Compiler\Compiler.csproj" />
<ProjectReference Include="..\..\src\FSharpWrapper\FSharpWrapper.fsproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="FSharp.Core" Version="4.7.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Update="System.ValueTuple" Version="4.5.0" />
</ItemGroup>
<ItemGroup>
<Compile Include="DifficultyAbility.fs" />
<Compile Include="..\..\src\Shared\SharedAssemblyFileVersion.fs">
<Link>SharedAssemblyFileVersion.fs</Link>
</Compile>
<Compile Include="AssemblyInfo.fs" />
<Compile Include="BayesPointMachine.fs" />
<Compile Include="ClinicalTrial.fs" />
@ -48,7 +78,7 @@
<Compile Include="MixtureofGaussians.fs" />
<Compile Include="TruncatedGaussian.fs" />
<Compile Include="TwoCoins.fs" />
<Compile Include="Program.fs" />
<Compile Include="TestTutorials.fs" />
<None Include="App.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>

Просмотреть файл

@ -0,0 +1,29 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#light
namespace TestFSharp
open System
open System.Diagnostics
open Xunit
module main =
[<Fact>]
let testTutorials() =
let coreAssemblyInfo = FileVersionInfo.GetVersionInfo(typeof<Object>.Assembly.Location)
printfn "%s .NET version %s mscorlib %s" (if Environment.Is64BitProcess then "64-bit" else "32-bit") (Environment.Version.ToString ()) coreAssemblyInfo.ProductVersion
//main Smoke Test .............................................
coins.twoCoinsTutorial()
truncated.truncatedGaussianTutorial()
ranges.learningAGaussianWithRanges()
clinical.clinicalTrialTutorial()
bayes.BayesPointMachineExample()
mixture.mixtureOfGaussiansTutorial()
DifficultyAbility.main()
//Console.ReadLine() |> ignore
testTutorials()

Просмотреть файл

@ -4,7 +4,7 @@
#light
namespace TruncatedGaussianTutorial
namespace TestFSharp
open System
open Microsoft.ML.Probabilistic.Models
@ -17,7 +17,7 @@ open Microsoft.ML.Probabilistic.FSharp
// The Model
module truncated =
let truncatedTestFunc() =
let truncatedGaussianTutorial() =
Console.WriteLine("\n====================Running Truncated Gaussian Tutorial======================\n");
let threshold = (Variable.New<float>()).Named("threshold")
let x = Variable.GaussianFromMeanAndVariance(0.0,1.0).Named("x")

Просмотреть файл

@ -4,7 +4,7 @@
#light
namespace TwoCoinsTutorial
namespace TestFSharp
open System
open Microsoft.ML.Probabilistic.Models
@ -16,8 +16,7 @@ open Microsoft.ML.Probabilistic.FSharp
//-----------------------------------------------------------------------------------
module coins =
let twoCoinsTestFunc() =
let twoCoinsTutorial() =
Console.WriteLine("\n========================Running Two Coins Tutorial========================\n");
let firstCoin = Variable.Bernoulli(0.5)
let secondCoin = Variable.Bernoulli(0.5)

Просмотреть файл

@ -58,8 +58,11 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>

Просмотреть файл

@ -0,0 +1,38 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>79b39d08-df06-47b8-b7ee-15e30eb9a209</ProjectGuid>
<ProjectHome>.</ProjectHome>
<StartupFile>test_tutorials.py</StartupFile>
<SearchPath>
</SearchPath>
<WorkingDirectory>.</WorkingDirectory>
<OutputPath>.</OutputPath>
<Name>TestPython</Name>
<RootNamespace>TestPython</RootNamespace>
<TestFramework>pytest</TestFramework>
<UnitTestPattern>Test*.py</UnitTestPattern>
<UnitTestRootDirectory>.</UnitTestRootDirectory>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DebugSymbols>true</DebugSymbols>
<EnableUnmanagedDebugging>false</EnableUnmanagedDebugging>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DebugSymbols>true</DebugSymbols>
<EnableUnmanagedDebugging>false</EnableUnmanagedDebugging>
</PropertyGroup>
<ItemGroup>
<Compile Include="test_tutorials.py" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Python Tools\Microsoft.PythonTools.targets" />
<!-- Uncomment the CoreCompile target to enable the Build command in
Visual Studio and specify your pre- and post-build commands in
the BeforeBuild and AfterBuild targets below. -->
<!--<Target Name="CoreCompile" />-->
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
</Project>

Просмотреть файл

@ -0,0 +1,259 @@
# For help using pythonnet:
# https://pythonnet.github.io/
# To stop the debugger in .NET code, follow the instructions at:
# https://mail.python.org/archives/list/pythonnet@python.org/message/AZ4BKHN72PIRAG32ERFY3XS52NVNCL47/
import clr
from System import Boolean, Double, Int32, Array
folder = "../Tests/bin/DebugFull/net461/"
clr.AddReference(folder+"Microsoft.ML.Probabilistic")
clr.AddReference(folder+"Microsoft.ML.Probabilistic.Compiler")
from Microsoft.ML.Probabilistic import *
from Microsoft.ML.Probabilistic.Distributions import VectorGaussian, Beta, Bernoulli, Discrete
from Microsoft.ML.Probabilistic.Math import Rand, Vector, DenseVector, PositiveDefiniteMatrix
from Microsoft.ML.Probabilistic.Models import Variable, InferenceEngine, Range, VariableArray
from Microsoft.ML.Probabilistic.Compiler.Reflection import Invoker
def TwoCoins():
firstCoin = Variable.Bernoulli(0.5)
secondCoin = Variable.Bernoulli(0.5)
bothHeads = firstCoin.op_BitwiseAnd(firstCoin, secondCoin)
engine = InferenceEngine()
print("Probability both coins are heads: %s" % engine.Infer(bothHeads))
bothHeads.ObservedValue = False
print("Probability distribution over firstCoin: %s" % engine.Infer(firstCoin))
def TruncatedGaussianEfficient():
threshold = Variable.New[Double]()
x = Variable.GaussianFromMeanAndVariance(0.0, 1.0)
Variable.ConstrainTrue(x.op_GreaterThan(x,threshold))
engine = InferenceEngine()
for thresh in [i*0.1 for i in range(11)]:
threshold.ObservedValue = thresh
print("Dist over x given thresh of %s = %s" % (thresh, engine.Infer(x)))
def LearningAGaussian():
# Restart the infer.NET random number generator
Rand.Restart(12347)
data = [Rand.Normal(0.0, 1.0) for i in range(100)]
mean = Variable.GaussianFromMeanAndVariance(0.0, 1.0).Named("mean")
precision = Variable.GammaFromShapeAndScale(1.0, 1.0).Named("precision")
for i in range(len(data)):
x = Variable.GaussianFromMeanAndPrecision(mean, precision)
x.ObservedValue = data[i]
engine = InferenceEngine()
print("mean=%s" % engine.Infer(mean))
print("precision=%s" % engine.Infer(precision))
def LearningAGaussianWithRanges():
# Restart the infer.NET random number generator
Rand.Restart(12347)
data = [Rand.Normal(0.0, 1.0) for i in range(100)]
mean = Variable.GaussianFromMeanAndVariance(0.0, 1.0).Named("mean")
precision = Variable.GammaFromShapeAndScale(1.0, 1.0).Named("precision")
data_range = Range(len(data)).Named("n")
x = Variable.Array[Double](data_range)
v = Variable.GaussianFromMeanAndPrecision(mean, precision).ForEach(data_range)
x.set_Item(data_range, v)
x.ObservedValue = data
engine = InferenceEngine()
print("mean=%s" % engine.Infer(mean))
print("precision=%s" %engine.Infer(precision))
def BayesPointMachine(incomes, ages, w, y):
j = y.Range
xData = [Vector.FromArray(income, age, 1) for income, age in zip(incomes, ages)]
x = VariableObserved(xData, j)
# The following does not work in pythonnet:
#x = Variable.Observed[Vector](Array[Vector](xData))
noise = 0.1
ip = Variable.InnerProduct(w, x.get_Item(j))
v = Variable.GaussianFromMeanAndVariance(ip, noise)
v = v.op_GreaterThan(v, 0.0)
y.set_Item(j, v)
def BayesPointMachineExample():
incomes = [63, 16, 28, 55, 22, 20]
ages = [38, 23, 40, 27, 18, 40]
will_buy = [True, False, True, True, False, False]
# The following does not work in pythonnet:
#y = Variable.Observed[bool](will_buy)
#y = Variable.Observed[bool].Overloads[Array[bool]](will_buy)
y = VariableObserved(will_buy)
eye = PositiveDefiniteMatrix.Identity(3)
m = Vector.Zero(3)
vg = VectorGaussian(m, eye)
w = Variable.Random[Vector](vg)
BayesPointMachine(incomes, ages, w, y)
engine = InferenceEngine()
wPosterior = engine.Infer(w)
print("Dist over w=\n%s" % wPosterior)
incomesTest = [ 58, 18, 22 ]
agesTest = [ 36, 24, 37 ]
ytest = Variable.Array[bool](Range(len(agesTest)))
BayesPointMachine(incomesTest, agesTest, Variable.Random[Vector](wPosterior), ytest)
print("output=\n%s" % engine.Infer(ytest));
def ClinicalTrial():
# Data from clinical trial
control_group_data = [False, False, True, False, False]
control_group = VariableObserved(control_group_data)
treated_group_data = [True, False, True, True, True]
treated_group = VariableObserved(treated_group_data)
i = control_group.Range
j = treated_group.Range
# Prior on being effective treatment
is_effective = Variable.Bernoulli(0.5)
# if block
if_var = Variable.If(is_effective)
# Model if treatment is effective
probIfControl = Variable.Beta(1.0, 1.0)
t = Variable.Bernoulli(probIfControl).ForEach(i)
control_group.set_Item(i, t)
probIfTreated = Variable.Beta(1.0, 1.0)
t = Variable.Bernoulli(probIfTreated).ForEach(j)
treated_group.set_Item(j, t)
if_var.Dispose()
# A bit of background
if_var = Variable.IfNot(is_effective)
# Model if treatment is not effective
prob_all = Variable.Beta(1.0, 1.0)
control_group.set_Item(i, Variable.Bernoulli(prob_all).ForEach(i))
treated_group.set_Item(j, Variable.Bernoulli(prob_all).ForEach(j))
if_var.Dispose()
# Clinical accuracy
engine = InferenceEngine()
print("Probability treatment has an effect = %s" % engine.Infer(is_effective))
print("Probability of good outcome if given treatment = %s" % engine.Infer[Beta](probIfTreated).GetMean())
print("Probability of good outcome if control = %s" % engine.Infer[Beta](probIfControl).GetMean())
def MixtureOfGaussians():
# Define a range for the number of mixture components
k = Range(2)
# Mixture component means
means = Variable.Array[Vector](k)
means_k = Variable.VectorGaussianFromMeanAndPrecision(Vector.FromArray(0.0, 0.0), PositiveDefiniteMatrix.IdentityScaledBy(2, 0.01)).ForEach(k)
means.set_Item(k, means_k)
# Mixture component precisions
precs = Variable.Array[PositiveDefiniteMatrix](k).Named("precs")
precs_k = Variable.WishartFromShapeAndScale(100.0, PositiveDefiniteMatrix.IdentityScaledBy(2, 0.01)).ForEach(k)
precs.set_Item(k, precs_k)
# Mixture weights
weights = Variable.Dirichlet(k, [ 1.0, 1.0 ]).Named("weights")
# Create a variable array which will hold the data
n = Range(300).Named("n")
data = Variable.Array[Vector](n).Named("x")
# Create latent indicator variable for each data point
z = Variable.Array[Int32](n).Named("z")
# The mixture of Gaussians model
forEachBlock = Variable.ForEach(n)
z.set_Item(n, Variable.Discrete(weights))
switchBlock = Variable.Switch(z.get_Item(n))
data.set_Item(n, Variable.VectorGaussianFromMeanAndPrecision(means.get_Item(z.get_Item(n)), precs.get_Item(z.get_Item(n))))
switchBlock.CloseBlock()
forEachBlock.CloseBlock()
# Attach some generated data
data.ObservedValue = GenerateData(n.SizeAsInt)
# Initialise messages randomly to break symmetry
zInit = Variable.Array[Discrete](n).Named("zInit")
zInit.ObservedValue = [Discrete.PointMass(Rand.Int(k.SizeAsInt), k.SizeAsInt) for i in range(n.SizeAsInt)]
# The following does not work in pythonnet:
#z.get_Item(n).InitialiseTo[Discrete].Overloads[Variable[Discrete]](zInit.get_Item(n))
InitialiseTo(z.get_Item(n), zInit.get_Item(n))
# The inference
engine = InferenceEngine();
print("Dist over pi=%s" % engine.Infer(weights))
print("Dist over means=\n%s" % engine.Infer(means))
print("Dist over precs=\n%s" % engine.Infer(precs))
def GenerateData(nData):
trueM1 = Vector.FromArray(2.0, 3.0)
trueM2 = Vector.FromArray(7.0, 5.0)
trueP1 = PositiveDefiniteMatrix(ToClr([ [ 3.0, 0.2 ], [ 0.2, 2.0 ] ]))
trueP2 = PositiveDefiniteMatrix(ToClr([ [ 2.0, 0.4 ], [ 0.4, 4.0 ] ]))
trueVG1 = VectorGaussian.FromMeanAndPrecision(trueM1, trueP1)
trueVG2 = VectorGaussian.FromMeanAndPrecision(trueM2, trueP2)
truePi = 0.6
trueB = Bernoulli(truePi)
# Restart the infer.NET random number generator
Rand.Restart(12347)
return [trueVG1.Sample() if trueB.Sample() else trueVG2.Sample() for j in range(nData)]
def VariableObserved(list, range=None):
t = type(list[0])
array = Array[t](list)
if range == None:
args = [array]
else:
args = [array, range]
return Invoker.InvokeStatic(Variable, "Observed", args)
def InitialiseTo(variable, distribution):
Invoker.InvokeInstance("InitialiseTo", variable, distribution)
def ToClr(matrix):
# See https://github.com/pythonnet/pythonnet/blob/ec424bb0a8f0217c496898395880cf9b99d073e6/src/tests/test_array.py#L1112
nr = len(matrix)
nc = len(matrix[0]) if nr > 0 else 0
t = type(matrix[0][0])
items = Array.CreateInstance(t, nr, nc)
for i in range(nr):
for j in range(nc):
items.SetValue(t(matrix[i][j]), (i, j))
return items
def __list_methods(obj):
for m in dir(obj):
try:
print(m)
except:
pass
def __list_method_overloads(method):
for m in f'{method.__overloads__}'.split('\n'):
print(' '.join(m.split(' ')[1:]))
# Using pytest in Visual Studio:
# https://devblogs.microsoft.com/python/whats-new-for-python-in-visual-studio-16-3-preview-2/
# pytest searches for tests in files named test_*.py or *_test.py
# pytest collects functions with names prefixed by "test", either outside a class or in a class with name prefixed by "Test"
def test_tutorials():
TwoCoins()
TruncatedGaussianEfficient()
LearningAGaussian()
LearningAGaussianWithRanges()
BayesPointMachineExample()
ClinicalTrial()
MixtureOfGaussians()
if __name__ == '__main__':
test_tutorials()

Просмотреть файл

@ -39,6 +39,29 @@ namespace Microsoft.ML.Probabilistic.Tests
return Util.ArrayInit(count, i => (min + i * inc));
}
/// <summary>
/// Demonstrate that EP can solve a linear program
/// </summary>
[Fact]
public void LinearProgrammingTest()
{
// Could repeat inference incrementally while decreasing precision
double precision = 1e-20;
// maximize x+y
// subject to x + 2*y = 1
// x >= 0
// y >= 0
Variable<double> x = Variable.GaussianFromMeanAndPrecision(1.0/precision/precision, precision);
Variable<double> y = Variable.GaussianFromMeanAndPrecision(1.0/precision/precision, precision);
Variable.ConstrainEqual(x + 2 * y, 1);
Variable.ConstrainPositive(x);
Variable.ConstrainPositive(y);
InferenceEngine engine = new InferenceEngine();
Console.WriteLine($"x = {engine.Infer(x)}");
Console.WriteLine($"y = {engine.Infer(y)}");
}
[Fact]
public void TruncatedGammaPowerTest()
{

Просмотреть файл

@ -49,12 +49,15 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<PackageReference Include="xunit.abstractions" Version="2.0.1" />
<PackageReference Include="xunit.analyzers" Version="0.9.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit.abstractions" Version="2.0.3" />
<PackageReference Include="xunit.analyzers" Version="0.10.0" />
<PackageReference Include="xunit.assert" Version="2.3.1" />
<PackageReference Include="xunit.core" Version="2.3.1" />
<PackageReference Include="xunit.extensibility.core" Version="2.3.1" />
@ -79,6 +82,12 @@
<None Update="Data\IRT2PL_10_1000.mat" CopyToOutputDirectory="PreserveNewest" />
<None Update="Data\IRT2PL_10_250.mat" CopyToOutputDirectory="PreserveNewest" />
<None Update="Data\test.mat" CopyToOutputDirectory="PreserveNewest" />
<None Update="sequential.xunit.runner.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="xunit.runner.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Remove="Bugs\*.txt" />
<Content Include="Bugs\*.txt" />

Просмотреть файл

@ -0,0 +1,4 @@
{
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
"methodDisplay": "method"
}

Просмотреть файл

@ -0,0 +1,5 @@
{
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
"methodDisplay": "method",
"parallelizeTestCollections": false
}

Просмотреть файл

@ -0,0 +1,4 @@
{
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
"methodDisplay": "method"
}

Просмотреть файл

@ -16,28 +16,25 @@ then
fi
compath=/bin/${configuration}/netcoreapp3.1/
dlls="Learners/LearnersTests${compath}Microsoft.ML.Probabilistic.Learners.Tests.dll Tests${compath}Microsoft.ML.Probabilistic.Tests.dll TestPublic${compath}TestPublic.dll"
# dotnet command
dotnet='dotnet --fx-version 3.1.3'
# path to the xunit runner
runner=~/.nuget/packages/xunit.runner.console/2.3.1/tools/netcoreapp2.0/xunit.console.dll
projects="Learners/LearnersTests Tests TestPublic TestFSharp"
# filter for parallel test run
parallel_filter='-notrait Platform=x86 -notrait Category=OpenBug -notrait Category=BadTest -notrait Category=CompilerOptionsTest -notrait Category=CsoftModel -notrait Category=ModifiesGlobals -notrait Category=DistributedTest -notrait Category=Performance'
#parallel_filter='-notrait Platform=x86 -notrait Category=OpenBug -notrait Category=BadTest -notrait Category=CompilerOptionsTest -notrait Category=CsoftModel -notrait Category=ModifiesGlobals -notrait Category=DistributedTest -notrait Category=Performance'
parallel_filter='--filter (Platform!=x86)&(Category!=OpenBug)&(Category!=BadTest)&(Category!=CompilerOptionsTest)&(Category!=CsoftModel)&(Category!=ModifiesGlobals)&(Category!=DistributedTest)&(Category!=Performance)'
# filter for sequential test run
sequential_filter='-notrait Platform=x86 -trait Category=CsoftModel -trait Category=ModifiesGlobals -trait Category=DistributedTests -trait Category=Performance -notrait Category=OpenBug -notrait Category=BadTest -notrait Category=CompilerOptionsTest'
#sequential_filter='-notrait Platform=x86 -trait Category=CsoftModel -trait Category=ModifiesGlobals -trait Category=DistributedTests -trait Category=Performance -notrait Category=OpenBug -notrait Category=BadTest -notrait Category=CompilerOptionsTest'
sequential_filter='--filter (Platform!=x86)&(Category!=OpenBug)&(Category!=BadTest)&(Category!=CompilerOptionsTest)&(Category=CsoftModel|Category=ModifiesGlobals|Category=DistributedTests|Category=Performance)'
exitcode=0
index=0
echo -e "\033[44;37m=====================PARALLEL TESTS RUNNING============================\033[0m"
for assembly in $dlls
cp "Tests/parallel.xunit.runner.json" "Tests/xunit.runner.json"
for project in $projects
do
# Please note that due to xUnit issue we need to run tests for each assembly separately
$dotnet "$runner" "$assembly" $parallel_filter -xml "netcoretest-result${index}.xml"
# Please note that due to xUnit issue we need to run tests for each project separately
dotnet test "$project" -c "$configuration" $parallel_filter --logger "trx;logfilename=netcoretest-result${index}.trx"
if [ 0 -ne $? ]
then
echo -e "\033[5;41;1;37mParallel running failure!\033[0m"
@ -49,7 +46,9 @@ do
done
echo -e "\033[44;37m=====================SEQUENTIAL TESTS RUNNING=========================\033[0m"
$dotnet "$runner" "Tests${compath}Microsoft.ML.Probabilistic.Tests.dll" $sequential_filter -parallel none -xml "netcoretest-result${index}.xml"
# See https://xunit.github.io/docs/configuration-files
cp "Tests/sequential.xunit.runner.json" "Tests/xunit.runner.json"
dotnet test Tests -c "$configuration" $sequential_filter --logger "trx;logfilename=netcoretest-result${index}.trx"
if [ 0 -ne $? ]
then
echo -e "\033[5;41;1;37mSequential running failure!\033[0m"
@ -57,5 +56,6 @@ then
else
echo -e "\033[32;1mSequential running success!\033[0m"
fi
cp "Tests/parallel.xunit.runner.json" "Tests/xunit.runner.json"
exit $exitcode