public override Vector MapToVector(params IKEPDist[] msgs) { if (msgs.Length != 1) { string err = string.Format("{0} only works on one distribution", MATLAB_CLASS); throw new ArgumentException(err); } IKEPDist dist = msgs[0]; InitMap(dist); Vector mean = dist.GetMeanVector(); Matrix cov = dist.GetCovarianceMatrix(); Vector wtm = mean * W; Matrix VW = cov * W; Matrix WVW = new Matrix(W.Rows, W.Cols); WVW.SetToElementwiseProduct(VW, W); int dim = W.Rows; Vector wvwVec = (Vector.Zero(dim) + 1) * WVW; double[] cosExp = Enumerable.Range(0, numFeatures).Select( i => Math.Cos(wtm[i] + B[i]) * Math.Exp(-0.5 * wvwVec[i]) ).ToArray(); Vector feature = Math.Sqrt(2.0 / numFeatures) * Vector.FromArray(cosExp); return(feature); }
// public override int NumInputMessages(){ // Debug.Assert(mwidth2s.Count == vwidth2s.Count); // return mwidth2s.Count; // } private Vector ToMVStack(IKEPDist[] msgs) { // stack all means and variances. Divide each by its corresponding // sqrt(gauss_width2). // *** Implementation must match RandFourierGaussMVMap.toMVStack() // in Matlab *** // empty vector Vector meanStack = Vector.Zero(0); Vector varStack = Vector.Zero(0); for (int i = 0; i < msgs.Length; i++) { IKEPDist di = msgs[i]; Vector smean = di.GetMeanVector() * (1 / Math.Sqrt(mwidth2s[i])); Matrix svar = di.GetCovarianceMatrix() * (1 / Math.Sqrt(vwidth2s[i])); // reshape column-wise as in Matlab. // Matrix in Infer.NET is stored row-wise Vector svarVec = Vector.FromArray(svar.Transpose().SourceArray); meanStack = Vector.Concat(meanStack, smean); varStack = Vector.Concat(varStack, svarVec); } Vector mv = Vector.Concat(meanStack, varStack); return(mv); }
public void InitMap(IKEPDist dist) { // dynamically initialize the random feature map if (W == null) { Debug.Assert(B == null); int dim = dist.GetMeanVector().Count; double[] reci = MatrixUtils.Reciprocal(gwidth2); double[] zero = Vector.Zero(dim).ToArray(); W = MatrixUtils.SampleDiagonalVectorGaussian(zero, reci, numFeatures); double[] Bvec = MatrixUtils.UniformVector(0, 2.0 * Math.PI, numFeatures); B = Vector.FromArray(Bvec); // VectorGaussian.SampleFromMeanAndVariance(); } }