ForEach blocks

When array definitions get complicated, the ForEach(range) block comes to the rescue. Every variable created within a ForEach(range) block is automatically tagged with .ForEach(range). Thus the ForEach block works very similarly to a foreach loop in C# (except it is treated as a single object by the Infer.NET compiler, with corresponding efficiency gains during compilation). For example, if bools and pixel are defined as in Working with arrays and ranges, then:

bools[pixel] = Variable.Bernoulli(0.7).ForEach(pixel) |

Variable.Bernoulli(0.4).ForEach(pixel);

is equivalent to:

using(Variable.ForEach(pixel))
{

bools[pixel] = Variable.Bernoulli(0.7) | Variable.Bernoulli(0.4);

}

ForEach blocks can also be nested, to define multi-dimensional arrays. So the following definition:

doubles2D[pixel,image] = Variable.GaussianFromMeanAndVariance(0,1).ForEach(pixel,image);

is equivalent to:

using(Variable.ForEach(pixel))
{

using (Variable.ForEach(image))
{

doubles2D[pixel, image] = Variable.GaussianFromMeanAndVariance(0,1);

}

}

and also equivalent to:

using (Variable.ForEach(pixel))
{

doubles2D[pixel, image] = Variable.GaussianFromMeanAndVariance(0,1).ForEach(image);

}

Last modified at 11/24/2008 3:37 PM  by John Guiver 
©2009 Microsoft Corporation. All rights reserved.  Terms of Use | Trademarks | Privacy Statement