Created
June 24, 2019 19:23
-
-
Save larroy/30ce05563b2a39ab32e6de59fc3073ed to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
import mxnet as mx | |
import mxnet.autograd as ag | |
from mxnet import nd | |
from mxnet import gluon | |
import sys | |
def main(): | |
x = nd.array([[0.2131, 0.5449, 0.9910], | |
[0.1600, 0.1665, 0.1387], | |
[0.6242, 0.0409, 0.0663], | |
[0.6590, 0.9822, 0.3108], | |
[0.8566, 0.3848, 0.8385]]) | |
x.attach_grad() | |
net = gluon.nn.Sequential() | |
with net.name_scope(): | |
net.add(gluon.nn.Dense(3)) | |
#net.initialize() | |
net.initialize(mx.init.Xavier(magnitude=2.24)) | |
w = net.collect_params().values() | |
print(f"w {w}") | |
with ag.record(): | |
y = net.forward(x) | |
x_grad = ag.grad(y, x, create_graph=True, retain_graph=True)[0] | |
print(x_grad) | |
return 1 | |
if __name__ == '__main__': | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment