Ejemplo n.º 1
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
        }
Ejemplo n.º 2
0
        public int LoadData(int r)
        {
            Rand.Restart(r);
            var xTrainList = new List <Vector>();
            var xTestList  = new List <Vector>();
            var yTrainList = new List <bool>();
            var yTestList  = new List <bool>();
            var ydata      = BioTests.ReadCSV(baseDir + "y.csv");
            var xdata      = BioTests.ReadCSV(baseDir + "x.csv");
            int N          = ydata.GetLength(0);
            int K          = xdata.GetLength(1);

            for (int i = 0; i < N; i++)
            {
                var x = Vector.Zero(K);
                for (int j = 0; j < K; j++)
                {
                    x[j] = xdata[i, j];
                }
                if (Rand.Double() < propTest)
                {
                    yTestList.Add(ydata[i, 0] == 1.0);
                    xTestList.Add(x);
                }
                else
                {
                    yTrainList.Add(ydata[i, 0] == 1.0);
                    xTrainList.Add(x);
                }
            }
            xtrain = xTrainList.ToArray();
            xtest  = xTestList.ToArray();
            ytrain = yTrainList.ToArray();
            ytest  = yTestList.ToArray();
            return(0);
        }