How do I use the test_derivative parameter?

Asked by Martin Sandve Alnæs

Setting
  parameters["adjoint"]["test_derivative"] = True
before running the forward model,
then running compute_gradient or minimize,
does not show anything on screen, does that mean
it tests and passes or does it not do anything?

Question information

Language:
English Edit question
Status:
Solved
For:
dolfin-adjoint Edit question
Assignee:
No assignee Edit question
Solved by:
Martin Sandve Alnæs
Solved:
Last query:
Last reply:
Revision history for this message
Simon Funke (simon-funke) said :
#1

Try

dolfin.parameters["optimization"]["test_gradient"] = True

run an optimisation problem.

This perform a Taylor test for each gradient computation. The convergence results are only printed to the screen if dolfin's verbosity is higher than "ERROR" I believe.

Revision history for this message
Martin Sandve Alnæs (martinal) said :
#2

Thanks, now I get some feedback at least, but not in the form of printed convergence tests but an exception:

Traceback (most recent call last):
  File "./run.py", line 146, in <module>
    m_opt = minimize(Jred, options={"disp":True}) # bounds=bounds, tol=1e-6,
  File "/home/martinal/opt/fenics/dorsal-hpc-unstable-27022013/lib/python2.7/site-packages/dolfin_adjoint/optimization.py", line 155, in minimize
    opt = algorithm(reduced_func.eval_array, dj, [p.data() for p in reduced_func.parameter], H = reduced_func.hessian_array, **kwargs)
  File "/home/martinal/opt/fenics/dorsal-hpc-unstable-27022013/lib/python2.7/site-packages/dolfin_adjoint/optimization.py", line 68, in minimize_scipy_generic
    res = scipy_minimize(J, m_global, method = method, **kwargs)
  File "/home/martinal/opt/fenics/dorsal-hpc-unstable-27022013/lib/python2.7/site-packages/scipy/optimize/_minimize.py", line 351, in minimize
    return _minimize_lbfgsb(fun, x0, args, jac, bounds, **options)
  File "/home/martinal/opt/fenics/dorsal-hpc-unstable-27022013/lib/python2.7/site-packages/scipy/optimize/lbfgsb.py", line 294, in _minimize_lbfgsb
    f, g = func_and_grad(x)
  File "/home/martinal/opt/fenics/dorsal-hpc-unstable-27022013/lib/python2.7/site-packages/scipy/optimize/lbfgsb.py", line 250, in func_and_grad
    g = jac(x, *args)
  File "/home/martinal/opt/fenics/dorsal-hpc-unstable-27022013/lib/python2.7/site-packages/dolfin_adjoint/optimization.py", line 153, in <lambda>
    seed = dolfin.parameters["optimization"]["test_gradient_seed"])
  File "/home/martinal/opt/fenics/dorsal-hpc-unstable-27022013/lib/python2.7/site-packages/dolfin_adjoint/reduced_functional.py", line 268, in derivative_array
    raise RuntimeWarning, "A gradient test failed during execution."
RuntimeWarning: A gradient test failed during execution.

Revision history for this message
Simon Funke (simon-funke) said :
#3

Try setting the dolfin log level to "PROGRESS".

This is what I get for a failing Taylor test:

Absolute functional evaluation differences: [0.003765875736707003, 0.002723969792824521, 0.0012424620144580456, 0.0005718752936968441, 0.00027196293374170954]
Convergence orders for Taylor remainder without adjoint information (should all be 1): [0.46727469104010944, 1.1325089595886442, 1.1194292609168197, 1.072290541520944]
Absolute functional evaluation differences with adjoint: [0.00034858927323111032, 0.00066673728785546444, 0.00021384576197351732, 5.7567167454579946e-05, 1.4808870620577474e-05]
Convergence orders for Taylor remainder with adjoint information (should all be 2): [-0.9355902377118878, 1.640547797373497, 1.8932524834737394, 1.958784607825552]
Traceback (most recent call last):
  File "optimization.py", line 67, in <module>
    u_opt = minimize(reduced_functional, method = 'L-BFGS-B', bounds = (lb, 1), tol = 1e-10, options = {'disp': True})
  File "/data/sf1409/src/dolfin-adjoint/dolfin_adjoint/optimization.py", line 155, in minimize
    opt = algorithm(reduced_func.eval_array, dj, [p.data() for p in reduced_func.parameter], H = reduced_func.hessian_array, **kwargs)
  File "/data/sf1409/src/dolfin-adjoint/dolfin_adjoint/optimization.py", line 66, in minimize_scipy_generic
    res = scipy_minimize(J, m_global, method = method, bounds = bounds, **kwargs)
  File "/data/sf1409/src/scipy/install/lib/python2.7/site-packages/scipy/optimize/_minimize.py", line 356, in minimize
    callback=callback, **options)
  File "/data/sf1409/src/scipy/install/lib/python2.7/site-packages/scipy/optimize/lbfgsb.py", line 312, in _minimize_lbfgsb
    f, g = func_and_grad(x)
  File "/data/sf1409/src/scipy/install/lib/python2.7/site-packages/scipy/optimize/lbfgsb.py", line 263, in func_and_grad
    g = jac(x, *args)
  File "/data/sf1409/src/dolfin-adjoint/dolfin_adjoint/optimization.py", line 153, in <lambda>
    seed = dolfin.parameters["optimization"]["test_gradient_seed"])
  File "/data/sf1409/src/dolfin-adjoint/dolfin_adjoint/reduced_functional.py", line 268, in derivative_array
    raise RuntimeWarning, "A gradient test failed during execution."
RuntimeWarning: A gradient test failed during execution.

Revision history for this message
Martin Sandve Alnæs (martinal) said :
#4

Thanks, that's better. I get output twice, the first looks good while the other does not. Does that mean the gradient is correct, but convergence slows down at the second iteration? Maybe it gets very close to the solution.

Absolute functional evaluation differences: [7.957034820066378e-07, 3.978612328377207e-07, 1.9893298903138346e-07, 9.94670876462711e-08, 4.973369208474687e-08]
Convergence orders for Taylor remainder without adjoint information (should all be 1): [0.9999655810000057, 0.999982793317739, 0.9999913970629176, 0.9999956991608453]
Absolute functional evaluation differences with adjoint: [3.7973301000355673e-11, 9.4948160983798205e-12, 2.3747955260738636e-12, 5.9426718366310783e-13, 1.4887198051024414e-13]
Convergence orders for Taylor remainder with adjoint information (should all be 2): [1.9997734544523396, 1.9993367573138945, 1.9986196798505018, 1.9970394683518387]
Gradient test succesfull.

...
At iterate 0 f= 1.41492D-02 |proj g|= 1.52921D-05
...

Absolute functional evaluation differences: [7.869470966849479e-07, 3.934830405498413e-07, 1.9674389336796216e-07, 9.837253992298067e-08, 4.918641825953085e-08]
Convergence orders for Taylor remainder without adjoint information (should all be 1): [0.9999651966558515, 0.9999825983406555, 0.9999912997514947, 0.9999956502388724]
Absolute functional evaluation differences with adjoint: [7.4155081689509456e-11, 2.7585333477409377e-11, 1.1419573697185796e-11, 5.1165478490018257e-12, 2.4099758839866075e-12]
Convergence orders for Taylor remainder with adjoint information (should all be 2): [1.4266441396516127, 1.27239262660796, 1.1582661419572653, 1.0861520377041614]

Revision history for this message
Simon Funke (simon-funke) said :
#5

Your remainders are pretty small (1e-12) and might be close to your solver tolerances.
Therefore, I would try a larger seed value (this value specifies the perturbation step size in the Taylor test), which you can set with

dolfin.parameters["optimization"]["test_gradient_seed"] = 0.001

The default is 0.0001, so give 0.001 a try.

Revision history for this message
Martin Sandve Alnæs (martinal) said :
#6

Increasing the seed to 0.001 gave larger convergence, and 0.01 gave > 1.9 for the second test. It drops below 1.9 for the third test, but I guess that's just sensitivity to a badly scaled problem setup or something and the gradient computation itself is good. Thanks!