use of gdsc.smlm.function.FakeGradientFunction in project GDSC-SMLM by aherbert.
the class LVMGradientProcedureTest method gradientProcedureUnrolledComputesSameAsGradientProcedure.
private void gradientProcedureUnrolledComputesSameAsGradientProcedure(int nparams, Type type, boolean precomputed) {
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);
Gradient1Function func = new FakeGradientFunction(blockWidth, nparams);
if (precomputed) {
double[] b = Utils.newArray(func.size(), 0.1, 1.3);
func = PrecomputedGradient1Function.wrapGradient1Function(func, b);
}
String name = String.format("[%d] %b", nparams, type);
for (int i = 0; i < paramsList.size(); i++) {
LVMGradientProcedure p1 = createProcedure(type, yList.get(i), func);
p1.gradient(paramsList.get(i));
LVMGradientProcedure p2 = LVMGradientProcedureFactory.create(yList.get(i), func, type);
p2.gradient(paramsList.get(i));
// Exactly the same ...
Assert.assertEquals(name + " Result: Not same @ " + i, p1.value, p2.value, 0);
Assert.assertArrayEquals(name + " Observations: Not same beta @ " + i, p1.beta, p2.beta, 0);
Assert.assertArrayEquals(name + " Observations: Not same alpha @ " + i, p1.getAlphaLinear(), p2.getAlphaLinear(), 0);
double[][] am1 = p1.getAlphaMatrix();
double[][] am2 = p2.getAlphaMatrix();
for (int j = 0; j < nparams; j++) Assert.assertArrayEquals(name + " Observations: Not same alpha @ " + i, am1[j], am2[j], 0);
}
}
use of gdsc.smlm.function.FakeGradientFunction in project GDSC-SMLM by aherbert.
the class PoissonGradientProcedureTest method gradientProcedureIsNotSlowerThanGradientCalculator.
private void gradientProcedureIsNotSlowerThanGradientCalculator(final int nparams) {
org.junit.Assume.assumeTrue(speedTests || TestSettings.RUN_SPEED_TESTS);
final int iter = 1000;
rdg = new RandomDataGenerator(new Well19937c(30051977));
final ArrayList<double[]> paramsList = new ArrayList<double[]>(iter);
createFakeParams(nparams, iter, paramsList);
final int n = blockWidth * blockWidth;
final FakeGradientFunction func = new FakeGradientFunction(blockWidth, nparams);
GradientCalculator calc = GradientCalculatorFactory.newCalculator(nparams, false);
for (int i = 0; i < paramsList.size(); i++) calc.fisherInformationMatrix(n, paramsList.get(i), func);
for (int i = 0; i < paramsList.size(); i++) {
PoissonGradientProcedure p = PoissonGradientProcedureFactory.create(func);
p.computeFisherInformation(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.fisherInformationMatrix(n, paramsList.get(k++ % iter), func);
}
}
};
long time1 = t1.getTime();
Timer t2 = new Timer(t1.loops) {
@Override
void run() {
for (int i = 0, k = 0; i < iter; i++) {
PoissonGradientProcedure p = PoissonGradientProcedureFactory.create(func);
for (int j = loops; j-- > 0; ) p.computeFisherInformation(paramsList.get(k++ % iter));
}
}
};
long time2 = t2.getTime();
log("GradientCalculator = %d : PoissonGradientProcedure %d = %d : %fx\n", time1, nparams, time2, (1.0 * time1) / time2);
if (TestSettings.ASSERT_SPEED_TESTS) {
// Add contingency
Assert.assertTrue(time2 < time1 * 1.5);
}
}
use of gdsc.smlm.function.FakeGradientFunction in project GDSC-SMLM by aherbert.
the class PoissonGradientProcedureTest method gradientProcedureComputesSameAsGradientCalculator.
private void gradientProcedureComputesSameAsGradientCalculator(int nparams) {
int iter = 10;
rdg = new RandomDataGenerator(new Well19937c(30051977));
ArrayList<double[]> paramsList = new ArrayList<double[]>(iter);
createFakeParams(nparams, iter, paramsList);
int n = blockWidth * blockWidth;
FakeGradientFunction func = new FakeGradientFunction(blockWidth, nparams);
GradientCalculator calc = GradientCalculatorFactory.newCalculator(nparams, false);
String name = String.format("[%d]", nparams);
for (int i = 0; i < paramsList.size(); i++) {
PoissonGradientProcedure p = PoissonGradientProcedureFactory.create(func);
p.computeFisherInformation(paramsList.get(i));
double[][] m = calc.fisherInformationMatrix(n, paramsList.get(i), func);
// Exactly the same ...
double[] al = p.getLinear();
Assert.assertArrayEquals(name + " Observations: Not same alpha @ " + i, al, new DenseMatrix64F(m).data, 0);
double[][] am = p.getMatrix();
for (int j = 0; j < nparams; j++) Assert.assertArrayEquals(name + " Observations: Not same alpha @ " + i, am[j], m[j], 0);
}
}
use of gdsc.smlm.function.FakeGradientFunction in project GDSC-SMLM by aherbert.
the class LSQLVMGradientProcedureTest 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);
String name = GradientCalculator.class.getSimpleName();
for (int i = 0; i < paramsList.size(); i++) {
BaseLSQLVMGradientProcedure p1 = LSQLVMGradientProcedureFactory.create(yList.get(i), func);
p1.gradient(paramsList.get(i));
BaseLSQLVMGradientProcedure p2 = new LSQLVMGradientProcedure(yList.get(i), func);
p2.gradient(paramsList.get(i));
// Exactly the same ...
Assert.assertEquals(name + " Result: Not same @ " + i, p1.value, p2.value, 0);
Assert.assertArrayEquals(name + " Observations: Not same beta @ " + i, p1.beta, p2.beta, 0);
Assert.assertArrayEquals(name + " Observations: Not same alpha @ " + i, p1.getAlphaLinear(), p2.getAlphaLinear(), 0);
double[][] am1 = p1.getAlphaMatrix();
double[][] am2 = p2.getAlphaMatrix();
for (int j = 0; j < nparams; j++) Assert.assertArrayEquals(name + " Observations: Not same alpha @ " + i, am1[j], am2[j], 0);
}
}
use of gdsc.smlm.function.FakeGradientFunction in project GDSC-SMLM by aherbert.
the class LSQLVMGradientProcedureTest method gradientProcedureLinearIsFasterThanGradientProcedureMatrix.
private void gradientProcedureLinearIsFasterThanGradientProcedureMatrix(final int nparams, final BaseLSQLVMGradientProcedureFactory factory1, final BaseLSQLVMGradientProcedureFactory factory2, boolean doAssert) {
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 Gradient1Function func = new FakeGradientFunction(blockWidth, nparams);
for (int i = 0; i < paramsList.size(); i++) {
BaseLSQLVMGradientProcedure p = factory1.createProcedure(yList.get(i), func);
p.gradient(paramsList.get(i));
p.gradient(paramsList.get(i));
BaseLSQLVMGradientProcedure p2 = factory2.createProcedure(yList.get(i), func);
p2.gradient(paramsList.get(i));
p2.gradient(paramsList.get(i));
// Check they are the same
Assert.assertArrayEquals("A " + i, p.getAlphaLinear(), p2.getAlphaLinear(), 0);
Assert.assertArrayEquals("B " + i, p.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++) {
BaseLSQLVMGradientProcedure p = factory1.createProcedure(yList.get(i), func);
for (int j = loops; j-- > 0; ) p.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++) {
BaseLSQLVMGradientProcedure p2 = factory2.createProcedure(yList.get(i), func);
for (int j = loops; j-- > 0; ) p2.gradient(paramsList.get(k++ % iter));
}
}
};
long time2 = t2.getTime();
log("Standard %s = %d : Unrolled %s %d = %d : %fx\n", factory1.getClass().getSimpleName(), time1, factory2.getClass().getSimpleName(), nparams, time2, (1.0 * time1) / time2);
if (doAssert)
Assert.assertTrue(time2 < time1);
}
Aggregations