locked
Creating Array of Array of Array of Variables (Migrated from community.research.microsoft.com) RRS feed

  • Question

  • akuzmenko posted on 03-10-2011 3:36 AM

    Hello,

    I am trying to build a jagged array of array of array (3 levels) of variables... 

    Using the documentation, I got to the following point... 

                _num1 = Variable.New<int>();
                _r1 = new Range(_num1);

                _num2 = Variable.Array<int>(_r1);
                _r2 = new Range(_num2[_r1]);

    But I am not sure how do I proceed from there? Assuming I create this array, then what is the best way to initialize it with the variables? Can I build a ForEach() block to go through all of them?

    Thank you!

     

    Friday, June 3, 2011 6:31 PM

Answers

  • akuzmenko replied on 03-10-2011 9:55 AM

    Great, thanks Tom.

    However, the shorter alternative (where you set all variables in one line) throws NotImplementedException. The longer version with three ForEach'es works ok.

    Friday, June 3, 2011 6:31 PM

All replies

  • minka replied on 03-10-2011 7:38 AM

    Here is an example of building a 3-deep jagged array:

                      double boolsPrior = 0.3;

                      Range outer = new Range(2).Named("outer");

                      VariableArray<int> middleSizes = Variable.Constant(new int[] { 2, 3 }, outer).Named("middleSizes");

                      Range middle = new Range(middleSizes[outer]).Named("middle");

                      var innerSizes = Variable.Constant(new int[][] {

                            new int[] { 1, 2 }, new int[] { 1,2,3 }

                      }, outer, middle).Named("innerSizes");

                      Range inner = new Range(innerSizes[outer][middle]).Named("inner");

                      var bools = Variable.Array<bool>(Variable.Array<bool>(Variable.Array<bool>(inner), middle), outer).Named("bools");

                      using (Variable.ForEach(outer)) {

                            using (Variable.ForEach(middle)) {

                                  using (Variable.ForEach(inner)) {

                                        bools[outer][middle][inner] = Variable.Bernoulli(boolsPrior);

                                  }

                            }

                      }

     

    Note the last line could be equivalently written without the using statements: 

        bools[outer][middle][inner] = Variable.Bernoulli(boolsPrior).ForEach(outer,middle,inner);

    Friday, June 3, 2011 6:31 PM
  • akuzmenko replied on 03-10-2011 9:55 AM

    Great, thanks Tom.

    However, the shorter alternative (where you set all variables in one line) throws NotImplementedException. The longer version with three ForEach'es works ok.

    Friday, June 3, 2011 6:31 PM
  • The above code doesn't work in Infer.NET 2.5 Beta 2.  Instead, use this:

                double boolsPrior = 0.3;
                Range outer = new Range(2).Named("outer");
                VariableArray<int> middleSizes = Variable.Constant(new int[] { 2, 3 }, outer).Named("middleSizes");
                Range middle = new Range(middleSizes[outer]).Named("middle");
                var innerSizes = Variable.Constant(new int[][] { 
                            new int[] { 1, 2 }, new int[] { 1,2,3 }
                      }, outer, middle).Named("innerSizes");
                Range inner = new Range(innerSizes[outer][middle]).Named("inner");
                var bools = Variable.Array(Variable.Array(Variable.Array<bool>(inner), middle), outer).Named("bools");
                bools[outer][middle][inner] = Variable.Bernoulli(boolsPrior).ForEach(outer,middle,inner);
    

    Friday, October 10, 2014 12:09 PM
    Owner