Created
August 9, 2016 15:20
-
-
Save unnonouno/d606c8570705c04deba21a9c7fb69890 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
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