private void SparseGPModel() { // Basis Vector Vector[] basis = new Vector[] { Vector.FromArray(new double[2] { 0.2, 0.2 }), Vector.FromArray(new double[2] { 0.2, 0.8 }), Vector.FromArray(new double[2] { 0.8, 0.2 }), Vector.FromArray(new double[2] { 0.8, 0.8 }) }; // The kernel double[] kp = new double[2]; kp[0] = 0.0; kp[1] = 0.0; IKernelFunction kf = new NNKernel(kp, 0.0); // The fixed parameters SparseGPFixed sgpf = new SparseGPFixed(kf, basis); SparseGP agp = new SparseGP(sgpf); IFunction a = Factor.Random(agp); Vector b = Vector.FromArray(0.1, 0.2); double c = Factor.FunctionEvaluate(a, b); InferNet.Infer(c, nameof(c)); }
public void TestNNReadWrite() { double[] logWeightVariances = { -0.123, 0.456, 1.789 }; double logBiasWtVars = System.Math.Log(3.456); NNKernel nnk = new NNKernel(logWeightVariances, logBiasWtVars); TestReadWrite(nnk); }
public void SparseGPTest() { // The basis Vector[] basis = new Vector[] { Vector.FromArray(new double[2] { 0.2, 0.2 }), Vector.FromArray(new double[2] { 0.2, 0.8 }), Vector.FromArray(new double[2] { 0.8, 0.2 }), Vector.FromArray(new double[2] { 0.8, 0.8 }) }; // The kernel IKernelFunction kf = new NNKernel(new double[] { 0.0, 0.0 }, 0.0); // The fixed parameters SparseGPFixed sgpf = new SparseGPFixed(kf, basis); // Alpha and beta Vector alpha = Vector.Zero(basis.Length); PositiveDefiniteMatrix beta = new PositiveDefiniteMatrix(basis.Length, basis.Length); for (int i = 0; i < alpha.Count; i++) { alpha[i] = i; for (int j = 0; j < alpha.Count; j++) { beta[i, j] = alpha.Count - System.Math.Abs(i - j); } } SparseGP a = new SparseGP(sgpf); #if false Rank1Pot r1p = new Rank1Pot(); r1p.Xi = basis[0]; // must be a basis point for this test r1p.Yi = 2.2; r1p.LambdaInv = 0.7; SparseGP b = new SparseGP(sgpf, r1p); DistributionTests.SettableToRatioTest(a, b); #endif DistributionTests.ProductWithUniformTest(a); DistributionTests.RatioWithUniformTest(a); DistributionTests.SettableToTest(a); }
private void GetItemSparseGPModel() { // Basis Vector Vector[] basis = new Vector[] { Vector.FromArray(new double[2] { 0.2, 0.2 }), Vector.FromArray(new double[2] { 0.2, 0.8 }), Vector.FromArray(new double[2] { 0.8, 0.2 }), Vector.FromArray(new double[2] { 0.8, 0.8 }) }; // The kernel double[] kp = new double[2]; kp[0] = 0.0; kp[1] = 0.0; IKernelFunction kf = new NNKernel(kp, 0.0); // The fixed parameters SparseGPFixed sgpf = new SparseGPFixed(kf, basis); SparseGP sgp = new SparseGP(sgpf); IFunction[] array = new IFunction[3]; for (int i = 0; i < 3; i++) { array[i] = Factor.Random(sgp); } IFunction c = Factor.GetItem(array, 1); IFunction[] d = new IFunction[2]; if (true) // preserve the previous assignment { d = Factor.GetItems(array, new int[] { 0, 2 }); } InferNet.Infer(c, nameof(c)); InferNet.Infer(d, nameof(d)); }
public void TestNNKernelDerivs() { NNKernel nnk = new NNKernel(); double[] x1 = { 0.1, 0.2, 0.3 }; double[] x2 = { 0.9, 0.7, 0.5 }; Vector v1 = Vector.FromArray(x1); Vector v2 = Vector.FromArray(x2); List <Vector> xlist = new List <Vector>(2); xlist.Add(v1); xlist.Add(v2); nnk.InitialiseFromData(xlist); Vector x1Vec = Vector.FromArray(x1); Vector x2Vec = Vector.FromArray(x2); TestDerivatives(nnk, x1Vec, x1Vec); TestDerivatives(nnk, x1Vec, x2Vec); }
public void GPClassificationExample() { // The data bool[] ydata = { true, true, false, true, false, false }; Vector[] xdata = new Vector[] { Vector.FromArray(new double[2] { 0, 0 }), Vector.FromArray(new double[2] { 0, 1 }), Vector.FromArray(new double[2] { 1, 0 }), Vector.FromArray(new double[2] { 0, 0.5 }), Vector.FromArray(new double[2] { 1.5, 0 }), Vector.FromArray(new double[2] { 0.5, 1.0 }) }; // Open an evidence block to allow model scoring Variable <bool> evidence = Variable.Bernoulli(0.5).Named("evidence"); IfBlock block = Variable.If(evidence); // Set up the GP prior, which will be filled in later Variable <SparseGP> prior = Variable.New <SparseGP>().Named("prior"); // The sparse GP variable - a distribution over functions Variable <IFunction> f = Variable <IFunction> .Random(prior).Named("f"); // The locations to evaluate the function VariableArray <Vector> x = Variable.Constant(xdata).Named("x"); Range j = x.Range.Named("j"); // The observation model VariableArray <bool> y = Variable.Array <bool>(j).Named("y"); y[j] = (Variable.GaussianFromMeanAndVariance(Variable.FunctionEvaluate(f, x[j]), 0.1) > 0); // Attach the observations y.ObservedValue = ydata; // Close the evidence block block.CloseBlock(); InferenceEngine engine = new InferenceEngine(); // The basis Vector[] basis = new Vector[] { Vector.FromArray(new double[2] { 0.2, 0.2 }), Vector.FromArray(new double[2] { 0.2, 0.8 }), Vector.FromArray(new double[2] { 0.8, 0.2 }), Vector.FromArray(new double[2] { 0.8, 0.8 }) }; for (int trial = 0; trial < 3; trial++) { // The kernel IKernelFunction kf; if (trial == 0) { kf = new SquaredExponential(-0.0); //kf = new LinearKernel(new double[] { 0.0, 0.0 }); } else if (trial == 1) { kf = new SquaredExponential(-0.5); } else { kf = new NNKernel(new double[] { 0.0, 0.0 }, -1.0); } // Fill in the sparse GP prior prior.ObservedValue = new SparseGP(new SparseGPFixed(kf, basis)); // Model score double NNscore = engine.Infer <Bernoulli>(evidence).LogOdds; Console.WriteLine("{0} evidence = {1}", kf, NNscore.ToString("g4")); } // Infer the posterior Sparse GP SparseGP sgp = engine.Infer <SparseGP>(f); // Check that training set is classified correctly Console.WriteLine(); Console.WriteLine("Predictions on training set:"); for (int i = 0; i < ydata.Length; i++) { Gaussian post = sgp.Marginal(xdata[i]); double postMean = post.GetMean(); string comment = (ydata[i] == (postMean > 0.0)) ? "correct" : "incorrect"; Console.WriteLine("f({0}) = {1} ({2})", xdata[i], post, comment); Assert.True(ydata[i] == (postMean > 0.0)); } }
public void Run() { InferenceEngine engine = new InferenceEngine(); if (!(engine.Algorithm is Algorithms.ExpectationPropagation)) { Console.WriteLine("This example only runs with Expectation Propagation"); return; } // The data Vector[] inputs = new Vector[] { Vector.FromArray(new double[2] { 0, 0 }), Vector.FromArray(new double[2] { 0, 1 }), Vector.FromArray(new double[2] { 1, 0 }), Vector.FromArray(new double[2] { 0, 0.5 }), Vector.FromArray(new double[2] { 1.5, 0 }), Vector.FromArray(new double[2] { 0.5, 1.0 }) }; bool[] outputs = { true, true, false, true, false, false }; // Open an evidence block to allow model scoring Variable <bool> evidence = Variable.Bernoulli(0.5).Named("evidence"); IfBlock block = Variable.If(evidence); // Set up the GP prior, a distribution over functions, which will be filled in later Variable <SparseGP> prior = Variable.New <SparseGP>().Named("prior"); // The sparse GP variable - a random function Variable <IFunction> f = Variable <IFunction> .Random(prior).Named("f"); // The locations to evaluate the function VariableArray <Vector> x = Variable.Observed(inputs).Named("x"); Range j = x.Range.Named("j"); // The observation model VariableArray <bool> y = Variable.Observed(outputs, j).Named("y"); Variable <double> score = Variable.FunctionEvaluate(f, x[j]).Named("score"); y[j] = (Variable.GaussianFromMeanAndVariance(score, 0.1) > 0); // Close the evidence block block.CloseBlock(); // The basis Vector[] basis = new Vector[] { Vector.FromArray(new double[2] { 0.2, 0.2 }), Vector.FromArray(new double[2] { 0.2, 0.8 }), Vector.FromArray(new double[2] { 0.8, 0.2 }), Vector.FromArray(new double[2] { 0.8, 0.8 }) }; for (int trial = 0; trial < 3; trial++) { // The kernel IKernelFunction kf; if (trial == 0) { kf = new SquaredExponential(-0.0); } else if (trial == 1) { kf = new SquaredExponential(-0.5); } else { kf = new NNKernel(new double[] { 0.0, 0.0 }, -1.0); } // Fill in the sparse GP prior GaussianProcess gp = new GaussianProcess(new ConstantFunction(0), kf); prior.ObservedValue = new SparseGP(new SparseGPFixed(gp, basis)); // Model score double NNscore = engine.Infer <Bernoulli>(evidence).LogOdds; Console.WriteLine("{0} evidence = {1}", kf, NNscore.ToString("g4")); } // Infer the posterior Sparse GP SparseGP sgp = engine.Infer <SparseGP>(f); // Check that training set is classified correctly Console.WriteLine(""); Console.WriteLine("Predictions on training set:"); for (int i = 0; i < outputs.Length; i++) { Gaussian post = sgp.Marginal(inputs[i]); double postMean = post.GetMean(); string comment = (outputs[i] == (postMean > 0.0)) ? "correct" : "incorrect"; Console.WriteLine("f({0}) = {1} ({2})", inputs[i], post, comment); } }