Search in sources :

Example 6 with FakeGradientFunction

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

the class FastMLEJacobianGradient2ProcedureTest method gradientProcedureComputesSameAsBaseGradientProcedure.

private void gradientProcedureComputesSameAsBaseGradientProcedure(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);
    for (int i = 0; i < paramsList.size(); i++) {
        FastMLEGradient2Procedure p = FastMLEGradient2ProcedureFactory.createUnrolled(yList.get(i), func);
        FastMLEJacobianGradient2Procedure p2 = new FastMLEJacobianGradient2Procedure(yList.get(i), func);
        p.computeSecondDerivative(paramsList.get(i));
        p2.computeSecondDerivative(paramsList.get(i));
        // Virtually the same ...
        Assert.assertArrayEquals(p.d1, p2.d1, 1e-5);
        Assert.assertArrayEquals(p.d2, p2.d2, 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 7 with FakeGradientFunction

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

the class FastMLEGradient2ProcedureTest method gradientProcedureLinearIsFasterThanGradientProcedure.

private void gradientProcedureLinearIsFasterThanGradientProcedure(final int nparams) {
    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);
    final ArrayList<double[]> yList = new ArrayList<double[]>(iter);
    createData(1, iter, paramsList, yList);
    // Remove the timing of the function call by creating a dummy function
    final Gradient2Function func = new FakeGradientFunction(blockWidth, nparams);
    for (int i = 0; i < paramsList.size(); i++) {
        FastMLEGradient2Procedure p1 = new FastMLEGradient2Procedure(yList.get(i), func);
        p1.computeSecondDerivative(paramsList.get(i));
        p1.computeSecondDerivative(paramsList.get(i));
        FastMLEGradient2Procedure p2 = FastMLEGradient2ProcedureFactory.createUnrolled(yList.get(i), func);
        p2.computeSecondDerivative(paramsList.get(i));
        p2.computeSecondDerivative(paramsList.get(i));
        // Check they are the same
        Assert.assertArrayEquals("D1 " + i, p1.d1, p2.d1, 0);
        Assert.assertArrayEquals("D2 " + i, p1.d2, p2.d2, 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++) {
                FastMLEGradient2Procedure p1 = new FastMLEGradient2Procedure(yList.get(i), func);
                for (int j = loops; j-- > 0; ) p1.computeSecondDerivative(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++) {
                FastMLEGradient2Procedure p2 = FastMLEGradient2ProcedureFactory.createUnrolled(yList.get(i), func);
                for (int j = loops; j-- > 0; ) p2.computeSecondDerivative(paramsList.get(k++ % iter));
            }
        }
    };
    long time2 = t2.getTime();
    log("Standard = %d : Unrolled %d = %d : %fx\n", time1, nparams, time2, (1.0 * time1) / time2);
    Assert.assertTrue(time2 < time1 * 1.5);
}
Also used : RandomDataGenerator(org.apache.commons.math3.random.RandomDataGenerator) Gradient2Function(gdsc.smlm.function.Gradient2Function) PrecomputedGradient2Function(gdsc.smlm.function.PrecomputedGradient2Function) ArrayList(java.util.ArrayList) Well19937c(org.apache.commons.math3.random.Well19937c) FakeGradientFunction(gdsc.smlm.function.FakeGradientFunction)

Example 8 with FakeGradientFunction

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

the class LSQLVMGradientProcedureTest method gradientProcedureIsNotSlowerThanGradientCalculator.

private void gradientProcedureIsNotSlowerThanGradientCalculator(final int nparams, final BaseLSQLVMGradientProcedureFactory factory) {
    org.junit.Assume.assumeTrue(speedTests || TestSettings.RUN_SPEED_TESTS);
    final int iter = 1000;
    rdg = new RandomDataGenerator(new Well19937c(30051977));
    final double[][] alpha = new double[nparams][nparams];
    final double[] beta = new double[nparams];
    final ArrayList<double[]> paramsList = new ArrayList<double[]>(iter);
    final ArrayList<double[]> yList = new ArrayList<double[]>(iter);
    int[] x = createFakeData(nparams, iter, paramsList, yList);
    final int n = x.length;
    final FakeGradientFunction func = new FakeGradientFunction(blockWidth, nparams);
    GradientCalculator calc = GradientCalculatorFactory.newCalculator(nparams, false);
    for (int i = 0; i < paramsList.size(); i++) calc.findLinearised(n, yList.get(i), paramsList.get(i), alpha, beta, func);
    for (int i = 0; i < paramsList.size(); i++) {
        BaseLSQLVMGradientProcedure p = factory.createProcedure(yList.get(i), func);
        p.gradient(paramsList.get(i));
    }
    // 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 < iter; i++) {
                GradientCalculator calc = GradientCalculatorFactory.newCalculator(nparams, false);
                for (int j = loops; j-- > 0; ) calc.findLinearised(n, yList.get(i), paramsList.get(k++ % iter), alpha, beta, func);
            }
        }
    };
    long time1 = t1.getTime();
    Timer t2 = new Timer(t1.loops) {

        @Override
        void run() {
            for (int i = 0, k = 0; i < iter; i++) {
                BaseLSQLVMGradientProcedure p = factory.createProcedure(yList.get(i), func);
                for (int j = loops; j-- > 0; ) p.gradient(paramsList.get(k++ % iter));
            }
        }
    };
    long time2 = t2.getTime();
    log("GradientCalculator = %d : %s %d = %d : %fx\n", time1, factory.getClass().getSimpleName(), nparams, time2, (1.0 * time1) / time2);
    if (TestSettings.ASSERT_SPEED_TESTS) {
        // Add contingency
        Assert.assertTrue(time2 < time1 * 1.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 9 with FakeGradientFunction

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

the class LVMGradientProcedureTest method gradientProcedureLinearIsFasterThanGradientProcedure.

private void gradientProcedureLinearIsFasterThanGradientProcedure(final int nparams, final Type type, 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);
    final ArrayList<double[]> yList = new ArrayList<double[]>(iter);
    createData(1, iter, paramsList, yList);
    // Remove the timing of the function call by creating a dummy function
    FakeGradientFunction fgf = new FakeGradientFunction(blockWidth, nparams);
    final Gradient1Function func;
    if (precomputed) {
        final double[] b = Utils.newArray(fgf.size(), 0.1, 1.3);
        func = PrecomputedGradient1Function.wrapGradient1Function(fgf, b);
    } else {
        func = fgf;
    }
    for (int i = 0; i < paramsList.size(); i++) {
        LVMGradientProcedure p1 = createProcedure(type, yList.get(i), func);
        p1.gradient(paramsList.get(i));
        p1.gradient(paramsList.get(i));
        LVMGradientProcedure p2 = LVMGradientProcedureFactory.create(yList.get(i), func, type);
        p2.gradient(paramsList.get(i));
        p2.gradient(paramsList.get(i));
        // Check they are the same
        Assert.assertArrayEquals("A " + i, p1.getAlphaLinear(), p2.getAlphaLinear(), 0);
        Assert.assertArrayEquals("B " + i, p1.beta, p2.beta, 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++) {
                LVMGradientProcedure p1 = createProcedure(type, yList.get(i), func);
                for (int j = loops; j-- > 0; ) p1.gradient(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++) {
                LVMGradientProcedure p2 = LVMGradientProcedureFactory.create(yList.get(i), func, type);
                for (int j = loops; j-- > 0; ) p2.gradient(paramsList.get(k++ % iter));
            }
        }
    };
    long time2 = t2.getTime();
    log("%s, Precomputed=%b : Standard = %d : Unrolled %d = %d : %fx\n", type, 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 10 with FakeGradientFunction

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

the class LVMGradientProcedureTest method gradientProcedureIsNotSlowerThanGradientCalculator.

private void gradientProcedureIsNotSlowerThanGradientCalculator(final int nparams, final boolean mle) {
    org.junit.Assume.assumeTrue(speedTests || TestSettings.RUN_SPEED_TESTS);
    final int iter = 1000;
    rdg = new RandomDataGenerator(new Well19937c(30051977));
    final double[][] alpha = new double[nparams][nparams];
    final double[] beta = new double[nparams];
    final ArrayList<double[]> paramsList = new ArrayList<double[]>(iter);
    final ArrayList<double[]> yList = new ArrayList<double[]>(iter);
    int[] x = createFakeData(nparams, iter, paramsList, yList);
    final int n = x.length;
    final FakeGradientFunction func = new FakeGradientFunction(blockWidth, nparams);
    GradientCalculator calc = GradientCalculatorFactory.newCalculator(nparams, mle);
    for (int i = 0; i < paramsList.size(); i++) calc.findLinearised(n, yList.get(i), paramsList.get(i), alpha, beta, func);
    final Type type = (mle) ? Type.MLE : Type.LSQ;
    for (int i = 0; i < paramsList.size(); i++) {
        LVMGradientProcedure p = LVMGradientProcedureFactory.create(yList.get(i), func, type);
        p.gradient(paramsList.get(i));
    }
    // 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 < iter; i++) {
                GradientCalculator calc = GradientCalculatorFactory.newCalculator(nparams, mle);
                for (int j = loops; j-- > 0; ) calc.findLinearised(n, yList.get(i), paramsList.get(k++ % iter), alpha, beta, func);
            }
        }
    };
    long time1 = t1.getTime();
    Timer t2 = new Timer(t1.loops) {

        @Override
        void run() {
            for (int i = 0, k = 0; i < iter; i++) {
                LVMGradientProcedure p = LVMGradientProcedureFactory.create(yList.get(i), func, type);
                for (int j = loops; j-- > 0; ) p.gradient(paramsList.get(k++ % iter));
            }
        }
    };
    long time2 = t2.getTime();
    log("GradientCalculator = %d : LVMGradientProcedure %d %b = %d : %fx\n", time1, nparams, mle, time2, (1.0 * time1) / time2);
    if (TestSettings.ASSERT_SPEED_TESTS) {
        // Add contingency
        Assert.assertTrue(time2 < time1 * 1.5);
    }
}
Also used : RandomDataGenerator(org.apache.commons.math3.random.RandomDataGenerator) ArrayList(java.util.ArrayList) Well19937c(org.apache.commons.math3.random.Well19937c) Type(gdsc.smlm.fitting.nonlinear.gradient.LVMGradientProcedureFactory.Type) 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