[译]Infer.NET –一个为喜欢概率的人准备的图书馆
By robot-v1.0
本文链接 https://www.kyfws.com/ai/infer-net-a-library-for-people-who-love-probabilit-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 8 分钟阅读 - 3776 个词 阅读量 0Infer.NET –一个为喜欢概率的人准备的图书馆(译文)
原文地址:https://www.codeproject.com/Articles/1278872/Infer-NET-A-Library-for-People-Who-Love-Probabilit
原文作者:Coding Notes
译文由本站 robot-v1.0 翻译
前言
An introduction to Infer.NET
Infer.NET简介
介绍(Introduction)
Infer.NET是一个开源库,可用于创建概率编程系统.我们可以使用Infer.NET解决许多不同种类的机器学习问题,例如分类,推荐等.在本文中,我将介绍如何在Visual Studio 2017社区中使用infer.NET库. Infer.NET同时支持C#和F#,在本文中我将使用C#.(Infer.NET is an open source library that can be used to create probabilistic programming systems. We can use Infer.NET to solve many different kinds of machine learning problems, such as classification, recommendation, and so on. In this article, I am going to introduce how to use the infer.NET library in Visual Studio 2017 Community. Infer.NET supports both C# and F#, and I am going to use C# in this article.)
背景(Background)
在VS 2017社区中安装Infer.NET库(Installing the Infer.NET Library in VS 2017 Community)
为简单起见,我们将创建一个控制台应用程序C#(.NET Framework)并将其命名为(For simplicity, we will create a Console Application C# (.NET Framework) and name it) InferDotNetDemo
.我们还需要选择.NET Framework的4.7版本(或更高版本),因为我们可能会遇到一些早期版本的错误.(. We also need to select the 4.7 version (or later versions) of the .NET Framework because we maybe meet some errors with earlier versions.)
在解决方案资源管理器窗口中,我们还将重命名(In the Solution Explorer window, we also rename the)**Program.cs(Program.cs)**至(to)InferDotNetDemo.cs(InferDotNetDemo.cs):(:)
我们可以通过右键单击安装Infer.NET(We can install the Infer.NET by right-clicking on the) InferDotNetDemo
项目,然后选择管理NuGet软件包:(project and choosing Manage NuGet Packages:)
在NuGet窗口中,选择"浏览"标签,然后在"搜索"字段中输入" Infer.net".最后,我们选择(In the NuGet window, we select the Browse tab and enter ‘Infer.net’ in the Search field. Finally, we select)Microsoft.ML.Probabilistic.Compiler(Microsoft.ML.Probabilistic.Compiler)然后点击(and click the)安装(Install)按钮:(button:)
在预览更改中单击确定.(Clicking OK in the Preview Changes.)
然后点击(and then clicking)我接受(I Accept)在许可接受中:(in the License Acceptance:)
几秒钟后,Visual Studio将在"输出"窗口中响应一条消息:(After a few seconds, Visual Studio will respond with a message in the Output window:)
一个Infer.NET程序(An Infer.NET Program)
Infer.NET程序包括三个关键步骤:(An Infer.NET program includes three key steps:)
- 步骤1:定义概率模型(Step 1: Definition of a probabilistic model)
所有Infer.NET程序都需要定义一个概率模型.在我们的演示中,我们可以通过定义分布中的随机变量来定义模型.(All Infer.NET programs need a probabilistic model to be defined. In our demo, we can define a model by defining random variables from distributions.)
-
步骤2:建立推理引擎(Step 2: Creation of an inference engine)通过使用推理引擎来实现所有推理(All inference is achieved through the use of an inference engine by using)
InferenceEngine
类.(class.) -
步骤3:执行推理查询(Step 3: Execution of an inference query)给定一个推理引擎,您可以使用以下方法查询变量的边际分布(Given an inference engine, you can query marginal distributions over variables using)
Infer()
.(.)
使用代码(Using the Code)
首先包括Infer.NET名称空间:(Let’s start by including the Infer.NET namespaces:)
using Microsoft.ML.Probabilistic.Models;
using Microsoft.ML.Probabilistic.Algorithms;
using Microsoft.ML.Probabilistic.Distributions;
现在,我将通过以下两个示例来说明如何在概率编程中使用infer.NET库.(And now, I am going to explain how to use infer.NET library in probabilistic programming through two examples below.)
概率概论-一个简单的例子(An Introduction to Probability - A simple example)
假设我们有一个包含六个球的盒子-一个红色球和五个蓝色球.我们被允许随机捡起2次球:(Assume that we have a box that contains six balls – one red ball and five blue balls. We are allowed to pick up a ball randomly 2 times:)
当我们捡起一个球时,有两种可能的结果:红色球或蓝色球.选红球的概率为p =1/6 =0.17,而选蓝球的概率为q =1 – p =5/6 =0.83.下表列出了如果我们两次捡起一个球可能发生的四种可能的结果:(When we pick up one ball, there are two possible outcomes: red ball or blue ball. The probability of picking a red ball is p = 1/6 = 0.17 and the probability of picking a blue ball is q = 1 – p = 5/6 = 0.83. The four possible outcomes that could occur if we picked up a ball twice are listed below table:)
为了表示每个选择,我们可以使用一个布尔变量,其中(To represent each picking, we can use a boolean variable where)真正(true)代表"挑红球",并且(represents “picking a red ball” and)假(false)代表"选一个蓝色的球".布尔值上具有一定概率为真的分布称为伯努利分布.因此,我们可以通过根据伯努利分布创建一个布尔型随机变量来模拟每个采摘.(represents “picking a blue ball”. A distribution over a boolean value with some probability of being true is called a Bernoulli distribution. So we can simulate each picking by creating a boolean random variable from a Bernoulli distribution.)
请注意,第一拣选和第二拣选是独立的.因此,在第一顺位选一个红球和在第二顺位选一个红球的概率是(1/6 * 1/6)=0.17 * 0.17 =0.0289(您可以找到有关伯努利分布的详细信息(Note that First Picking and Second Picking are independent. Hence, the probability of picking a red ball on First Pick and a red ball on Second Pick is (1/61/6) = 0.170.17 = 0.0289 (you can find out details about Bernoulli distribution) 这里(here) ).().)
到目前为止,我们可以为程序编写一些C#代码行.首先,我们将通过定义伯努利分布中的随机变量以1/6 =0.17成立的概率来定义概率模型:(So far, we can write some lines of C# code for our program. First, we are going to define a probabilistic model by defining random variables from a Bernoulli distribution with a 1/6 = 0.17 probability of being true:)
Variable<bool> firstPicking = Variable.Bernoulli(0.17);
Variable<bool> secondPicking = Variable.Bernoulli(0.17);
产生随机变量的另一种方法是使用包含其他随机变量的表达式派生它,如下所示:(Another way of making a random variable is to derive it using an expression containing other random variables like so:)
Variable<bool> bothReds = firstPicking & secondPicking;
这里,(Here,) bothReds
是(is) true
只有当两者(only if both) firstPicking
和(and) secondPicking
是(are) true
因此,它代表了两个Pickings是一个红球的情况.(and hence it represents the situation where both Pickings are a red ball.)
其次,我们创建了一个变量(Second, we have created a variable) bothReds
我们将通过创建一个使用默认推理算法(预期传播)的推理引擎来找出其分布:(and we are going to find out its distribution by creating an inference engine which uses the default inference algorithm (Expectation Propagation):)
InferenceEngine ie = new InferenceEngine();
我们可以使用(We can use the) Infer()
该引擎查询边际分布的方法(method of this engine to query marginal distribution of the) bothReds
变量:(variable:)
Console.WriteLine("Probability both pickings are a red ball: " + ie.Infer(bothReds));
我们所有的代码都在(All of our code in the)**InferDotNetDemo.cs(InferDotNetDemo.cs)**文件:(file:)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.ML.Probabilistic.Models;
using Microsoft.ML.Probabilistic.Algorithms;
using Microsoft.ML.Probabilistic.Distributions;
namespace ConsoleApp1
{
class InferDemoProgram
{
static void Main(string[] args)
{
//**************Step 1: Define a probabilistic model*************************
//creating a random variable for first picking
Variable<bool> firstPicking = Variable.Bernoulli(0.17);
//creating a random variable for second picking
Variable<bool> secondPicking = Variable.Bernoulli(0.17);
//creating a random variable based on other random variables
Variable<bool> bothReds = firstPicking & secondPicking;
//**************Step 2: Creating an inference engine*************************
InferenceEngine ie = new InferenceEngine();
//**************Step 3: Execution of an inference query*************************
//Using Expectation Propagation - the default algorithm
if (!(ie.Algorithm is VariationalMessagePassing))
{
Console.WriteLine("Probability both pickings are a red ball: " + ie.Infer(bothReds));
}
else
Console.WriteLine("Not run with Variational Message Passing!");
Console.ReadKey();
}
}
}
运行我们的应用程序并获得如下所示的结果:(Run our application and get the result which can look like this:)
还有一个更复杂的例子(And a more complex example)
在此示例中,我们将使用高斯分布和Gamma分布来构建一个模型,该模型可以基于已知数据集预测一天的温度.我们的(In this example, we are going to use a Gaussian distribution and a Gamma distribution to built a model which can predict temperature of a day based on known-dataset. Our)**InferDotNetDemo(InferDotNetDemo)**程序(program)****可以看起来像这样:(can look like this:)
class InferDemoProgram
{
static void Main(string[] args)
{
//**************Step 1: Define a probabilistic model*************************
//Assume that we know temperature of three days:
//Day 1: 13 Celsius, Day 2: 17 Celsius, and Day 3: 16 Celsius
double[] temp = new double[3] { 13, 17, 16 };
// Creating a Gaussian distribution and a Gamma distribution
// and I also assume that we have some parameters such as mean, precision, shape and scale
Variable<double> averageTemp = Variable.GaussianFromMeanAndPrecision(15, 0.01).Named("Average Temperature");
Variable<double> precision = Variable.GammaFromShapeAndScale(2.0, 0.5).Named("Precision");
// Train the model
// using the Range object to handle the array of data efficiently
Range dataRange = new Range(temp.Length).Named("n");
VariableArray<double> daysTemp = Variable.Array<double>(dataRange);
daysTemp[dataRange] = Variable.GaussianFromMeanAndPrecision(averageTemp, precision).ForEach(dataRange);
daysTemp.ObservedValue = temp;
//**************Step 2: Creating an inference engine*************************
InferenceEngine ie = new InferenceEngine();
//**************Step 3: Execution of an inference query*************************
//Make predictions
//Add a prediction variable and retrain the model
Variable<double> tomorrowsTemp = Variable.GaussianFromMeanAndPrecision(averageTemp, precision).Named("Tomorrows Predicted Temperature");
//get the Gaussian distribution
Gaussian tomorrowsTempDist = ie.Infer<Gaussian>(tomorrowsTemp);
// get the mean
double tomorrowsMean = tomorrowsTempDist.GetMean();
//get the variance
double tomorrowsStdDev = Math.Sqrt(tomorrowsTempDist.GetVariance());
//Using Expectation Propagation - the default algorithm
if (!(ie.Algorithm is VariationalMessagePassing))
{
// Write out the results.
Console.WriteLine("Tomorrows predicted temperature: {0:f2} Celsius plus or minus {1:f2}", tomorrowsMean, tomorrowsStdDev);
// Ask other questions of the model
double probTempLessThan18Celsius = ie.Infer<Bernoulli>(tomorrowsTemp < 18.0).GetProbTrue();
Console.WriteLine("Probability that the temperature is less than 18 Celsius: {0:f2}", probTempLessThan18Celsius);
}
else
Console.WriteLine("Not run with Variational Message Passing!");
Console.ReadKey();
}
}
如果我们运行程序,结果为:(If we run program, the result:)
可视化我们的模型(Visualising our model)
Infer.NET允许我们以以下形式可视化正在执行推理的模型(Infer.NET allows us to visualise the model in which inference is being performed, in the form of a)因子图(factor graph).为此,我们需要分两步实施:(. To do this, we need to implement in two steps:)
- 步骤1:下载并安装(Step 1: download and install) Graphviz(Graphviz)
- 步骤2:设定(Step 2: set the)ShowFactorGraph(ShowFactorGraph)推理引擎的属性(property of the inference engine to)真正(true)
InferenceEngine ie = new InferenceEngine();
ie.ShowFactorGraph = true;
如果我们运行程序(第二个示例),我们将采取以下措施:(if we run program (second example), we will take something like:)
兴趣点(Points of Interest)
在本文中,我仅介绍了基本的Infer.NET库.如果您想了解有关此库的更多信息,可以参考以下一些最佳资源:(In this article, I only introduced the basic Infer.NET library. If you want to discover more about this library, you can refer to some of the best sources below:)
- Infer.NET用户指南(Infer.NET User Guide)
- 通过概率编程进行机器学习作者:Yordan Zaykov(Machine Learning Through Probabilistic Programming By Yordan Zaykov)
- 使用Infer.NET对竞争对手进行评分作者:James McCaffrey(Rating Competitors Using Infer.NET By James McCaffrey)
历史(History)
- 4(4)日(th)2019年3月:初始版本(March, 2019: Initial version)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C# .NET Visual-Studio machine-learning 新闻 翻译