如何处理infer.net和pythonnet?

2024-03-29 14:24:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在Python3.6中使用infer.net(https://dotnet.github.io/infer/)。为此,我安装了pythonnet包,并下载了使用infer.net所需的dll文件。文件/包的导入、新类型的使用都在起作用,但是当我试图推断一个基本模型时,我得到了一个PlatformNotSupportedException,我不知道如何处理这种错误。有人能帮我吗?我用的是Jupyter笔记本这是我用的代码


import clr
import System
import numpy as np

from System import Console
from System import Array
from System import IO
from System import Array, Double, Type

clr.AddReference("Microsoft.ML.Probabilistic")
clr.AddReference("Microsoft.ML.Probabilistic.Compiler")
clr.AddReference("Microsoft.ML.Probabilistic.Learners")
clr.AddReference("System.CodeDom")



#---------import all classes and methods from above namespaces-----------
from Microsoft.ML.Probabilistic.Distributions import *
from Microsoft.ML.Probabilistic.Models import *
from Microsoft.ML.Probabilistic.Collections import *
from Microsoft.ML.Probabilistic.Factors import *
from Microsoft.ML.Probabilistic.Math import *

from Microsoft.ML.Probabilistic.Models import Variable, VariableArray, Range, InferenceEngine
from Microsoft.ML.Probabilistic.Distributions import Gaussian, Gamma


len = Variable.New[int]()
dataRange = Range(len)
x = Variable.Array[float](dataRange)
mean = Variable.GaussianFromMeanAndVariance(0.0, 100.0).Named('mean')
precision = Variable.GammaFromShapeAndScale(1.0, 1.0).Named('precision')
x.set_Item(dataRange,
           Variable.GaussianFromMeanAndPrecision(mean,
                                                 precision).ForEach(dataRange))


data = []  # System.Array.CreateInstance(float, 100)
for i in range(0, 100):
    data.append(np.random.normal(42, 1))

ie = InferenceEngine()
len.ObservedValue = 100
x.ObservedValue = data

engine = InferenceEngine() 
engine.ModelName = 'Gaussian_priors'

engine.Infer(mean)

---------------------------------------------------------------------------
PlatformNotSupportedException             Traceback (most recent call last)
PlatformNotSupportedException: L'opération n'est pas prise en charge sur cette plateforme.
   à Microsoft.CSharp.CSharpCodeGenerator.FromFileBatch(CompilerParameters options, String[] fileNames)
   à Microsoft.CSharp.CSharpCodeGenerator.System.CodeDom.Compiler.ICodeCompiler.CompileAssemblyFromFileBatch(CompilerParameters options, String[] fileNames)
   à Microsoft.ML.Probabilistic.Compiler.CodeCompiler.CompileWithCodeDom(List`1 filenames, List`1 sources, ICollection`1 referencedAssemblies)
   à Microsoft.ML.Probabilistic.Compiler.CodeCompiler.Compile(List`1 filenames, List`1 sources, ICollection`1 referencedAssemblies)

The above exception was the direct cause of the following exception:

PlatformNotSupportedException             Traceback (most recent call last)
<ipython-input-1-c6ab7cf71833> in <module>()
     47 engine.ModelName = 'Gaussian_priors'
     48 
---> 49 engine.Infer(mean)

PlatformNotSupportedException: Current platform is not supported by the current compiler choice Auto. Try a different one.
   à Microsoft.ML.Probabilistic.Compiler.CodeCompiler.Compile(List`1 filenames, List`1 sources, ICollection`1 referencedAssemblies)
   à Microsoft.ML.Probabilistic.Compiler.CodeCompiler.WriteAndCompile(List`1 typeDeclarations)
   à Microsoft.ML.Probabilistic.Compiler.ModelCompiler.CompileWithoutParams[T](List`1 itds)
   à Microsoft.ML.Probabilistic.Models.InferenceEngine.Compile()
   à Microsoft.ML.Probabilistic.Models.InferenceEngine.GetCompiledInferenceAlgorithm(Boolean inferOnlySpecifiedVars, IVariable var)
   à Microsoft.ML.Probabilistic.Models.InferenceEngine.Infer(IVariable var)

Tags: fromimportcompilermodelsmeanvariablesystemml