locked
initializing the weight matrix - infer.net RRS feed

  • Question

  • Hi everybody,

    I am writing a code to infer a matrix of weights finally. These weights come from different distributions due to the fact that if they are diagonal or non-diagonal elements. I don't understand what is the problem here and why the model compilation fails. Sorry if this is a stupid question, I'm quite new in infer.net.

    Any help is appreciated :-)

    Zahra

    ****   My code is like the following:

    ===

    Range rM = new Range(3);
    Range rD = new Range(3);
    VariableArray2D<bool> vW = Variable.Array<bool>(rM, rD).Named("W");
    using (ForEachBlock pixelBlock = Variable.ForEach(rM))
         {
             using (ForEachBlock imageBlock = Variable.ForEach(rD))
             {
                 using (Variable.If(imageBlock.Index == pixelBlock.Index))
                 {
                      vW[rM, rD] = Variable.Bernoulli(0.2);
                 }
                 using (Variable.IfNot(imageBlock.Index == pixelBlock.Index))
                    {
                        vW[rM, rD] = Variable.Bernoulli(0.2) | Variable.Bernoulli(0.4);
                    }
                }
            }

    Console.WriteLine(engine.Infer(vW));

    ===

    Monday, May 19, 2014 4:22 PM

Answers

  • Hi Zahra,

    I think that the issue you're experiencing is explained here.

    However, by looking at your definition of weightedSum and the placement of the Console.WriteLine, I think you might be confusing modelling code with execution code. Please take a quick look at how Infer.NET works.

    Cheers,
    Yordan

    • Marked as answer by RazinR Wednesday, May 21, 2014 9:34 AM
    Tuesday, May 20, 2014 1:10 PM

All replies

  • Please try the following:

    using (ForEachBlock pixelBlock = Variable.ForEach(rM))
    {
        using (ForEachBlock imageBlock = Variable.ForEach(rD))
        {
            var diagonalElement = Variable.Copy(imageBlock.Index == pixelBlock.Index);
    
            using (Variable.If(diagonalElement))
            {
                vW[rM, rD].SetTo(Variable.Bernoulli(0.2));
            }
    
            using (Variable.IfNot(diagonalElement))
            {
                vW[rM, rD].SetTo(Variable.Bernoulli(0.2) | Variable.Bernoulli(0.4));
            }
        }
    }
    Monday, May 19, 2014 10:46 PM
  • Thanks a lot Yordan :) I understood the case here.

    I have still another question. Would you please have a look at this one too?

    I have a network. Each node's state at time t2 in this network is a function of ( the value of the node itself in t1 (times a coefficient, alpha, which is itself a Gaussian variable) ) plus  { beta x ( 1 over the sum of other node's value times weight w(i,j ) in t1 ) }. So here we have a weight matrix for wij s. I use the diagonal values of this weight matrix for storing alpha (Wii), so they need a different distribution from w.  I used the previous code to define distributions on this weight matrix.

    Now for inference, I have to connect this weight matrix to varT1 (variables at time t1) and varT2, and then observe values for varT1 and varT2 and then the weight matrix is inferred.

    Would you please help me in this case too?

    Thanks a lot in advance.

    Zahra

    ==========The code I wrote is as following:===========

                var beta = Variable.Array<double>(outerRange).Named("beta");
                beta[outerRange] = Variable.GaussianFromMeanAndVariance(1, 0.002).ForEach(outerRange);            

                using (ForEachBlock firstBlock = Variable.ForEach(outerRange))
                {
                    Variable<double> selfEffect = -(w[outerRange,outerRange] * varT1[outerRange]);
                    Variable<double> weightedSum = 1;  // this is to get weighted sum of other nodes' effect on node i
                    using (ForEachBlock secondBlock = Variable.ForEach(innerRange))
                    {
                        var diagonalElement = Variable.Copy(secondBlock.Index == firstBlock.Index);
                        using (Variable.IfNot(diagonalElement))
                        {
                            // Console.WriteLine("summing " + outerRange + " + " + innerRange);
                            weightedSum = weightedSum * w[outerRange, innerRange];
                        }                    
                    }
                    Variable<double> othersEffect = beta[outerRange] * (1 / (1 + weightedSum)); ///?????? error in math.pow(e, weightsum) // NEED SOFTMAX HERE
                    varT2[outerRange] = selfEffect + othersEffect;
                }

    ======== output errors =====================

    When I try to run this, I get an error: vdouble32 was created in condition [vbool5==False] and cannot be used outside.  To give vdouble32 a conditional definition, use SetTo inside [vbool5==False] rather than assignment (=). I don't know if this is even possible to do such a thing.

    In console.WriteLine command, I just don't get the second index's name and I just get "index3" instead of "innerRange" which is innerRange is named to.

    =====  Even if I delete the conditional on diagonalElement, the model does not compile. It says innerRange is not in this context.  =====

    Error in console after the model is compiled:

    summing outerRange  + index3
    Compiling model...IterativeProcessTransform had 3 warning(s).
      [1] GaussianProductOp.ProductAverageConditional(vdouble28_B[outerRange], W_uses_F[outerRange , outerRange][0], _hoist) has quality band Experimental which is less than the recommended quality band (Preview)
      [2] GaussianProductOp.AAverageConditional(1.0, vdouble36_B[outerRange], vdouble 34_F[outerRange]) has quality band Experimental which is less than the recommende d quality band (Preview)
      [3] GaussianProductOp.ProductAverageConditional(vdouble37_B[outerRange ], _hoist 2, vdouble36_F[outerRange ]) has quality band Experimental which is less than the recommended quality band (Preview) done. Compilation failed with 1 error(s)
    \\\Visual Studio 2010\Projects\DynamicPertNetwork\DynamicPertNetwork\bin\Debug\GeneratedSource\Model_EP.cs(439,93) : error CS0103: The name 'index3' does not exist in the current context

    And this is the error which is thrown:

    MicrosoftResearch.Infer.CompilationFailedException was unhandled
      Message=Errors found when compiling generated code for:
      Source=Infer.Compiler
      StackTrace:
           at MicrosoftResearch.Infer.ModelCompiler.CompileWithoutParams(ITypeDeclaration itd, MethodBase method, AttributeRegistry`2 inputAttributes) in C:\infernetBuilds\19-04-2013_13-25\Compiler\Infer\ModelCompiler.cs:line 518
           at MicrosoftResearch.Infer.InferenceEngine.Compile() in C:\infernetBuilds\19-04-2013_13-25\Compiler\Infer\InferenceEngine.cs:line 189
           at MicrosoftResearch.Infer.InferenceEngine.BuildAndCompile(Boolean inferOnlySpecifiedVars, IEnumerable`1 vars) in C:\infernetBuilds\19-04-2013_13-25\Compiler\Infer\InferenceEngine.cs:line 531
           at MicrosoftResearch.Infer.InferenceEngine.GetCompiledInferenceAlgorithm(Boolean inferOnlySpecifiedVars, IVariable var) in C:\infernetBuilds\19-04-2013_13-25\Compiler\Infer\InferenceEngine.cs:line 500
           at MicrosoftResearch.Infer.InferenceEngine.InferAll(Boolean inferOnlySpecifiedVars, IVariable var) in C:\infernetBuilds\19-04-2013_13-25\Compiler\Infer\InferenceEngine.cs:line 415
           at MicrosoftResearch.Infer.InferenceEngine.Infer(IVariable var) in C:\infernetBuilds\19-04-2013_13-25\Compiler\Infer\InferenceEngine.cs:line 210
           at DynamicPertNetwork.Program.Main(String[] args) in \\bitsmb.bit.uni-bonn.de\homes\narimani\visual studio 2010\Projects\DynamicPertNetwork\DynamicPertNetwork\Program.cs:line 110
           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:


    Tuesday, May 20, 2014 10:54 AM
  • Hi Zahra,

    I think that the issue you're experiencing is explained here.

    However, by looking at your definition of weightedSum and the placement of the Console.WriteLine, I think you might be confusing modelling code with execution code. Please take a quick look at how Infer.NET works.

    Cheers,
    Yordan

    • Marked as answer by RazinR Wednesday, May 21, 2014 9:34 AM
    Tuesday, May 20, 2014 1:10 PM
  • Thanks that solved a part of the problem.

    But now using SetTo (I mean:  weightSum.SetTo(weightSum * w[geneRange, geneRange2]) ), I get this error:

    The left-hand side indices (geneRange) do not include the range 'index2', which appears on the right-hand side (perhaps implicitly by an open ForEach block).

    How can I define a variable which is the summation of those variables in a row of matrix?

    For example here I have a matrix, w, and I need a weighted sum which is a weighted summation over one of rows. How can I implement this?

    Thanks in advance :)

    Tuesday, May 20, 2014 2:44 PM
  • Dear Yordan,

    I made a new post for the last question since I think this is not related to current subject. Thank you for your help. I make this one as answered.

    The new post address :

    http://social.microsoft.com/Forums/en-US/00ab9506-4b6f-4839-9141-5f0ccd41fd82/difference-between-setto-and-assignment-using-setto-in-loop-inernet?forum=infer.net

    Wednesday, May 21, 2014 9:34 AM