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);
}
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);
}
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;
}
}
Aggregations