Ejemplo n.º 1
0
        internal void ProbabilisticIndexMap()
        {
            //TODO: change the path for cross platform using
            double[,] dataIn  = MatlabReader.ReadMatrix(new double[10, 6400 * 3], @"c:\temp\pim\chand.txt", ' ');
            Vector[,] pixData = new Vector[10, 6400];
            for (int i = 0; i < pixData.GetLength(0); i++)
            {
                int ct = 0;
                for (int j = 0; j < pixData.GetLength(1); j++)
                {
                    pixData[i, j] = Vector.FromArray(dataIn[i, ct++], dataIn[i, ct++], dataIn[i, ct++]);
                }
            }
            Range images = new Range(pixData.GetLength(0));
            Range pixels = new Range(pixData.GetLength(1));
            VariableArray2D <Vector> pixelData = Variable.Constant(pixData, images, pixels);

            // For each image we have a palette of L multivariate Gaussians
            Range L = new Range(2);
            VariableArray2D <Vector> means = Variable.Array <Vector>(images, L).Named("means");

            means[images, L] = Variable.VectorGaussianFromMeanAndPrecision(
                Vector.FromArray(0.5, 0.5, 0.5),
                PositiveDefiniteMatrix.Identity(3)).ForEach(images, L);
            VariableArray2D <PositiveDefiniteMatrix> precs = Variable.Array <PositiveDefiniteMatrix>(images, L).Named("precs");

            precs[images, L] = Variable.WishartFromShapeAndScale(1.0, PositiveDefiniteMatrix.Identity(3)).ForEach(images, L);

            // Across all pixels we have a
            VariableArray <Vector> pi = Variable.Array <Vector>(pixels);

            pi[pixels] = Variable.Dirichlet(L, new double[] { 1.1, 1.0 }).ForEach(pixels);
            // For each pixel of each image we have a discrete indicator
            VariableArray2D <int> ind = Variable.Array <int>(images, pixels).Named("ind");

            ind[images, pixels] = Variable.Discrete(pi[pixels]).ForEach(images);

            using (Variable.ForEach(pixels))
            {
                using (Variable.ForEach(images))
                {
                    using (Variable.Switch(ind[images, pixels]))
                    {
                        pixelData[images, pixels] = Variable.VectorGaussianFromMeanAndPrecision(means[images, ind[images, pixels]],
                                                                                                precs[images, ind[images, pixels]]);
                    }
                }
            }
            InferenceEngine ie = new InferenceEngine(new VariationalMessagePassing());

            ie.ShowProgress       = true;
            ie.NumberOfIterations = 5;
            Console.WriteLine("Dist over L: " + ie.Infer(pi));
        }
Ejemplo n.º 2
0
        internal void ProbabilisticIndexMapNoGate()
        {
            //TODO: change path for cross platform using
            double[,] pixData = MatlabReader.ReadMatrix(new double[10, 6400], @"c:\temp\pim\chand2.txt", ' ');
            Range images = new Range(pixData.GetLength(0));
            Range pixels = new Range(pixData.GetLength(1));
            VariableArray2D <double> pixelData = Variable.Constant(pixData, images, pixels);
            //pixelData.QuoteInMSL = false;

            // For each image we have a palette of L multivariate Gaussians
            VariableArray <double> means = Variable.Array <double>(images).Named("means");

            means[images] = Variable.GaussianFromMeanAndPrecision(0.5, 1).ForEach(images);
            VariableArray <double> precs = Variable.Array <double>(images).Named("precs");

            precs[images] = Variable.GammaFromShapeAndScale(1.0, 1.0).ForEach(images);

            // Across all pixels we have a
            VariableArray <Vector> pi = Variable.Array <Vector>(pixels).Named("pi");

            Dirichlet[] dinit = new Dirichlet[pixels.SizeAsInt];
            for (int i = 0; i < dinit.Length; i++)
            {
                double d = Rand.Double();
                dinit[i] = new Dirichlet(1.0 + d / 10, 1.0 - d / 10);
            }
            pi[pixels] = Variable.Dirichlet(new double[] { 1.0 }).ForEach(pixels);
            // For each pixel of each image we have a discrete indicator
            VariableArray2D <int> ind = Variable.Array <int>(images, pixels).Named("ind");

            ind[images, pixels] = Variable.Discrete(pi[pixels]).ForEach(images);

            using (Variable.ForEach(pixels))
            {
                using (Variable.ForEach(images))
                {
                    pixelData[images, pixels] = Variable.GaussianFromMeanAndPrecision(means[images], //10);
                                                                                      precs[images]);
                    Variable.ConstrainEqualRandom(ind[images, pixels], Discrete.Uniform(1));
                }
            }
            InferenceEngine ie = new InferenceEngine(new VariationalMessagePassing());

            ie.ModelName          = "PIM_NoGate";
            ie.NumberOfIterations = 8;
            ie.ShowTimings        = true;
            DistributionArray <Dirichlet> piDist = ie.Infer <DistributionArray <Dirichlet> >(pi);

            //Console.WriteLine("Dist over pi: " + ie.Infer(pi));
            //TODO: change path for cross platform using
            WriteMatrix(piDist.ToArray(), @"C:\temp\pim\results.txt");
        }
Ejemplo n.º 3
0
        public void FactorAnalysisMslTest()
        {
            InferenceEngine engine = new InferenceEngine(new VariationalMessagePassing());

            engine.Compiler.DeclarationProvider = RoslynDeclarationProvider.Instance;
            double[,] dataIn = MatlabReader.ReadMatrix(new double[400, 64],
                                                       Path.Combine(
#if NETCORE
                                                           Path.GetDirectoryName(typeof(VmpMslTests).Assembly.Location), // work dir is not the one with Microsoft.ML.Probabilistic.Tests.dll on netcore and neither is .Location on netfull
#endif
                                                           "Data", "pca.txt"),
                                                       ' ');
            int C = 8;
            engine.ShowProgress                 = true;
            engine.ShowTimings                  = true;
            engine.NumberOfIterations           = 20;
            engine.Compiler.DeclarationProvider = RoslynDeclarationProvider.Instance;
            var ca = engine.Compiler.Compile(FactorAnalysisModel, C, dataIn,
                                             BioTests.RandomGaussianArray(C, dataIn.GetLength(1)),
                                             BioTests.RandomGaussianArray(dataIn.GetLength(0), C));
            ca.Execute(engine.NumberOfIterations);
            DistributionArray2D <Gaussian> wMarginal = ca.Marginal <DistributionArray2D <Gaussian> >("W");
            DistributionArray2D <Gaussian> xMarginal = ca.Marginal <DistributionArray2D <Gaussian> >("x");
            //WriteMatrix(wMarginal.ToArray<Gaussian[,]>(), @"..\..\faresultsW.txt");
            //WriteMatrix(xMarginal.ToArray<Gaussian[,]>(), @"..\..\faresultsX.txt");

            // Reconstruct
            DistributionArray2D <Gaussian> productMarginal = MatrixMultiplyOp.MatrixMultiplyAverageLogarithm(xMarginal, wMarginal, null);
            double error = 0;
            for (int i = 0; i < productMarginal.GetLength(0); i++)
            {
                for (int j = 0; j < productMarginal.GetLength(1); j++)
                {
                    //Assert.True(productMarginal[i,j].GetLogProb(dataIn[i,j]) > -130);
                    error += System.Math.Abs(productMarginal[i, j].GetMean() - dataIn[i, j]);
                }
            }
            error /= productMarginal.Count;
            // error = 0.121231278712027
            Console.WriteLine("error = {0}", error);
            Assert.True(error < 0.15); // C=2: 0.15
        }