Answered by:
a variable<vector> sum factor

Question
-
Hello,
I am new to Infer.NET. This is a factor I created as according to add new factor . But it does not work .
using System; using System.Collections.Generic; using System.Linq; using System.Text; using MicrosoftResearch.Infer.Models; using MicrosoftResearch.Infer.Maths; using MicrosoftResearch.Infer; using MicrosoftResearch.Infer.Distributions; using MicrosoftResearch.Infer.Factors; [assembly: MicrosoftResearch.Infer.Factors.HasMessageFunctions] public static class MyFactor { public static Vector SumTwoVector(Vector a, Vector b) { return a+b; } [FactorMethod(typeof(MyFactor), "SumTwoVector",typeof(Variable<Vector>),typeof(Variable<Vector>))] public static class SumTwoVectorOp { public static VectorGaussian SumAverageLogarithm(VectorGaussian vector1,VectorGaussian vector2) { Vector v=null; PositiveDefiniteMatrix m=null; vector1.GetMeanAndVariance(v,m); Vector v2=null; PositiveDefiniteMatrix m2=null; vector2.GetMeanAndVariance(v2,m2); return VectorGaussian.FromMeanAndVariance(v+v2,m+m2); } public static VectorGaussian TwoP1AverageLogarithm(VectorGaussian sum, VectorGaussian vector1) { Vector v=null; PositiveDefiniteMatrix m=null; sum.GetMeanAndVariance(v,m); Vector v2=null; PositiveDefiniteMatrix m2=null; vector1.GetMeanAndVariance(v2,m2); return VectorGaussian.FromMeanAndVariance(v-v2,m); } public static VectorGaussian TwoP2AverageLogarithm(VectorGaussian sum, VectorGaussian vector2) { Vector v=null; PositiveDefiniteMatrix m=null; sum.GetMeanAndVariance(v,m); Vector v2=null; PositiveDefiniteMatrix m2=null; vector2.GetMeanAndVariance(v2,m2); return VectorGaussian.FromMeanAndVariance(v-v2,m); } } }
Is there any special notations should be added to factor method declaration?
I did not see which connects the method and the operator....
In the method declaration, it only accepts ordinary types, right?
Friday, July 13, 2012 2:30 PM
Answers
-
(a) I suggest adding parameter names to your Factor method:
[ParameterNames("Sum", "A", "B")] public static Vector SumTwoVector(Vector a, Vector b) { return a + b; }
(b) Your operator class should have the following attribute (i.e. Vector, not Variable<Vector>)
[FactorMethod(typeof(MyFactor), "SumTwoVector",typeof(Vector),typeof(Vector))]
(c) The operator method names have to reflect the names of the variables ("Sum" "A", "B" given the ParameterNames attribute above. So:
SumAverageLogarithm, AAverageLogarithm, and BAverageLogarithm.
John
- Edited by John GuiverMicrosoft employee, Owner Tuesday, July 17, 2012 2:07 PM
- Marked as answer by invisible911 Sunday, July 22, 2012 7:46 PM
Tuesday, July 17, 2012 1:44 PMOwner
All replies
-
(a) I suggest adding parameter names to your Factor method:
[ParameterNames("Sum", "A", "B")] public static Vector SumTwoVector(Vector a, Vector b) { return a + b; }
(b) Your operator class should have the following attribute (i.e. Vector, not Variable<Vector>)
[FactorMethod(typeof(MyFactor), "SumTwoVector",typeof(Vector),typeof(Vector))]
(c) The operator method names have to reflect the names of the variables ("Sum" "A", "B" given the ParameterNames attribute above. So:
SumAverageLogarithm, AAverageLogarithm, and BAverageLogarithm.
John
- Edited by John GuiverMicrosoft employee, Owner Tuesday, July 17, 2012 2:07 PM
- Marked as answer by invisible911 Sunday, July 22, 2012 7:46 PM
Tuesday, July 17, 2012 1:44 PMOwner -
John,
Thanks for your reply. I modified the codes as you suggested. But when I try to call this method, It alert a compiler error. As the method requires a Vector not a Variable<Vector> .
using System; using System.Collections.Generic; using System.Linq; using System.Text; using MicrosoftResearch.Infer.Models; using MicrosoftResearch.Infer.Factors; using MicrosoftResearch.Infer; using MicrosoftResearch.Infer.Distributions; using MicrosoftResearch.Infer.Maths; public class Class1 { class program { static void Main() { Vector[] pros = new Vector[] { Vector.FromArray(0.3, 0.3,0.4 ) /* */, Vector.FromArray(0.3,0.3,0.4 ) /* */, Vector.FromArray(0.1, 0.4,0.5 ),Vector.FromArray(0.1,0.5,0.4 )}; var Pros = pros.Select(v => VectorGaussian.PointMass(v)).ToArray(); VariableArray<Vector> contributionMatrix; Range K = new Range(4); Range D = new Range(3); Variable<PositiveDefiniteMatrix> MatrixC; MatrixC = PositiveDefiniteMatrix.Identity(3); VariableArray<VectorGaussian> contributionPrior; contributionPrior = Variable.Array<VectorGaussian>(K).Named("contributionprior"); contributionMatrix = Variable.Array<Vector>(K).Named("Emission"); contributionMatrix[K] = Variable<Vector>.Random<VectorGaussian>(contributionPrior[K]); contributionMatrix.SetValueRange(D); contributionPrior.ObservedValue = Pros; InferenceEngine ei = new InferenceEngine(new VariationalMessagePassing()); ei.ShowFactorGraph = true; Variable<Vector> r1 = Variable.VectorGaussianFromMeanAndPrecision(contributionMatrix[0], MatrixC); Variable<Vector> r2 = Variable.VectorGaussianFromMeanAndPrecision(contributionMatrix[1], MatrixC); Variable<Vector> r = MyFactor.SumTwoVector(r1,r2); Console.WriteLine(ei.Infer(r1)); Console.Read(); } } }
- Edited by invisible911 Thursday, July 19, 2012 9:13 PM
Thursday, July 19, 2012 9:12 PM -
You need to use the Factor method on Variable<Vector> to incorporate your factor into your model as described in the first paragraph of http://research.microsoft.com/infernet/docs/How%20to%20add%20a%20new%20factor%20and%20message%20operators.aspx - see link to http://research.microsoft.com/infernet/docs/applying%20functions%20and%20operators%20to%20variables.aspx
Variable<Vector>.Factor<Vector, Vector>(MyFactor.SumTwoVector, r1, r2)
- Edited by John GuiverMicrosoft employee, Owner Friday, July 20, 2012 7:56 AM
Friday, July 20, 2012 7:52 AMOwner