示例#1
0
        public new static RFGJointKGG FromMatlabStruct(MatlabStruct s)
        {
            //			s.className=class(this);
            //			s.embed_width2s_cell = this.embed_width2s_cell;
            //			s.outer_width2 = this.outer_width2;
            //			s.numFeatures=this.numFeatures;
            //			s.innerNumFeatures = this.innerNumFeatures;
            //			s.eprodMap=this.eprodMap.toStruct();
            //			s.Wout = this.Wout;
            //			s.Bout = this.Bout;

            string className = s.GetString("className");

            if (!className.Equals(MATLAB_CLASS))
            {
                throw new ArgumentException("The input does not represent a " + MATLAB_CLASS);
            }

            double       outer_width2     = s.GetDouble("outer_width2");
            int          numFeatures      = s.GetInt("numFeatures");
            int          innerNumFeatures = s.GetInt("innerNumFeatures");
            MatlabStruct mapStruct        = s.GetStruct("eprodMap");
            RFGEProdMap  eprodMap         = RFGEProdMap.FromMatlabStruct(mapStruct);
            Matrix       Wout             = s.GetMatrix("Wout");

            if (innerNumFeatures != Wout.Rows)
            {
                throw new ArgumentException("inner #features must be  = #rows of Wout");
            }
            if (numFeatures != Wout.Cols)
            {
                throw new ArgumentException("numFeatures must be = #cols of Wout");
            }
            Vector Bout = s.Get1DVector("Bout");

            if (Bout.Count != numFeatures)
            {
                throw new ArgumentException("Bout must have length = numFeatures");
            }
            RFGJointKGG jointMap = new RFGJointKGG();

            jointMap.outer_width2     = outer_width2;
            jointMap.numFeatures      = numFeatures;
            jointMap.innerNumFeatures = innerNumFeatures;
            jointMap.eprodMap         = eprodMap;
            jointMap.Wout             = Wout;
            jointMap.Bout             = Bout;
            // construct object
            return(jointMap);
        }
示例#2
0
        public void InitMap(DVectorNormal joint)
        {
            // dynamically initialize the random feature map
            if (Wout == null)
            {
                Debug.Assert(Bout == null);
                int totalDim = joint.GetDimension();
                if (totalDim != flattenEmbedWidth2s.Length)
                {
                    throw new SystemException("Expect total dim. of the joint to be = length of params.");
                }

                eprodMap = new RFGEProdMap(flattenEmbedWidth2s, innerNumFeatures);
                Wout     = MatrixUtils.Randn(innerNumFeatures, numFeatures) * (1.0 / Math.Sqrt(outer_width2));
                double[] Bvec = MatrixUtils.UniformVector(0, 2.0 * Math.PI, numFeatures);
                Bout = Vector.FromArray(Bvec);
                //			VectorGaussian.SampleFromMeanAndVariance();
            }
        }
示例#3
0
        public new static RFGEProdMap FromMatlabStruct(MatlabStruct s)
        {
            //			s.className=class(this);
            //			s.gwidth2=this.gwidth2;
            //			s.numFeatures=this.numFeatures;
            //			s.W=this.W;
            //			s.B=this.B;

            string className = s.GetString("className");

            if (!className.Equals(MATLAB_CLASS))
            {
                throw new ArgumentException("The input does not represent a " + MATLAB_CLASS);
            }

            // Vector of Gaussian width^2 for the mebedding kernel, one for
            // each dimension of the input.
            // Can be a scalar i.e., same param for each dimension.
            //			double[] gwidth2 = s.Get1DDoubleArray("gwidth2");
            int numFeatures = s.GetInt("numFeatures");
            // weight matrix. dim x numFeatures.
            Matrix W = s.GetMatrix("W");

            if (W.Cols != numFeatures)
            {
                throw new ArgumentException("numFeatures should be = #cols of W");
            }
            // coefficients b. a vector of length numFeatures.
            // Drawn form U[0, 2*pi]
            Vector B = s.Get1DVector("B");
            //			int numFeatures = s.GetInt("numFeatures");
            RFGEProdMap map = new RFGEProdMap();

            //			map.gwidth2 = gwidth2;
            map.numFeatures = numFeatures;
            map.W           = W;
            map.B           = B;
            return(map);
        }
示例#4
0
        /**  - Generate a cell array of FeatureMap candidates from medf,
         * a list of factors to be  multiplied with the
         * diagonal of the average covariance matrices.
         *
         * - subsamples can be used to limit the samples used to compute
         * median distance.
         * See RFGJointKGG in Matlab
         */
        public override List <RandomFeatureMap> GenCandidates(
            List <IKEPDist[]> msgs, int[] numFeatures, double[] medianFactors, Random rng)
        {
            if (numFeatures.Length != 2)
            {
                throw new ArgumentException("numFeatures must have length = 2");
            }
            List <DVectorNormal> joints = msgs.Select(m => ToJointGaussian(m)).ToList();
            int             n           = joints.Count;
            int             subsamples  = Math.Min(1500, n);
            List <IKEPDist> jointsBase  = joints.Cast <IKEPDist>().ToList();
            Vector          avgCov      = RFGEProdMap.GetAverageDiagCovariance(jointsBase, subsamples, rng);

            double[] embedWidth2 = avgCov.ToArray();

            // generate candidates
            double meanEmbedMedian2 = KGGaussian <DVectorNormal> .MedianPairwise(
                joints, embedWidth2);

            var mapCandidates = new List <RandomFeatureMap>(medianFactors.Length);

            for (int i = 0; i < medianFactors.Length; i++)
            {
                double medf = medianFactors[i];
                if (medf <= 0)
                {
                    string text = String.Format("medf must be strictly positive. Found i={0}, medf[i]={1}",
                                                i, medf);
                    throw new ArgumentException(text);
                }
                double width2 = meanEmbedMedian2 * medf;
                mapCandidates.Add(new RFGJointKGG(embedWidth2, width2,
                                                  numFeatures[0], numFeatures[1]));
            }
            return(mapCandidates);
        }