示例#1
0
        public void WriteRecords(string filePath, Dictionary <string, object> extra = null)
        {
            MatlabWriter writer = new MatlabWriter(filePath);

            WriteGammaList(writer, InPrecision, "inShape", "inRate");
            WriteGammaList(writer, OutPrecision, "outShape", "outRate");

            // MatlabWritter cannot write boolean
            writer.Write("consultOracle", MatrixUtils.ToDouble(ConsultOracle));
            Matrix uncertaintyMat = MatrixUtils.StackColumnsIgnoreNull(Uncertainty);

            // write a Matrix
            writer.Write("uncertainty", uncertaintyMat);
            WriteGammaList(writer, OracleOut, "oraOutShape", "oraOutRate");

            if (extra != null)
            {
                foreach (var kv in extra)
                {
                    writer.Write(kv.Key, kv.Value);
                }
            }

            writer.Dispose();
        }
示例#2
0
        public static void TestWritingMat()
        {
            string       p = Config.PathToSavedFile("test_save_mat.mat");
            MatlabWriter w = new MatlabWriter(p);

            w.Write("b", 2);
            double[] arr = new double[] { 1, 2, 3 };
            w.Write("arr", arr);
            Vector vec = Vector.FromArray(new double[] { 4, 5, 6, 7 });

            w.Write("vec", vec);

            List <double> list = new List <double>(arr);

            w.Write("list", list);
            Matrix m = Matrix.Parse("1 2\n 3 4");

            w.Write("m", m);

            long time = 1329L;

            w.Write("longNum", time);

            List <Matrix> mats = new List <Matrix>();

            mats.Add(Matrix.IdentityScaledBy(2, 3.0));
            mats.Add(Matrix.IdentityScaledBy(3, 4.0));
            w.Write("list_mats", mats);
            w.Dispose();
        }
示例#3
0
        public static void ToLogisticSerializeToMat(string file,
                                                    List <Tuple <Beta, Gaussian, Beta> > msgs,
                                                    Dictionary <string, object> extra)
        {
            int n = msgs.Count;

            double[] outBetaA = new double[n];
            double[] outBetaB = new double[n];

            double[] inNormalMeans     = new double[n];
            double[] inNormalVariances = new double[n];
            // alpha parameters
            double[] inBetaA = new double[n];
            double[] inBetaB = new double[n];

            for (int i = 0; i < msgs.Count; i++)
            {
                Tuple <Beta, Gaussian, Beta> pairI = msgs[i];
                Beta     toBeta       = pairI.Item1;
                Gaussian fromX        = pairI.Item2;
                Beta     fromLogistic = pairI.Item3;

                fromX.GetMeanAndVariance(out inNormalMeans[i], out inNormalVariances[i]);
                outBetaA[i] = toBeta.TrueCount;
                outBetaB[i] = toBeta.FalseCount;
                double alpha = fromLogistic.TrueCount;
                double beta  = fromLogistic.FalseCount;
                inBetaA[i] = alpha;
                inBetaB[i] = beta;
            }

            // write to .mat file
            MatlabWriter matWriter = new MatlabWriter(file);

            matWriter.Write("outBetaA", outBetaA);
            matWriter.Write("outBetaB", outBetaB);
            matWriter.Write("inNormalMeans", inNormalMeans);
            matWriter.Write("inNormalVariances", inNormalVariances);
            matWriter.Write("inBetaA", inBetaA);
            matWriter.Write("inBetaB", inBetaB);

            if (extra != null)
            {
                foreach (var kv in extra)
                {
                    matWriter.Write(kv.Key, kv.Value);
                }
            }
            matWriter.Dispose();
        }
示例#4
0
        public void RecordInferNETTime()
        {
            /**Records time by infer.net*/

            /**
             * Only one W just like in Ali's paper.
             * In practice, we typically observe multiple sets of observations
             * where we want to do inference on the same model with the same
             * parameter.
             */
            Rand.Restart(init_fixed_seed);
            Vector w = Vector.Zero(d);

            Rand.Normal(Vector.Zero(d), PositiveDefiniteMatrix.Identity(d), w);


            // Create the Logistic operator instance only one because we want to use the same
            // one after a new problem (new seed).
            // stopwatch for measuring inference time for each problem
            Stopwatch watch      = new Stopwatch();
            Type      logisticOp = typeof(LogisticOp2);

            LogisticOp2.Watch = watch;
            List <long> allInferTimes = new List <long>();
            var         allPosteriors = new List <VectorGaussian>();

            LogisticOp2.IsCollectLogisticMessages = false;
            LogisticOp2.IsCollectProjMsgs         = false;
            LogisticOp2.IsCollectXMessages        = false;
            for (int seed = seed_from; seed <= seed_to; seed++)
            {
                Rand.Restart(seed);
                double b = 0;
                // combine the bias term into W
                Vector[] X;
                bool[]   Y;
                LogisticRegression.GenData(n, w, b, out X, out Y, seed);

                Console.Write("Y: ");
                StringUtils.PrintArray(Y);

                VectorGaussian wPost;

                // start the watch
                watch.Restart();
                LogisticRegression.InferCoefficientsNoBias(X, Y, out wPost, epIter, logisticOp);
                // stop the watch
                long inferenceTime = watch.ElapsedMilliseconds;
                allInferTimes.Add(inferenceTime);

                allPosteriors.Add(wPost);

                //print
                Console.WriteLine("n: {0}", n);
                Console.WriteLine("d: {0}", d);
                int t = Y.Sum(o => o ? 1 : 0);
                Console.WriteLine("number of true: {0}", t);
                Console.WriteLine("True bias: {0}", b);
                //			Vector meanW = wPost.GetMean();

                Console.WriteLine("True w: {0}", w);
                Console.WriteLine("Inferred w: ");
                Console.WriteLine(wPost);
            }
            string fnameM = string.Format("rec_dnet_n{0}_logistic_iter{1}_sf{2}_st{3}.mat",
                                          n, epIter, seed_from, seed_to);
            string       recordPathM = Config.PathToSavedFile(fnameM);
            MatlabWriter writer      = new MatlabWriter(recordPathM);

            writer.Write("allInferTimes", MatrixUtils.ToDouble(allInferTimes));
            Vector[] postMeans = allPosteriors.Select(vg => vg.GetMean()).ToArray();
            Matrix[] postCovs  = allPosteriors.Select(vg => vg.GetVariance()).ToArray();
            writer.Write("postMeans", postMeans);
            writer.Write("postCovs", postCovs);
            writer.Write("dim", d);
            writer.Write("n", n);
            writer.Write("epIter", epIter);
            writer.Write("seed_from", seed_from);
            writer.Write("seed_to", seed_to);
            writer.Write("init_fixed_seed", init_fixed_seed);
            writer.Dispose();
        }