Skip to content

Instantly share code, notes, and snippets.

@delta2323
Last active November 21, 2016 05:47
Show Gist options
  • Save delta2323/41424985ad1057c4435b31f07dd16d34 to your computer and use it in GitHub Desktop.
Save delta2323/41424985ad1057c4435b31f07dd16d34 to your computer and use it in GitHub Desktop.
import chainer
import chainer.links as L
import chainer.functions as F
import numpy as np
class Wrapper(chainer.Chain):
pass
x = chainer.Variable(np.random.uniform(-1, 1, (1, 3)).astype(np.float32))
l = L.Linear(3, 2)
l2 = Wrapper(l=l)
o = chainer.optimizers.SGD(0.01)
o.setup(l)
p = chainer.optimizers.SGD(0.01)
p.setup(l2)
for i in range(10):
l.cleargrads()
y = l(x)
w = F.sum(y * y)
w.backward()
if i % 2 == 0:
o.update()
else:
p.update()
print(np.linalg.norm(l.W.data))
'''
1.66004
1.62767
1.5969
1.56765
1.53986
1.51344
1.48833
1.46447
1.44179
1.42025
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment