Last active
May 26, 2020 20:37
-
-
Save tswedish/f73640aa6878f23e8e62ec74fcda7e2d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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