Skip to content

Instantly share code, notes, and snippets.

@unnonouno
Created August 9, 2016 15:20
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 unnonouno/d606c8570705c04deba21a9c7fb69890 to your computer and use it in GitHub Desktop.
Save unnonouno/d606c8570705c04deba21a9c7fb69890 to your computer and use it in GitHub Desktop.
import numpy
import chainer
import chainer.functions as F
import chainer.links as L
class LongChain(chainer.Chain):
def __init__(self):
super(LongChain, self).__init__()
links = []
for i in range(100):
links += [('conv{}'.format(i + 1),
L.Convolution2D(100, 100, 1, initialW=0.01))]
for link in links:
self.add_link(*link)
self.links = links
def __call__(self, x):
for i in range(0, len(self.links), 10):
links = self.links[i:i+10]
class n_conv(object):
def __init__(self, links):
self.links = links
def __call__(self, x):
for name, f in self.links:
x = f(x)
return x
x = F.forget(n_conv(links), x)
return x
m = LongChain()
x = chainer.Variable(numpy.random.uniform(-1, 1, (100, 100, 100)).astype('f')[None, ...])
y = F.sum(m(x))
m.zerograds()
y.backward()
print(m.conv1.W.grad)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment