locked
ForEach blocks versus direct ForEach assignment RRS feed

  • Question

  • Hi, I'm not quite sure what the difference is between using ForEach blocks and using ForEach directly in an assignment.

    I'm trying the WetGrassSprinklerRain example, where I have replaced the line:

    CPTWetGrass[S][R] = Variable<Vector>.Random(CPTWetGrassPrior[S][R]);
    

    with:

    CPTWetGrass[S][R] = Variable.DirichletUniform(W).ForEach(S,R);
    


    I'm running only the parameter learning (usage 2) and the line above works fine. 

    However, if I replace it with the following:

    using (Variable.ForEach(S)) 
      using (Variable.ForEach(R)) 
        CPTWetGrass[S][R] = Variable.DirichletUniform(W);
    

    I receive the following error:

    System.Exception: Cannot index vDirichlet6 by Sprinkler[N] since vDirichlet6 has an implicit dependency on S. Try making the dependency explicit by putting vDirichlet6 into an array indexed by S

    In the generated code I see this:

            for(int S = 0; S<2; S++) {
              for(int R = 0; R<2; R++) {
                Dirichlet vDirichlet6 = Dirichlet.Uniform(2);
              }
            }
            Vector[][] CPTWetGrass = new Vector[2][];
            for(int S = 0; S<2; S++) {
              CPTWetGrass[S] = new Vector[2];
            }
            for(int S = 0; S<2; S++) {
              for(int R = 0; R<2; R++) {
                CPTWetGrass[S][R] = Factor.Random<Vector>(vDirichlet6);
              }
            }
    


    That first for loop cannot be correct, it's redeclaring vDirichlet6 every time.

    Is this a bug or am I doing something incorrectly?

    Friday, January 13, 2012 6:52 PM

Answers

  • This is a known bug in Variable.DirichletUniform, when it is used inside of ForEach blocks.  Stick to the first version and you should be fine.
    • Marked as answer by Bryan Rink Wednesday, January 18, 2012 7:09 PM
    Monday, January 16, 2012 2:21 PM
    Owner