locked
System.ArgumentException on duplicate key in Infer.NET code... (Migrated from community.research.microsoft.com) RRS feed

  • Question

  • nbeckman posted on 05-25-2010 6:22 AM

    Hi,

    I am getting an exception from within Infer.NET and I'm not really sure why. The exception is from a duplicate key from some internal dictionary. I'll post the exception trace at the end...

     

    Basically I think I am doing pretty simple stuff. I create a number of Bernoulli random variables with appropriate priors (e.g, Variable.Bernoulli(0.9)), I occasionally add constraints of the form, Variable.ConstraintEqualRandom( (v1 && v2), Variable.Constant(Distributions.Bernouilli(0.9)). And then, on the call to InferenceEngine.Infer, I get the exception.

     

    Any ideas what could be going on? Am I using the API wrong... ?

     

    Thanks,

    Nels

     

    Stack trace:

    Unhandled Exception: System.ArgumentException: An item with the same key has already been added.
       at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
       at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
       at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
       at MicrosoftResearch.Infer.Transforms.GroupTransform.ConvertExpressionStatement(IBlockStatement bs, IExpressionStatement ies) in C:\infernetBuilds\12-11-2009_17-18\Compiler\Infer\Transforms\GroupTransform.cs:line 601
       at MicrosoftResearch.Transforms.CopyTransform.DoConvertStatement(IBlockStatement bs, IStatement ist) in C:\infernetBuilds\12-11-2009_17-18\Compiler\Transform Framework\CopyTransform.cs:line 372
       at MicrosoftResearch.Infer.Transforms.GroupTransform.DoConvertStatement(IBlockStatement bs, IStatement ist) in C:\infernetBuilds\12-11-2009_17-18\Compiler\Infer\Transforms\GroupTransform.cs:line 505
       at MicrosoftResearch.Transforms.CopyTransform.ConvertStatement(IBlockStatement bs, IStatement ist, Object key) in C:\infernetBuilds\12-11-2009_17-18\Compiler\TransformFramework\CopyTransform.cs:line 325
       at MicrosoftResearch.Transforms.CopyTransform.ConvertBlock(IBlockStatement outputBlock, IBlockStatement inputBlock, Object key) in C:\infernetBuilds\12-11-2009_17-18\Compiler\TransformFramework\CopyTransform.cs:line 303
       at MicrosoftResearch.Transforms.CopyTransform.ConvertMethodBody(IMethodDeclaration md, IMethodDeclaration imd) in C:\infernetBuilds\12-11-2009_17-18\Compiler\TransformFramework\CopyTransform.cs:line 283
       at MicrosoftResearch.Infer.Transforms.GroupTransform.ConvertMethodBody(IMethodDeclaration md, IMethodDeclaration imd) in C:\infernetBuilds\12-11-2009_17-18\Compiler\Infer\Transforms\GroupTransform.cs:line 89
       at MicrosoftResearch.Transforms.CopyTransform.ConvertMethod(IMethodDeclaration md, IMethodDeclaration imd) in C:\infernetBuilds\12-11-2009_17-18\Compiler\TransformFramework\CopyTransform.cs:line 268
       at MicrosoftResearch.Transforms.CopyTransform.DoConvertMethod(IMethodDeclaration imd, Object key) in C:\infernetBuilds\12-11-2009_17-18\Compiler\TransformFramework\CopyTransform.cs:line 169
       at MicrosoftResearch.Transforms.CopyTransform.ConvertMethods(ITypeDeclaration td, ITypeDeclaration itd) in C:\infernetBuilds\12-11-2009_17-18\Compiler\TransformFramework\CopyTransform.cs:line 152
       at MicrosoftResearch.Transforms.CopyTransform.ConvertType(Object owner, ITypeDeclaration td, ITypeDeclaration itd) in C:\infernetBuilds\12-11-2009_17-18\Compiler\TransformFramework\CopyTransform.cs:line 64
       at MicrosoftResearch.Transforms.CopyTransform.Transform(ITypeDeclaration itd) in C:\infernetBuilds\12-11-2009_17-18\Compiler\TransformFramework\CopyTransform.cs:line 54
       at MicrosoftResearch.Transforms.CodeTransformer.TransformToDeclaration(ITypeDeclaration typeDecl) in C:\infernetBuilds\12-11-2009_17-18\Compiler\TransformFramework\CodeTransformer.cs:line 116
       at MicrosoftResearch.Transforms.TransformerChain.TransformToDeclaration(ITypeDeclaration itd, AttributeRegistry`2 inputAttributes) in C:\infernetBuilds\12-11-2009_17-18\Compiler\TransformFramework\TransformerChain.cs:line 62
       at MicrosoftResearch.Infer.ModelCompiler.CompileWithoutParams(ITypeDeclaration itd, MethodBase method, AttributeRegistry`2 inputAttributes) in C:\infernetBuilds\12-11-2009_17-18\Compiler\Infer\ModelCompiler.cs:line 235
       at MicrosoftResearch.Infer.Models.ModelBuilder.Compile(InferenceEngine engine, Boolean checkParamsAreSet) in C:\infernetBuilds\12-11-2009_17-18\Compiler\Infer\Models\ModelBuilder.cs:line 184
       at MicrosoftResearch.Infer.InferenceEngine.GetCompiledInferenceAlgorithm(IEnumerable`1 vars, Boolean checkParamsAreSet, Boolean inferOnlySpecifiedVars) in C:\infernetBuilds\12-11-2009_17-18\Compiler\Infer\InferenceEngine.cs:line 402
       at MicrosoftResearch.Infer.InferenceEngine.InferAll(Boolean inferOnlySpecifiedVars, IEnumerable`1 vars) in C:\infernetBuilds\12-11-2009_17-18\Compiler\Infer\InferenceEngine.cs:line 346
       at MicrosoftResearch.Infer.InferenceEngine.Infer(IVariable var) in C:\infernetBuilds\12-11-2009_17-18\Compiler\Infer\InferenceEngine.cs:line 129
       at MicrosoftResearch.Infer.InferenceEngine.Infer[TReturn](IVariable var) in C:\infernetBuilds\12-11-2009_17-18\Compiler\Infer\InferenceEngine.cs:line 194
       at Anek.ProbConstraints.helper@433-9.Invoke(FSharpList`1 ns, FSharpMap`2 result) in C:\Users\t-nelbec\anek\GraphLoader\GraphLoader\ProbConstraints.fs:line 443
       at Anek.ProbConstraints.genFractionFunction(InferenceEngine ie, method_Graph old_graph, node_Var_Maps node_maps, Double f_threshold) in C:\Users\t-nelbec\anek\GraphLoader\GraphLoader\ProbConstraints.fs:line 432

    Friday, June 3, 2011 5:50 PM

Answers

  • nbeckman replied on 06-26-2010 10:41 AM

    John,

     

    Thanks for the response. I know I never got back to you. It's because this bug was intermittent and disappeared when I changed the factor graph that I was creating. If I am able to recreate the bug again, I will send you the code.

     

    Thanks again,

    Nels

    Friday, June 3, 2011 5:51 PM

All replies

  • John Guiver replied on 05-27-2010 9:04 AM

    Hi Nels

    This looks like it could be a bug in the Group transform of the Infer.NET model compiler. However, I have not been able to duplicate it with the 2.3 Beta 4 release. The code I have tried is:

    var v1 = Variable.Bernoulli(0.9);
    var v2 = Variable.Bernoulli(0.7);
    var v3 = Variable.Bernoulli(0.5);
    Variable.ConstrainEqualRandom(v1 & v2, new Bernoulli(0.9));
    Variable.ConstrainEqualRandom(v1 & v3, new Bernoulli(0.9));
    InferenceEngine engine = new InferenceEngine();
    Bernoulli b1 = engine.Infer<Bernoulli>(v1);
    Bernoulli b2 = engine.Infer<Bernoulli>(v2);
    Bernoulli b3 = engine.Infer<Bernoulli>(v3);

    Could you distil your example down to a small bit of code like this so that I can look into it in more detail?

    Thanks

    John

    Friday, June 3, 2011 5:50 PM
  • nbeckman replied on 06-26-2010 10:41 AM

    John,

     

    Thanks for the response. I know I never got back to you. It's because this bug was intermittent and disappeared when I changed the factor graph that I was creating. If I am able to recreate the bug again, I will send you the code.

     

    Thanks again,

    Nels

    Friday, June 3, 2011 5:51 PM