Search in sources :

Example 1 with FakeGradientFunction

use of gdsc.smlm.function.FakeGradientFunction in project GDSC-SMLM by aherbert.

the class PoissonGradientProcedureTest method gradientProcedureIsFasterUnrolledThanGradientProcedure.

private void gradientProcedureIsFasterUnrolledThanGradientProcedure(final int nparams, final boolean precomputed) {
    org.junit.Assume.assumeTrue(speedTests || TestSettings.RUN_SPEED_TESTS);
    final int iter = 100;
    rdg = new RandomDataGenerator(new Well19937c(30051977));
    final ArrayList<double[]> paramsList = new ArrayList<double[]>(iter);
    createFakeParams(nparams, iter, paramsList);
    // Remove the timing of the function call by creating a dummy function
    FakeGradientFunction f = new FakeGradientFunction(blockWidth, nparams);
    final Gradient1Function func = (precomputed) ? PrecomputedGradient1Function.wrapGradient1Function(f, Utils.newArray(f.size(), 0.1, 1.3)) : f;
    for (int i = 0; i < paramsList.size(); i++) {
        PoissonGradientProcedure p1 = new PoissonGradientProcedure(func);
        p1.computeFisherInformation(paramsList.get(i));
        p1.computeFisherInformation(paramsList.get(i));
        PoissonGradientProcedure p2 = PoissonGradientProcedureFactory.create(func);
        p2.computeFisherInformation(paramsList.get(i));
        p2.computeFisherInformation(paramsList.get(i));
        // Check they are the same
        Assert.assertArrayEquals("M " + i, p1.getLinear(), p2.getLinear(), 0);
    }
    // Realistic loops for an optimisation
    final int loops = 15;
    // Run till stable timing
    Timer t1 = new Timer() {

        @Override
        void run() {
            for (int i = 0, k = 0; i < paramsList.size(); i++) {
                PoissonGradientProcedure p1 = new PoissonGradientProcedure(func);
                for (int j = loops; j-- > 0; ) p1.computeFisherInformation(paramsList.get(k++ % iter));
            }
        }
    };
    long time1 = t1.getTime();
    Timer t2 = new Timer(t1.loops) {

        @Override
        void run() {
            for (int i = 0, k = 0; i < paramsList.size(); i++) {
                PoissonGradientProcedure p2 = PoissonGradientProcedureFactory.create(func);
                for (int j = loops; j-- > 0; ) p2.computeFisherInformation(paramsList.get(k++ % iter));
            }
        }
    };
    long time2 = t2.getTime();
    log("Precomputed=%b : Standard %d : Unrolled %d = %d : %fx\n", precomputed, time1, nparams, time2, (1.0 * time1) / time2);
    Assert.assertTrue(time2 < time1);
}
Also used : Gradient1Function(gdsc.smlm.function.Gradient1Function) PrecomputedGradient1Function(gdsc.smlm.function.PrecomputedGradient1Function) RandomDataGenerator(org.apache.commons.math3.random.RandomDataGenerator) ArrayList(java.util.ArrayList) Well19937c(org.apache.commons.math3.random.Well19937c) FakeGradientFunction(gdsc.smlm.function.FakeGradientFunction)

Example 2 with FakeGradientFunction

use of gdsc.smlm.function.FakeGradientFunction in project GDSC-SMLM by aherbert.

the class FastMLEGradient2ProcedureTest method gradientProcedureComputesSameLogLikelihoodAsMLEGradientCalculator.

private void gradientProcedureComputesSameLogLikelihoodAsMLEGradientCalculator(int nparams) {
    int iter = 10;
    rdg = new RandomDataGenerator(new Well19937c(30051977));
    ArrayList<double[]> paramsList = new ArrayList<double[]>(iter);
    ArrayList<double[]> yList = new ArrayList<double[]>(iter);
    createFakeData(nparams, iter, paramsList, yList);
    FakeGradientFunction func = new FakeGradientFunction(blockWidth, nparams);
    MLEGradientCalculator calc = (MLEGradientCalculator) GradientCalculatorFactory.newCalculator(nparams, true);
    String name = String.format("[%d]", nparams);
    for (int i = 0; i < paramsList.size(); i++) {
        FastMLEGradient2Procedure p = FastMLEGradient2ProcedureFactory.createUnrolled(yList.get(i), func);
        double s = p.computeLogLikelihood(paramsList.get(i));
        double s2 = calc.logLikelihood(yList.get(i), paramsList.get(i), func);
        // Virtually the same ...
        Assert.assertEquals(name + " Result: Not same @ " + i, s, s2, Math.abs(s) * 1e-5);
    }
}
Also used : RandomDataGenerator(org.apache.commons.math3.random.RandomDataGenerator) ArrayList(java.util.ArrayList) Well19937c(org.apache.commons.math3.random.Well19937c) FakeGradientFunction(gdsc.smlm.function.FakeGradientFunction)

Example 3 with FakeGradientFunction

use of gdsc.smlm.function.FakeGradientFunction in project GDSC-SMLM by aherbert.

the class FastMLEGradient2ProcedureTest method gradientProcedureUnrolledComputesSameAsGradientProcedure.

private void gradientProcedureUnrolledComputesSameAsGradientProcedure(int nparams) {
    int iter = 10;
    rdg = new RandomDataGenerator(new Well19937c(30051977));
    ArrayList<double[]> paramsList = new ArrayList<double[]>(iter);
    ArrayList<double[]> yList = new ArrayList<double[]>(iter);
    createFakeData(nparams, iter, paramsList, yList);
    FakeGradientFunction func = new FakeGradientFunction(blockWidth, nparams);
    FastMLEGradient2Procedure p1, p2;
    String name = String.format("[%d]", nparams);
    for (int i = 0; i < paramsList.size(); i++) {
        p1 = new FastMLEGradient2Procedure(yList.get(i), func);
        p2 = FastMLEGradient2ProcedureFactory.createUnrolled(yList.get(i), func);
        double[] a = paramsList.get(i);
        double ll1 = p1.computeLogLikelihood(a);
        double ll2 = p2.computeLogLikelihood(a);
        Assert.assertEquals(name + " LL: Not same @ " + i, ll1, ll2, 0);
        p1 = new FastMLEGradient2Procedure(yList.get(i), func);
        p2 = FastMLEGradient2ProcedureFactory.createUnrolled(yList.get(i), func);
        p1.computeFirstDerivative(a);
        p2.computeFirstDerivative(a);
        Assert.assertArrayEquals(name + " first derivative value: Not same @ " + i, p1.u, p2.u, 0);
        Assert.assertArrayEquals(name + " first derivative: Not same @ " + i, p1.d1, p2.d1, 0);
        p1 = new FastMLEGradient2Procedure(yList.get(i), func);
        p2 = FastMLEGradient2ProcedureFactory.createUnrolled(yList.get(i), func);
        p1.computeSecondDerivative(a);
        p2.computeSecondDerivative(a);
        Assert.assertArrayEquals(name + " update value: Not same @ " + i, p1.u, p2.u, 0);
        Assert.assertArrayEquals(name + " update: Not same d1 @ " + i, p1.d1, p2.d1, 0);
        Assert.assertArrayEquals(name + " update: Not same d2 @ " + i, p1.d2, p2.d2, 0);
    }
}
Also used : RandomDataGenerator(org.apache.commons.math3.random.RandomDataGenerator) ArrayList(java.util.ArrayList) Well19937c(org.apache.commons.math3.random.Well19937c) FakeGradientFunction(gdsc.smlm.function.FakeGradientFunction)

Example 4 with FakeGradientFunction

use of gdsc.smlm.function.FakeGradientFunction in project GDSC-SMLM by aherbert.

the class LSQLVMGradientProcedureTest method gradientProcedureComputesSameAsGradientCalculator.

private void gradientProcedureComputesSameAsGradientCalculator(int nparams, BaseLSQLVMGradientProcedureFactory factory) {
    int iter = 10;
    rdg = new RandomDataGenerator(new Well19937c(30051977));
    double[][] alpha = new double[nparams][nparams];
    double[] beta = new double[nparams];
    ArrayList<double[]> paramsList = new ArrayList<double[]>(iter);
    ArrayList<double[]> yList = new ArrayList<double[]>(iter);
    int[] x = createFakeData(nparams, iter, paramsList, yList);
    int n = x.length;
    FakeGradientFunction func = new FakeGradientFunction(blockWidth, nparams);
    GradientCalculator calc = GradientCalculatorFactory.newCalculator(nparams, false);
    String name = factory.getClass().getSimpleName();
    for (int i = 0; i < paramsList.size(); i++) {
        BaseLSQLVMGradientProcedure p = factory.createProcedure(yList.get(i), func);
        p.gradient(paramsList.get(i));
        double s = p.value;
        double s2 = calc.findLinearised(n, yList.get(i), paramsList.get(i), alpha, beta, func);
        // Exactly the same ...
        Assert.assertEquals(name + " Result: Not same @ " + i, s, s2, 0);
        Assert.assertArrayEquals(name + " Observations: Not same beta @ " + i, p.beta, beta, 0);
        double[] al = p.getAlphaLinear();
        Assert.assertArrayEquals(name + " Observations: Not same alpha @ " + i, al, new DenseMatrix64F(alpha).data, 0);
        double[][] am = p.getAlphaMatrix();
        for (int j = 0; j < nparams; j++) Assert.assertArrayEquals(name + " Observations: Not same alpha @ " + i, am[j], alpha[j], 0);
    }
}
Also used : RandomDataGenerator(org.apache.commons.math3.random.RandomDataGenerator) ArrayList(java.util.ArrayList) Well19937c(org.apache.commons.math3.random.Well19937c) DenseMatrix64F(org.ejml.data.DenseMatrix64F) FakeGradientFunction(gdsc.smlm.function.FakeGradientFunction)

Example 5 with FakeGradientFunction

use of gdsc.smlm.function.FakeGradientFunction in project GDSC-SMLM by aherbert.

the class LVMGradientProcedureTest method gradientProcedureComputesSameAsGradientCalculator.

private void gradientProcedureComputesSameAsGradientCalculator(int nparams, boolean mle) {
    int iter = 10;
    rdg = new RandomDataGenerator(new Well19937c(30051977));
    double[][] alpha = new double[nparams][nparams];
    double[] beta = new double[nparams];
    ArrayList<double[]> paramsList = new ArrayList<double[]>(iter);
    ArrayList<double[]> yList = new ArrayList<double[]>(iter);
    int[] x = createFakeData(nparams, iter, paramsList, yList);
    int n = x.length;
    FakeGradientFunction func = new FakeGradientFunction(blockWidth, nparams);
    GradientCalculator calc = GradientCalculatorFactory.newCalculator(nparams, mle);
    String name = String.format("[%d] %b", nparams, mle);
    for (int i = 0; i < paramsList.size(); i++) {
        LVMGradientProcedure p = LVMGradientProcedureFactory.create(yList.get(i), func, (mle) ? Type.MLE : Type.LSQ);
        p.gradient(paramsList.get(i));
        double s = p.value;
        double s2 = calc.findLinearised(n, yList.get(i), paramsList.get(i), alpha, beta, func);
        // Exactly the same ...
        Assert.assertEquals(name + " Result: Not same @ " + i, s, s2, 0);
        Assert.assertArrayEquals(name + " Observations: Not same beta @ " + i, p.beta, beta, 0);
        double[] al = p.getAlphaLinear();
        Assert.assertArrayEquals(name + " Observations: Not same alpha @ " + i, al, new DenseMatrix64F(alpha).data, 0);
        double[][] am = p.getAlphaMatrix();
        for (int j = 0; j < nparams; j++) Assert.assertArrayEquals(name + " Observations: Not same alpha @ " + i, am[j], alpha[j], 0);
    }
}
Also used : RandomDataGenerator(org.apache.commons.math3.random.RandomDataGenerator) ArrayList(java.util.ArrayList) Well19937c(org.apache.commons.math3.random.Well19937c) DenseMatrix64F(org.ejml.data.DenseMatrix64F) FakeGradientFunction(gdsc.smlm.function.FakeGradientFunction)

Aggregations

FakeGradientFunction (gdsc.smlm.function.FakeGradientFunction)17 ArrayList (java.util.ArrayList)17 RandomDataGenerator (org.apache.commons.math3.random.RandomDataGenerator)17 Well19937c (org.apache.commons.math3.random.Well19937c)17 Gradient1Function (gdsc.smlm.function.Gradient1Function)5 PrecomputedGradient1Function (gdsc.smlm.function.PrecomputedGradient1Function)4 DenseMatrix64F (org.ejml.data.DenseMatrix64F)3 Type (gdsc.smlm.fitting.nonlinear.gradient.LVMGradientProcedureFactory.Type)1 Gradient2Function (gdsc.smlm.function.Gradient2Function)1 PrecomputedGradient2Function (gdsc.smlm.function.PrecomputedGradient2Function)1