Search in sources :

Example 16 with FakeGradientFunction

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

the class FastMleGradient2ProcedureTest method gradientProcedureComputesSameLogLikelihoodAsMleGradientCalculator.

private void gradientProcedureComputesSameLogLikelihoodAsMleGradientCalculator(RandomSeed seed, int nparams, DoubleDoubleBiPredicate equality) {
    final int iter = 10;
    final ArrayList<double[]> paramsList = new ArrayList<>(iter);
    final ArrayList<double[]> yList = new ArrayList<>(iter);
    createFakeData(RngUtils.create(seed.getSeed()), nparams, iter, paramsList, yList);
    final FakeGradientFunction func = new FakeGradientFunction(blockWidth, nparams);
    final MleGradientCalculator calc = (MleGradientCalculator) GradientCalculatorUtils.newCalculator(nparams, true);
    final IndexSupplier msg = new IndexSupplier(1, "[" + nparams + "] Result: not same @ ", null);
    for (int i = 0; i < paramsList.size(); i++) {
        final FastMleGradient2Procedure p = FastMleGradient2ProcedureUtils.createUnrolled(yList.get(i), func);
        final double s = p.computeLogLikelihood(paramsList.get(i));
        final double s2 = calc.logLikelihood(yList.get(i), paramsList.get(i), func);
        // Virtually the same ...
        TestAssertions.assertTest(s, s2, equality, msg.set(0, i));
    }
}
Also used : IndexSupplier(uk.ac.sussex.gdsc.test.utils.functions.IndexSupplier) ArrayList(java.util.ArrayList) FakeGradientFunction(uk.ac.sussex.gdsc.smlm.function.FakeGradientFunction)

Example 17 with FakeGradientFunction

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

the class FastMleJacobianGradient2ProcedureTest method gradientProcedureComputesSameAsBaseGradientProcedure.

private void gradientProcedureComputesSameAsBaseGradientProcedure(RandomSeed seed, int nparams, DoubleDoubleBiPredicate equality) {
    final int iter = 10;
    final ArrayList<double[]> paramsList = new ArrayList<>(iter);
    final ArrayList<double[]> yList = new ArrayList<>(iter);
    createFakeData(RngUtils.create(seed.getSeed()), nparams, iter, paramsList, yList);
    final FakeGradientFunction func = new FakeGradientFunction(blockWidth, nparams);
    for (int i = 0; i < paramsList.size(); i++) {
        final FastMleGradient2Procedure p = FastMleGradient2ProcedureUtils.createUnrolled(yList.get(i), func);
        final FastMleJacobianGradient2Procedure p2 = new FastMleJacobianGradient2Procedure(yList.get(i), func);
        p.computeSecondDerivative(paramsList.get(i));
        p2.computeSecondDerivative(paramsList.get(i));
        // Virtually the same ...
        TestAssertions.assertArrayTest(p.d1, p2.d1, equality);
        TestAssertions.assertArrayTest(p.d2, p2.d2, equality);
    }
}
Also used : ArrayList(java.util.ArrayList) FakeGradientFunction(uk.ac.sussex.gdsc.smlm.function.FakeGradientFunction)

Example 18 with FakeGradientFunction

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

the class LsqLvmGradientProcedureTest method gradientProcedureComputesSameAsGradientCalculator.

private void gradientProcedureComputesSameAsGradientCalculator(RandomSeed seed, int nparams, BaseLsqLvmGradientProcedureFactory factory) {
    final int iter = 10;
    final double[][] alpha = new double[nparams][nparams];
    final double[] beta = new double[nparams];
    final ArrayList<double[]> paramsList = new ArrayList<>(iter);
    final ArrayList<double[]> yList = new ArrayList<>(iter);
    final int[] x = createFakeData(RngUtils.create(seed.getSeed()), nparams, iter, paramsList, yList);
    final int n = x.length;
    final FakeGradientFunction func = new FakeGradientFunction(blockWidth, nparams);
    final GradientCalculator calc = GradientCalculatorUtils.newCalculator(nparams, false);
    final String name = factory.getClass().getSimpleName();
    // Create messages
    final IndexSupplier msgR = new IndexSupplier(1, name + "Result: Not same ", null);
    final IndexSupplier msgOb = new IndexSupplier(1, name + "Observations: Not same beta ", null);
    final IndexSupplier msgOal = new IndexSupplier(1, name + "Observations: Not same alpha linear ", null);
    final IndexSupplier msgOam = new IndexSupplier(1, name + "Observations: Not same alpha matrix ", null);
    for (int i = 0; i < paramsList.size(); i++) {
        final BaseLsqLvmGradientProcedure p = factory.createProcedure(yList.get(i), func);
        p.gradient(paramsList.get(i));
        final double s = p.value;
        final double s2 = calc.findLinearised(n, yList.get(i), paramsList.get(i), alpha, beta, func);
        // Exactly the same ...
        Assertions.assertEquals(s, s2, msgR.set(0, i));
        Assertions.assertArrayEquals(p.beta, beta, msgOb.set(0, i));
        final double[] al = p.getAlphaLinear();
        Assertions.assertArrayEquals(al, new DenseMatrix64F(alpha).data, msgOal.set(0, i));
        final double[][] am = p.getAlphaMatrix();
        Assertions.assertArrayEquals(am, alpha, msgOam.set(0, i));
    }
}
Also used : ArrayList(java.util.ArrayList) DenseMatrix64F(org.ejml.data.DenseMatrix64F) IndexSupplier(uk.ac.sussex.gdsc.test.utils.functions.IndexSupplier) FakeGradientFunction(uk.ac.sussex.gdsc.smlm.function.FakeGradientFunction)

Example 19 with FakeGradientFunction

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

the class LsqLvmGradientProcedureTest method gradientProcedureLinearIsFasterThanGradientProcedureMatrix.

private void gradientProcedureLinearIsFasterThanGradientProcedureMatrix(RandomSeed seed, final int nparams, final BaseLsqLvmGradientProcedureFactory factory1, final BaseLsqLvmGradientProcedureFactory factory2, boolean doAssert) {
    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 Gradient1Function func = new FakeGradientFunction(blockWidth, nparams);
    // Create messages
    final IndexSupplier msgA = new IndexSupplier(1, "A ", null);
    final IndexSupplier msgB = new IndexSupplier(1, "B ", null);
    for (int i = 0; i < paramsList.size(); i++) {
        final BaseLsqLvmGradientProcedure p1 = factory1.createProcedure(yList.get(i), func);
        p1.gradient(paramsList.get(i));
        p1.gradient(paramsList.get(i));
        final BaseLsqLvmGradientProcedure p2 = factory2.createProcedure(yList.get(i), func);
        p2.gradient(paramsList.get(i));
        p2.gradient(paramsList.get(i));
        // Check they are the same
        Assertions.assertArrayEquals(p1.getAlphaLinear(), p2.getAlphaLinear(), msgA.set(0, i));
        Assertions.assertArrayEquals(p1.beta, p2.beta, msgB.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 BaseLsqLvmGradientProcedure p = factory1.createProcedure(yList.get(i), func);
                for (int j = loops; j-- > 0; ) {
                    p.gradient(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 BaseLsqLvmGradientProcedure p2 = factory2.createProcedure(yList.get(i), func);
                for (int j = loops; j-- > 0; ) {
                    p2.gradient(paramsList.get(k++ % iter));
                }
            }
        }
    };
    final long time2 = t2.getTime();
    final LogRecord record = TestLogUtils.getTimingRecord(factory1.getClass().getSimpleName() + nparams, time1, factory2.getClass().getSimpleName(), time2);
    if (!doAssert && record.getLevel() == TestLevel.TEST_FAILURE) {
        record.setLevel(TestLevel.TEST_WARNING);
    }
    logger.log(record);
}
Also used : Gradient1Function(uk.ac.sussex.gdsc.smlm.function.Gradient1Function) IndexSupplier(uk.ac.sussex.gdsc.test.utils.functions.IndexSupplier) LogRecord(java.util.logging.LogRecord) ArrayList(java.util.ArrayList) FakeGradientFunction(uk.ac.sussex.gdsc.smlm.function.FakeGradientFunction)

Example 20 with FakeGradientFunction

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

the class LvmGradientProcedureTest method gradientProcedureIsFasterUnrolledThanGradientProcedure.

private void gradientProcedureIsFasterUnrolledThanGradientProcedure(RandomSeed seed, final int nparams, final Type type, final boolean precomputed) {
    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 FakeGradientFunction fgf = new FakeGradientFunction(blockWidth, nparams);
    final Gradient1Function func;
    if (precomputed) {
        final double[] b = SimpleArrayUtils.newArray(fgf.size(), 0.1, 1.3);
        func = OffsetGradient1Function.wrapGradient1Function(fgf, b);
    } else {
        func = fgf;
    }
    final FastLog fastLog = type == Type.FAST_LOG_MLE ? getFastLog() : null;
    final IntArrayFormatSupplier msgA = new IntArrayFormatSupplier("A [%d]", 1);
    final IntArrayFormatSupplier msgB = new IntArrayFormatSupplier("B [%d]", 1);
    for (int i = 0; i < paramsList.size(); i++) {
        final LvmGradientProcedure p1 = createProcedure(type, yList.get(i), func, fastLog);
        p1.gradient(paramsList.get(i));
        p1.gradient(paramsList.get(i));
        final LvmGradientProcedure p2 = LvmGradientProcedureUtils.create(yList.get(i), func, type, fastLog);
        p2.gradient(paramsList.get(i));
        p2.gradient(paramsList.get(i));
        // Check they are the same
        Assertions.assertArrayEquals(p1.getAlphaLinear(), p2.getAlphaLinear(), msgA.set(0, i));
        Assertions.assertArrayEquals(p1.beta, p2.beta, msgB.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 LvmGradientProcedure p1 = createProcedure(type, yList.get(i), func, fastLog);
                for (int j = loops; j-- > 0; ) {
                    p1.gradient(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 LvmGradientProcedure p2 = LvmGradientProcedureUtils.create(yList.get(i), func, type, fastLog);
                for (int j = loops; j-- > 0; ) {
                    p2.gradient(paramsList.get(k++ % iter));
                }
            }
        }
    };
    final long time2 = t2.getTime();
    logger.log(TestLogUtils.getTimingRecord(new TimingResult(() -> String.format("%s, Precomputed=%b : Standard", type, precomputed), time1), new TimingResult(() -> String.format("Unrolled %d", nparams), time2)));
}
Also used : Gradient1Function(uk.ac.sussex.gdsc.smlm.function.Gradient1Function) OffsetGradient1Function(uk.ac.sussex.gdsc.smlm.function.OffsetGradient1Function) TimingResult(uk.ac.sussex.gdsc.test.utils.TimingResult) ArrayList(java.util.ArrayList) IntArrayFormatSupplier(uk.ac.sussex.gdsc.test.utils.functions.IntArrayFormatSupplier) FakeGradientFunction(uk.ac.sussex.gdsc.smlm.function.FakeGradientFunction) FastLog(uk.ac.sussex.gdsc.smlm.function.FastLog)

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