Skip to content

Instantly share code, notes, and snippets.

@harujoh
Created June 21, 2016 10:14
Show Gist options
  • Save harujoh/2fa6e4e6ea5b16d866046b0a2b48e73f to your computer and use it in GitHub Desktop.
Save harujoh/2fa6e4e6ea5b16d866046b0a2b48e73f to your computer and use it in GitHub Desktop.
import numpy as np
from chainer import Function, Variable, optimizers
from chainer import Chain
import chainer.functions as F
import chainer.links as L
class NN(Chain):
def __init__(self):
initial_W1 = np.array([[[[1.0, 0.5,0.0],[0.5, 0.0,-0.5],[0.0, -0.5,-1.0]]],
[[[0.0, -0.1, 0.1],[-0.3, 0.4, 0.7],[0.5, -0.2, 0.2]]]], dtype=np.float32)
initial_W2 = np.array([[[[-0.1, 0.6],[0.3, -0.9]],[[0.7, 0.9],[-0.2, -0.3]]],[[[-0.6, -0.1],[0.3, 0.3]],[[-0.5, 0.8],[0.9, 0.1]]]], dtype=np.float32)
initial_W3 = np.array([[0.5, 0.3, 0.4, 0.2, 0.6, 0.1, 0.4, 0.3],[0.6,0.4,0.9,0.1,0.5,0.2,0.3,0.4]], dtype=np.float32)
initial_b3 = np.array([0.01, 0.02], dtype=np.float32)
initial_W4 = np.array([[0.8, 0.2], [0.4, 0.6]], dtype=np.float32)
initial_b4 = np.array([0.02, 0.01], dtype=np.float32)
super(NN, self).__init__(
conv1=F.Convolution2D(1,2,3,initialW=initial_W1,nobias = True),
conv2=F.Convolution2D(2,2,2,initialW=initial_W2,nobias = True),
fl3=F.Linear(8,2,initialW=initial_W3,initial_bias=initial_b3),
fl4=F.Linear(2,2,initialW=initial_W4,initial_bias=initial_b4)
)
def __call__(self, x):
h_conv1 = F.relu(self.conv1(x))
#print(h_conv1.data)
h_pool1 = F.max_pooling_2d(h_conv1, 2)
#print(h_pool1.data)
h_conv2 = F.relu(self.conv2(h_pool1))
#print(h_conv2.data)
h_pool2 = F.max_pooling_2d(h_conv2, 2)
#print(h_pool2.data)
h_fc1 = F.relu(self.fl3(h_pool2))
#print(h_fc1.data)
y = self.fl4(h_fc1)
#print(y.data)
return y
class SquaredError(Function):
def forward(self, inputs):
x0, x1 = inputs
self.diff = x0 - x1
diff = self.diff.ravel()
return np.array(diff.dot(diff) / 2.),
def backward(self, inputs, gy):
gx0 = self.diff
return gx0, -gx0
def squared_error(x0, x1):
return SquaredError()(x0, x1)
if __name__ == '__main__':
nn = NN()
x = Variable(np.array([[[
[0,0,0,0,0,0.2,0.9,0.2,0,0,0,0],
[0,0,0,0,0.2,0.8,0.9,0.1,0,0,0,0],
[0,0,0,0.1,0.8,0.5,0.8,0.1,0,0,0,0],
[0,0,0,0.3,0.3,0.1,0.7,0.2,0,0,0,0],
[0,0,0,0.1,0,0.1,0.7,0.2,0,0,0,0],
[0,0,0,0,0,0.1,0.7,0.1,0,0,0,0],
[0,0,0,0,0,0.4,0.8,0.1,0,0,0,0],
[0,0,0,0,0,0.8,0.4,0.1,0,0,0,0],
[0,0,0,0,0.2,0.8,0.3,0,0,0,0,0],
[0,0,0,0,0.1,0.8,0.2,0,0,0,0,0],
[0,0,0,0,0.1,0.7,0.2,0,0,0,0,0],
[0,0,0,0,0,0.3,0,0,0,0,0,0]
]]], dtype=np.float32))
t = Variable(np.array([[0, 1]], dtype=np.float32))
y = nn(x)
optimizer = optimizers.SGD(lr=0.1)
optimizer.setup(nn)
nn.zerograds()
loss = squared_error(y, t)
loss.backward()
print("W^1 grad\n{}".format(nn.conv1.W.grad))
print()
#print("W^2 grad\n{}".format(nn.conv2.W.grad))
#print()
#print("W^3 grad\n{}".format(nn.fl3.W.grad))
#print()
#print("W^4 grad\n{}".format(nn.fl4.W.grad))
#print()
#print("b^3 grad\n{}".format(nn.fl3.b.grad))
#print()
#print("b^4 grad\n{}".format(nn.fl4.b.grad))
#print()
optimizer.update()
print("after W^1\n{}".format(nn.conv1.W.data))
#print()
#print("after W^1\n{}".format(nn.conv2.W.data))
#print()
#print("after W^3\n{}".format(nn.fl3.W.data))
#print()
#print("after W^4\n{}".format(nn.fl4.W.data))
#print()
#print("after b^3\n{}".format(nn.fl3.b.data))
#print()
#print("after b^4\n{}".format(nn.fl4.b.data))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment