Skip to content

Instantly share code, notes, and snippets.

@tswedish
Last active May 26, 2020 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tswedish/f73640aa6878f23e8e62ec74fcda7e2d to your computer and use it in GitHub Desktop.
Save tswedish/f73640aa6878f23e8e62ec74fcda7e2d to your computer and use it in GitHub Desktop.
# compose a graph of nodes
x = Variable(10.01)
y = Variable(0.05)
z = x * y
m = 1.3
q = m+y
L = (q - (z**(m/2.) + z**2. - 1./z))**2
# the forward calculation
print(L.value)
# the backward calculation
L.backward()
# gradients of the inputs are updated
print('x: {} \ny: {}'.format(x.gradient, y.gradient))
# clear gradients if we don't want to accumulate on next backward()
# will follow tree down to clear gradient of x,y as well
L.clear_gradient()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment