Created
January 11, 2018 05:47
-
-
Save delta2323/0d4ca8896a7cabdc41b79c75212cd222 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 chainer | |
import chainer.links as L | |
import chainer.functions as F | |
import numpy as np | |
class CNN(chainer.Chain): | |
def __init__(self): | |
super(CNN, self).__init__() | |
with self.init_scope(): | |
self.cnv1 = L.Convolution2D(None, 32, 3, 1, 1) | |
self.cnv2 = L.Convolution2D(None, 64, 3, 1, 1) | |
self.cnv3 = L.Convolution2D(None, 64, 3, 1, 1) | |
def __call__(self, x): | |
y = F.relu(self.cnv1(x)) | |
y.name = 'cnv1' | |
y = F.relu(self.cnv2(y)) | |
y.name = 'cnv2' | |
y = F.relu(self.cnv3(y)) | |
y.name = 'cnv3' | |
return y | |
def make_grad(shape): | |
return np.random.uniform(-1, 1, shape).astype(np.float32) | |
def get_target(y, target): | |
while y is not None: | |
if y.name == target: | |
return y | |
else: | |
if y.creator is None: | |
return None | |
else: | |
y = y.creator.inputs[0] | |
return None | |
print(chainer.__version__) | |
input = np.random.uniform(-1, 1, (10, 10, 10, 10)).astype(np.float32) | |
net = CNN() | |
y = net(input) | |
y.grad = make_grad(y.shape) | |
y.backward(retain_grad=True) | |
target = get_target(y, 'cnv3') | |
print(target.grad.shape) | |
""" | |
v3 | |
%python test.py | |
3.2.0 | |
(10, 64, 10, 10) | |
""" | |
""" | |
v2 | |
%python test.py | |
2.1.0 | |
(10, 64, 10, 10) | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment