Skip to content

Instantly share code, notes, and snippets.

@junpenglao
Created February 16, 2018 11:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save junpenglao/cbe3b4f14397c19d08203794f7e1efc2 to your computer and use it in GitHub Desktop.
Save junpenglao/cbe3b4f14397c19d08203794f7e1efc2 to your computer and use it in GitHub Desktop.
import numpy
import theano
import theano.tensor as tt
from theano.gradient import disconnected_grad as stop_grad
x = tt.dscalar('x')
y = x ** 2
gy = tt.grad(y, x)
f = theano.function([x], gy)
f(4)
numpy.allclose(f(94.2), 188.4)
fy = theano.function([x], y)
fy(4)
def magicbox(x):
return tt.exp(x-stop_grad(x))
y2 = magicbox(x ** 2)
fy2 = theano.function([x], y2)
fy2(4)
gy2 = tt.grad(y2, x)
f2 = theano.function([x], gy2)
f2(4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment