In [1]:
import mxnet as mx
In [2]:
x = mx.nd.array([[1, 2], [3, 4]])
If we want to compute a function f with respect to x, we need a place to store it. Communicating that we want plan to store a gradient is done by invoking 'attack_grad()' function.
In [3]:
x.attach_grad()
MXNet will build the graph only when it is explicitly said so in order to minimize the computational requirements. Recoding the gradients it done as follow:
In [4]:
with mx.autograd.record():
y = x * 2
z = y * x
# Thus z = 2 * x * x
v = y / x
In [5]:
z.backward(retain_graph=True)
In [6]:
print(z)
In [7]:
print(x.grad)
In [8]:
print(y)
In [9]:
y.backward(retain_graph=True)
In [10]:
x.grad
Out[10]:
In [11]:
v.backward(retain_graph=True)
In [12]:
x.grad
Out[12]: