Search in sources :

Example 26 with FakeGradientFunction

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

the class PoissonGradientProcedureTest method gradientProcedureIsFasterUnrolledThanGradientProcedure.

private void gradientProcedureIsFasterUnrolledThanGradientProcedure(RandomSeed seed, final int nparams, final boolean precomputed) {
    Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
    final int iter = 100;
    final ArrayList<double[]> paramsList = new ArrayList<>(iter);
    createFakeParams(RngUtils.create(seed.getSeed()), nparams, iter, paramsList);
    // Remove the timing of the function call by creating a dummy function
    final FakeGradientFunction f = new FakeGradientFunction(blockWidth, nparams);
    final Gradient1Function func = (precomputed) ? OffsetGradient1Function.wrapGradient1Function(f, SimpleArrayUtils.newArray(f.size(), 0.1, 1.3)) : f;
    final IntArrayFormatSupplier msg = new IntArrayFormatSupplier("M [%d]", 1);
    for (int i = 0; i < paramsList.size(); i++) {
        final PoissonGradientProcedure p1 = new PoissonGradientProcedure(func);
        p1.computeFisherInformation(paramsList.get(i));
        p1.computeFisherInformation(paramsList.get(i));
        final PoissonGradientProcedure p2 = PoissonGradientProcedureUtils.create(func);
        p2.computeFisherInformation(paramsList.get(i));
        p2.computeFisherInformation(paramsList.get(i));
        // Check they are the same
        Assertions.assertArrayEquals(p1.getLinear(), p2.getLinear(), msg.set(0, i));
    }
    // Realistic loops for an optimisation
    final int loops = 15;
    // Run till stable timing
    final Timer t1 = new Timer() {

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

        @Override
        void run() {
            for (int i = 0, k = 0; i < paramsList.size(); i++) {
                final PoissonGradientProcedure p2 = PoissonGradientProcedureUtils.create(func);
                for (int j = loops; j-- > 0; ) {
                    p2.computeFisherInformation(paramsList.get(k++ % iter));
                }
            }
        }
    };
    final long time2 = t2.getTime();
    logger.log(TestLogUtils.getTimingRecord("precomputed=" + precomputed + " Standard " + nparams, time1, "Unrolled", time2));
// Assertions.assertTrue(time2 < time1);
}
Also used : Gradient1Function(uk.ac.sussex.gdsc.smlm.function.Gradient1Function) OffsetGradient1Function(uk.ac.sussex.gdsc.smlm.function.OffsetGradient1Function) ArrayList(java.util.ArrayList) IntArrayFormatSupplier(uk.ac.sussex.gdsc.test.utils.functions.IntArrayFormatSupplier) FakeGradientFunction(uk.ac.sussex.gdsc.smlm.function.FakeGradientFunction)

Example 27 with FakeGradientFunction

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

the class FastMleGradient2ProcedureTest method gradientProcedureLinearIsFasterThanGradientProcedure.

private void gradientProcedureLinearIsFasterThanGradientProcedure(RandomSeed seed, final int nparams) {
    Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
    final int iter = 100;
    final ArrayList<double[]> paramsList = new ArrayList<>(iter);
    final ArrayList<double[]> yList = new ArrayList<>(iter);
    createData(RngUtils.create(seed.getSeed()), 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++) {
        final FastMleGradient2Procedure p1 = new FastMleGradient2Procedure(yList.get(i), func);
        p1.computeSecondDerivative(paramsList.get(i));
        p1.computeSecondDerivative(paramsList.get(i));
        final FastMleGradient2Procedure p2 = FastMleGradient2ProcedureUtils.createUnrolled(yList.get(i), func);
        p2.computeSecondDerivative(paramsList.get(i));
        p2.computeSecondDerivative(paramsList.get(i));
        // Check they are the same
        final int ii = i;
        Assertions.assertArrayEquals(p1.d1, p2.d1, () -> "D1 " + ii);
        Assertions.assertArrayEquals(p1.d2, p2.d2, () -> "D2 " + ii);
    }
    // Realistic loops for an optimisation
    final int loops = 15;
    // Run till stable timing
    final Timer t1 = new Timer() {

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

        @Override
        void run() {
            for (int i = 0, k = 0; i < paramsList.size(); i++) {
                final FastMleGradient2Procedure p2 = FastMleGradient2ProcedureUtils.createUnrolled(yList.get(i), func);
                for (int j = loops; j-- > 0; ) {
                    p2.computeSecondDerivative(paramsList.get(k++ % iter));
                }
            }
        }
    };
    final long time2 = t2.getTime();
    logger.log(TestLogUtils.getRecord(Level.INFO, "Standard = %d : Unrolled %d = %d : %fx", time1, nparams, time2, (1.0 * time1) / time2));
    Assertions.assertTrue(time2 < time1 * 1.5);
}
Also used : Gradient2Function(uk.ac.sussex.gdsc.smlm.function.Gradient2Function) OffsetGradient2Function(uk.ac.sussex.gdsc.smlm.function.OffsetGradient2Function) ArrayList(java.util.ArrayList) FakeGradientFunction(uk.ac.sussex.gdsc.smlm.function.FakeGradientFunction)

Example 28 with FakeGradientFunction

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

the class ParameterBoundsTest method canStepParameter.

private static void canStepParameter(double value) {
    final ParameterBounds bounds = new ParameterBounds(new FakeGradientFunction(1, 1, 1));
    double[] a1 = new double[1];
    double[] a2 = new double[1];
    double[] tmp;
    final double[] step = new double[] { value };
    final IndexSupplier msg = new IndexSupplier(1, "Step ", null);
    for (int i = 1; i <= 10; i++) {
        bounds.applyBounds(a1, step, a2);
        Assertions.assertArrayEquals(a2, new double[] { i * value }, msg.set(0, i));
        tmp = a1;
        a1 = a2;
        a2 = tmp;
    }
}
Also used : IndexSupplier(uk.ac.sussex.gdsc.test.utils.functions.IndexSupplier) FakeGradientFunction(uk.ac.sussex.gdsc.smlm.function.FakeGradientFunction)

Aggregations

FakeGradientFunction (uk.ac.sussex.gdsc.smlm.function.FakeGradientFunction)28 ArrayList (java.util.ArrayList)25 IntArrayFormatSupplier (uk.ac.sussex.gdsc.test.utils.functions.IntArrayFormatSupplier)11 Gradient1Function (uk.ac.sussex.gdsc.smlm.function.Gradient1Function)8 IndexSupplier (uk.ac.sussex.gdsc.test.utils.functions.IndexSupplier)8 OffsetGradient1Function (uk.ac.sussex.gdsc.smlm.function.OffsetGradient1Function)6 FastLog (uk.ac.sussex.gdsc.smlm.function.FastLog)4 DenseMatrix64F (org.ejml.data.DenseMatrix64F)3 UniformRandomProvider (org.apache.commons.rng.UniformRandomProvider)2 DoubleDoubleBiPredicate (uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate)2 TimingResult (uk.ac.sussex.gdsc.test.utils.TimingResult)2 LogRecord (java.util.logging.LogRecord)1 Test (org.junit.jupiter.api.Test)1 Gradient2Function (uk.ac.sussex.gdsc.smlm.function.Gradient2Function)1 OffsetGradient2Function (uk.ac.sussex.gdsc.smlm.function.OffsetGradient2Function)1