locked
Infer.NET in Matlab (Migrated from community.research.microsoft.com) RRS feed

  • Question

  • TimSal posted on 01-26-2011 10:56 AM

    Hi guys,

    I would like to be able to use the Infer.NET functionality within my own Matlab code. As an experiment I have been trying to do the 'Two Coins' tutorial in Matlab, code:

     

     

    % START INFER.NET COIN EXAMPLE

     

    % add .NET assemblies

    NET.addAssembly('c:\Program Files (x86)\Microsoft Research\Infer.NET 2.3 Beta\bin\Release\Infer.Runtime.dll');

    NET.addAssembly('c:\Program Files (x86)\Microsoft Research\Infer.NET 2.3 Beta\bin\Release\Infer.Compiler.dll');

     

    % import all namespaces

    import MicrosoftResearch.Infer.*

    import MicrosoftResearch.Infer.Models.*

    import MicrosoftResearch.Infer.Distributions.*

     

    % The model

    firstCoin = Variable.Bernoulli(0.5);

    secondCoin = Variable.Bernoulli(0.5);

    bothHeads = firstCoin.op_Equality(firstCoin,secondCoin);

     

    % The inference

    ie = InferenceEngine();

    res = ie.Infer(bothHeads);

     

     

    % END INFER.NET COIN EXAMPLE

     

    This Matlab script fails on the last line with the following error message:

     

    ??? Message: ModelAnalysisTransform failed with 2 error(s) and 0 warning(s):

      Cannot redefine constant variable 'vBernoulli0'. in

    Bernoulli vBernoulli0 = new Bernoulli(0.5)

      Cannot redefine constant variable 'vBernoulli1'. in

    Bernoulli vBernoulli1 = new Bernoulli(0.5)

    Source: Infer.Compiler

    HelpLink:

     

    Is this error due to the limitations of Matlab in dealing with .NET assemblies or am I doing something wrong? Thanks in advance!

    Friday, June 3, 2011 6:17 PM

Answers

  • TimSal replied on 02-16-2011 8:29 AM

    I just reinstalled Matlab, .NET framework and Infer.NET and now the above works fine. Not sure wat the problem was.

    Friday, June 3, 2011 6:18 PM

All replies

  • arrigo replied on 01-26-2011 3:50 PM

    Maybe you need to convert the Matlab variable to .NET types before calling the .NET method? Below is a fragment of code that works for me. I am using MATLAB 7.11.0

     

     

    NetDll = NET.addAssembly(

     

    'C:\MatlabUnwrapping.dll');

    netObj = MatlabUnwrapping.Unwrap;

    convertedImgType = NET.convertArray(Z);

     

     

     

    netObj.UnwrapImage(convertedImgType, 2);

    UWImg=double(convertedImgType);

     

    Friday, June 3, 2011 6:17 PM
  • minka replied on 01-27-2011 9:37 AM

    This works perfectly for me in Matlab 7.11 (R2010b).  By the way, bothHeads should be defined using BitwiseAnd, not Equality.

    Friday, June 3, 2011 6:17 PM
  • TimSal replied on 01-28-2011 5:26 AM

    I just upgraded to the latest version of Infer.NET (was using 2.3) and now it works fine!

    Friday, June 3, 2011 6:17 PM
  • TimSal replied on 02-11-2011 8:12 AM

    Could you please explain how to initialize VariableArray's in Matlab? We can't use the <> brackets used in the C# examples (i.e. Variable.Array<T>(Range r1,Range r2,...)

    Friday, June 3, 2011 6:18 PM
  • minka replied on 02-11-2011 9:00 AM

    To invoke generic methods, Matlab requires you to use NET.invokeGenericMethod.  Generic classes are similar.

    Friday, June 3, 2011 6:18 PM
  • TimSal replied on 02-11-2011 10:47 AM

    could you give an example?

    Friday, June 3, 2011 6:18 PM
  • TimSal replied on 02-16-2011 7:15 AM

    My guess would be to use

     

    r=Range(10);

    test=NET.invokeGenericMethod('MicrosoftResearch.Infer.Models.Variable','Array',{'System.Double'},r)

     

    but that gives the following error:

    ??? Message: Ambiguous match found.

    Source: mscorlib

    Friday, June 3, 2011 6:18 PM
  • TimSal replied on 02-16-2011 8:29 AM

    I just reinstalled Matlab, .NET framework and Infer.NET and now the above works fine. Not sure wat the problem was.

    Friday, June 3, 2011 6:18 PM