Answered by:
difference between SetTo and assignment (using SetTo in loop)- Iner.NET

Question
-
Hi
I'm trying to infer a weight matrix for a network. Every node's value in time t2 is dependent to it's value in time t1
If I replace SetTo with normal '=', and remove IfNot block, this code works. But I need the If block. Using SetTo, I get this error:
The left-hand side indices (outerRange) do not include the range 'index2', which appears on the right-hand side (perhaps implicitly by an open ForEach block).
It seems I can't use SetTo in a loop for a variable. Then how can I produce this weighted sum (Weighted sum of some values, in which weights are elements of a 2DArray).
I appreciate any help.
Thanks,
Zahra
* in weight matrix diagonal elements are weights Wii and are used for considering the effect of a node on itself at the next time step.
===============
VariableArray2D<double> w = Variable.Array<double>(outerRange, innerRange).Named("W");
/// here I initialize wij prior distributions.
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] * variableT1[outerRange]);
Variable<double> weightSum = Variable.New<double>();
using (ForEachBlock secondBlock = Variable.ForEach(innerRange))
{
var diagonalElement = Variable.Copy(secondBlock.Index == firstBlock.Index);
using (Variable.IfNot(diagonalElement))
{
Console.WriteLine("summing " + outerRange + " + " + innerRange);
weightSum.SetTo(weightSum + (w[outerRange, innerRange] * variableT1[innerRange]));
}
}
Variable<double> othersEffect = beta[outerRange] * Variable.Logistic(weightSum);
variableT2[outerRange] = selfEffect + othersEffect;
}
- Edited by RazinR Wednesday, May 21, 2014 9:57 AM
Wednesday, May 21, 2014 9:32 AM
Answers
-
Put the products w[outerRange,innerRange]*variableT1[innerRange] into an array, then apply Variable.Sum to this array. See the Recommender System example.
- Marked as answer by RazinR Thursday, May 22, 2014 8:36 AM
Wednesday, May 21, 2014 11:24 AMOwner -
You're seeing the correct behaviour. If you haven't observed any data, then the marginal over w will be at its prior. This is explained in Running Inference, and especially in the paragraph on the term 'marginal'. You might find reading our User Guide very helpful.
- Marked as answer by RazinR Thursday, May 22, 2014 8:36 AM
Thursday, May 22, 2014 12:24 AM
All replies
-
Put the products w[outerRange,innerRange]*variableT1[innerRange] into an array, then apply Variable.Sum to this array. See the Recommender System example.
- Marked as answer by RazinR Thursday, May 22, 2014 8:36 AM
Wednesday, May 21, 2014 11:24 AMOwner -
Thank you Tom.
This way it works. I can compile and run the model. But after calling engine.infer, w still has the same distribution as what it was initialized to. Why w does not take any effect in this case? I expect distribution of Variables in wij change after calling infer() method.
Am I missing something here?
Thanks,
Zahra
- Edited by RazinR Wednesday, May 21, 2014 3:05 PM
Wednesday, May 21, 2014 2:00 PM -
What observed values do you set in the model before inferring the posterior over w?Wednesday, May 21, 2014 3:46 PM
-
Thank you so much for replying :)
Sorry to answer late. I want to observe values for variableT1 and variableT2. Not over w.
By the way, when I just write the code above - without observing anything - and I run Infer, I get the same posterior over w. I expect to get something different. Because now w is not independent of distributions over varialbeT1 and variableT2.
Why posterior over w is in this case the same as its prior?
Thanks
Wednesday, May 21, 2014 8:08 PM -
You're seeing the correct behaviour. If you haven't observed any data, then the marginal over w will be at its prior. This is explained in Running Inference, and especially in the paragraph on the term 'marginal'. You might find reading our User Guide very helpful.
- Marked as answer by RazinR Thursday, May 22, 2014 8:36 AM
Thursday, May 22, 2014 12:24 AM -
Yes now I got it. Thank you Yordan :)Thursday, May 22, 2014 8:37 AM