locked
infer.net tutorials RRS feed

  • Question

  • I'm running through the tutorials in VB.net with Infer.net 2.4 Beta 2 and I can not get tutorial to work.  Attached is my code and the resulting error message.

    Imports MicrosoftResearch.Infer
    Imports MicrosoftResearch.Infer.Models
    Imports MicrosoftResearch.Infer.Distributions
    
    Module Module1
    
        Sub Main()
    
            Console.WriteLine("~~~~~~~~~~~~~ Tutorial One ~~~~~~~~~~~~~~~~~~~~")
            Dim firstcoin As Variable(Of Boolean) = Variable.Bernoulli(0.5)
            Dim secondcoin As Variable(Of Boolean) = Variable.Bernoulli(0.5)
            Dim bothHeads As Variable(Of Boolean) = firstcoin And secondcoin
    
            Dim ie As New InferenceEngine
            Console.WriteLine("Probability both coins are heads: " & ie.Infer(bothHeads).ToString)
            Console.ReadLine()
    
            bothHeads.ObservedValue = False
            Console.WriteLine("Probability distribution over firstCoin: " + ie.Infer(firstcoin).ToString)
            Console.ReadLine()
    
            Console.WriteLine()
            Console.WriteLine("~~~~~~~~~~~~~ Tutorial Two ~~~~~~~~~~~~~~~~~~~~")
            Dim x As Variable(Of Double) = Variable.GammaFromMeanAndVariance(0, 1).Named("x")
            Variable.ConstrainTrue(x > 0.5)
            Dim engine As InferenceEngine = New InferenceEngine
            engine.Algorithm = New ExpectationPropagation
            Console.WriteLine(engine.Infer(x))
            Console.ReadLine()
    
        End Sub
    
    End Module

    The error I receive is on Console.WriteLine(engine.Infer(x))

    MicrosoftResearch.Transforms.TransformFailedException was unhandled
      Message=MessageTransform failed with 4 error(s) and 0 warning(s):
    [0] System.ArgumentException: Gamma is not of type WrappedGaussian for argument 1 of method DoublePlusOp.AAverageConditional
        Parameter Provided Expected
        --------- -------- --------
        sum       Gamma    WrappedGaussian
        b         double   WrappedGaussian
    [1] System.ArgumentException: Gamma is not of type WrappedGaussian for argument 1 of method DoublePlusOp.AAverageConditional
        Parameter Provided Expected
        --------- -------- --------
        sum       Gamma    WrappedGaussian
        b         double   double
    [2] System.ArgumentException: Gamma is not of type TruncatedGaussian for argument 1 of method DoublePlusOp.AAverageConditional
        Parameter Provided Expected
        --------- -------- --------
        sum       Gamma    TruncatedGaussian
        b         double   double
    [3] System.ArgumentException: Gamma is not of type double for argument 1 of method DoublePlusOp.AAverageConditional
        Parameter Provided Expected
        --------- -------- --------
        sum       Gamma    double
        b         double   TruncatedGaussian
    [4] System.ArgumentException: Gamma is not of type Gaussian for argument 1 of method DoublePlusOp.AAverageConditional
        Parameter Provided Expected
        --------- -------- --------
        Sum       Gamma    Gaussian
        b         double   Gaussian
    [5] System.ArgumentException: Gamma is not of type double for argument 1 of method DoublePlusOp.AAverageConditional
        Parameter Provided Expected
        --------- -------- --------
        Sum       Gamma    double
        b         double   Gaussian
    [6] System.ArgumentException: Gamma is not of type Gaussian for argument 1 of method DoublePlusOp.AAverageConditional
        Parameter Provided Expected
        --------- -------- --------
        Sum       Gamma    Gaussian
        b         double   double
    [7] System.ArgumentException: Gamma is not of type double for argument 1 of method DoublePlusOp.AAverageConditional
        Parameter Provided Expected
        --------- -------- --------
        Sum       Gamma    double
        b         double   double
      Could not construct operator method in
    Factor.Difference(x_uses[0], 0.5)
    [0] System.ArgumentException: Gamma is not of type WrappedGaussian for argument 1 of method DoublePlusOp.SumAverageConditional
        Parameter Provided Expected
        --------- -------- --------
        a         Gamma    WrappedGaussian
        b         double   WrappedGaussian
    [1] System.ArgumentException: Gamma is not of type WrappedGaussian for argument 1 of method DoublePlusOp.SumAverageConditional
        Parameter Provided Expected
        --------- -------- --------
        a         Gamma    WrappedGaussian
        b         double   double
    [2] System.ArgumentException: Gamma is not of type double for argument 1 of method DoublePlusOp.SumAverageConditional
        Parameter Provided Expected
        --------- -------- --------
        a         Gamma    double
        b         double   WrappedGaussian
    [3] System.ArgumentException: Gamma is not of type double for argument 1 of method DoublePlusOp.SumAverageConditional
        Parameter Provided Expected
        --------- -------- --------
        a         Gamma    double
        b         double   TruncatedGaussian
    [4] System.ArgumentException: Gamma is not of type TruncatedGaussian for argument 1 of method DoublePlusOp.SumAverageConditional
        Parameter Provided Expected
        --------- -------- --------
        a         Gamma    TruncatedGaussian
        b         double   double
    [5] System.ArgumentException: Gamma is not of type Gaussian for argument 1 of method DoublePlusOp.SumAverageConditional
        Parameter Provided Expected
        --------- -------- --------
        a         Gamma    Gaussian
        b         double   Gaussian
    [6] System.ArgumentException: Gamma is not of type double for argument 1 of method DoublePlusOp.SumAverageConditional
        Parameter Provided Expected
        --------- -------- --------
        a         Gamma    double
        b         double   Gaussian
    [7] System.ArgumentException: Gamma is not of type Gaussian for argument 1 of method DoublePlusOp.SumAverageConditional
        Parameter Provided Expected
        --------- -------- --------
        a         Gamma    Gaussian
        b         double   double
    [8] System.ArgumentException: Gamma is not of type double for argument 1 of method DoublePlusOp.SumAverageConditional
        Parameter Provided Expected
        --------- -------- --------
        a         Gamma    double
        b         double   double
      Could not construct operator method in
    Factor.Difference(x_uses[0], 0.5)
    System.ArgumentException: Gamma is not of type Gaussian for argument 1 of method IsPositiveOp.IsPositiveAverageConditional
    Parameter Provided Expected
    --------- -------- --------
    x         Gamma    Gaussian
      Could not construct operator method in
    Factor.IsPositive(vdouble2_uses[0])
    [0] System.ArgumentException: Gamma is not of type Gaussian for argument 2 of method IsPositiveOp.XAverageConditional
        Parameter  Provided  Expected
        ---------  --------  --------
        isPositive Bernoulli Bernoulli
        x          Gamma     Gaussian
    [1] System.ArgumentException: Gamma is not of type double for argument 2 of method IsPositiveOp.XAverageConditional
        Parameter  Provided  Expected
        ---------  --------  --------
        isPositive Bernoulli Bernoulli
        x          Gamma     double
    [2] System.ArgumentException: Bernoulli is not of type bool for argument 1 of method IsPositiveOp.XAverageConditional
        Parameter  Provided  Expected
        ---------  --------  --------
        isPositive Bernoulli bool
        x          Gamma     Gaussian
    [3] System.ArgumentException: Bernoulli is not of type bool for argument 1 of method IsPositiveOp.XAverageConditional
        Parameter  Provided  Expected
        ---------  --------  --------
        isPositive Bernoulli bool
    [4] System.ArgumentException: MicrosoftResearch.Infer.Distributions.Gamma is not of type MicrosoftResearch.Infer.Distributions.TruncatedGaussian for result of method IsPositiveOp.XAverageConditional
      Could not construct operator method in
    Factor.IsPositive(vdouble2_uses[0])
    
      Source=Infer.Compiler
      StackTrace:
           at MicrosoftResearch.Transforms.TransformResults.ThrowIfErrors(String msg, Boolean treatWarningsAsErrors) in C:\infernetBuilds\17-12-2010_15-34\Compiler\TransformFramework\TransformResults.cs:line 80
           at MicrosoftResearch.Transforms.TransformResults.ThrowIfErrors(String msg) in C:\infernetBuilds\17-12-2010_15-34\Compiler\TransformFramework\TransformResults.cs:line 70
           at MicrosoftResearch.Transforms.TransformerChain.TransformToDeclaration(ITypeDeclaration itd, AttributeRegistry`2 inputAttributes, Boolean trackTransform, Boolean showProgress, Boolean showWarnings) in C:\infernetBuilds\17-12-2010_15-34\Compiler\TransformFramework\TransformerChain.cs:line 68
           at MicrosoftResearch.Infer.ModelCompiler.GetTransformedDeclaration(ITypeDeclaration itd, MethodBase method, AttributeRegistry`2 inputAttributes) in C:\infernetBuilds\17-12-2010_15-34\Compiler\Infer\ModelCompiler.cs:line 418
           at MicrosoftResearch.Infer.ModelCompiler.CompileWithoutParams(ITypeDeclaration itd, MethodBase method, AttributeRegistry`2 inputAttributes) in C:\infernetBuilds\17-12-2010_15-34\Compiler\Infer\ModelCompiler.cs:line 426
           at MicrosoftResearch.Infer.InferenceEngine.Compile() in C:\infernetBuilds\17-12-2010_15-34\Compiler\Infer\InferenceEngine.cs:line 208
           at MicrosoftResearch.Infer.InferenceEngine.BuildAndCompile(Boolean inferOnlySpecifiedVars, IEnumerable`1 vars) in C:\infernetBuilds\17-12-2010_15-34\Compiler\Infer\InferenceEngine.cs:line 602
           at MicrosoftResearch.Infer.InferenceEngine.GetCompiledInferenceAlgorithm(Boolean inferOnlySpecifiedVars, IVariable var) in C:\infernetBuilds\17-12-2010_15-34\Compiler\Infer\InferenceEngine.cs:line 571
           at MicrosoftResearch.Infer.InferenceEngine.InferAll(Boolean inferOnlySpecifiedVars, IVariable var) in C:\infernetBuilds\17-12-2010_15-34\Compiler\Infer\InferenceEngine.cs:line 488
           at MicrosoftResearch.Infer.InferenceEngine.Infer(IVariable var) in C:\infernetBuilds\17-12-2010_15-34\Compiler\Infer\InferenceEngine.cs:line 229
           at ConsoleApplication1.Module1.Main() in C:\Users\Craig\AppData\Local\Temporary Projects\ConsoleApplication1\Module1.vb:line 29
           at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException: 
    


    Thanks for any help,


    Craig

    Thursday, September 29, 2011 9:28 PM

Answers

  • Craig

    This will work if you use Variable.GaussianFromMeanAndVariance rather than Variable.GammaFromMeanAndVariance.

    The part of the model represented by Variable.ConstrainTrue(x > 0.5) is not supported for Gamma-distributed variables. You see the error when you call engine.Infer(x)) because that is the point at which the model is compiled into inference code. At that point the Infer.NET compiler tries to construct 'message operators' (the low level methods called by the generated inference code) with Gamma arguments and is not able to do so but shows you the best set of matching message operators.

    John

    • Marked as answer by cevans3098 Friday, September 30, 2011 3:04 PM
    Friday, September 30, 2011 8:46 AM
    Owner

All replies

  • Craig

    This will work if you use Variable.GaussianFromMeanAndVariance rather than Variable.GammaFromMeanAndVariance.

    The part of the model represented by Variable.ConstrainTrue(x > 0.5) is not supported for Gamma-distributed variables. You see the error when you call engine.Infer(x)) because that is the point at which the model is compiled into inference code. At that point the Infer.NET compiler tries to construct 'message operators' (the low level methods called by the generated inference code) with Gamma arguments and is not able to do so but shows you the best set of matching message operators.

    John

    • Marked as answer by cevans3098 Friday, September 30, 2011 3:04 PM
    Friday, September 30, 2011 8:46 AM
    Owner
  • John,

    Thanks.  Mistake on my part - I think intellisense got me and I didn't look close enough.

    Thanks again,

    Craig

    Friday, September 30, 2011 3:03 PM