Skip to content

Instantly share code, notes, and snippets.

@dwf
Created October 19, 2011 03:42
Show Gist options
  • Save dwf/1297430 to your computer and use it in GitHub Desktop.
Save dwf/1297430 to your computer and use it in GitHub Desktop.
Demonstrates an issue with the numerical stability of the gradient of arccos.
import theano.tensor as tensor
from theano import function
import numpy as np
value = np.array([ 0.992827313923019039165751564724,
0.118152792712208978831434080803,
-0.008531793400639134036800292904,
0.016157066691618496290239193058])
aa = tensor.vector('aa')
bb = tensor.vector('bb')
ee = tensor.scalar()
cc = tensor.arccos(tensor.clip((aa * bb).sum(), -1, 1 - ee))
dd = tensor.grad(cc, bb)
g = function([aa, bb, ee], dd)
print g(value, value, 1e-7)
print g(value, value, 1e-8)
print g(value, value, 1e-9)
print g(value, value, 0)
Output of the above Python code as of Theano master 11ee6cee3b:
wardefar@grieg:~$ python test.py
[-0. -0. 0. -0.]
[-0. -0. 0. -0.]
[-0. -0. 0. -0.]
[-66627513.18554519 -7929099.69734382 572558.96299959
-1084282.39124676]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment