Created
June 10, 2015 08:14
-
-
Save ktnyt/d2e63256ef28f8cb9dd3 to your computer and use it in GitHub Desktop.
Minimum Chainer implementation of 3-Layer Perceptron for solving XOR
This file contains 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
#!/usr/bin/env python | |
import numpy as np | |
from chainer import cuda, Function, FunctionSet, gradient_check, Variable, optimizers | |
import chainer.functions as F | |
x_train = np.array([ | |
[[0., 0.]], | |
[[0., 1.]], | |
[[1., 0.]], | |
[[1., 1.]] | |
], dtype=np.float32) | |
y_train = np.array([[0], [1], [1], [0]], dtype=np.int32) | |
model = FunctionSet( | |
l1=F.Linear(2, 2), | |
l2=F.Linear(2, 1) | |
) | |
def forward(x_data, y_data): | |
x = Variable(x_data) | |
t = Variable(y_data) | |
h = F.sigmoid(model.l1(x)) | |
y = F.sigmoid(model.l2(h)) | |
return F.mean_squared_error(y, t) | |
optimizer = optimizers.MomentumSGD() | |
optimizer.setup(model.collect_parameters()) | |
mean_loss = 0 | |
last_epoch = 0 | |
for epoch in xrange(10000): | |
sum_loss = 0 | |
for i in xrange(len(x_train)): | |
optimizer.zero_grads() | |
loss = forward(x_train[i], y_train[i]) | |
sum_loss += loss.data | |
loss.backward() | |
optimizer.update() | |
mean_loss = sum_loss / len(x_train) | |
last_epoch = epoch + 1 | |
if last_epoch % 1000 == 0: | |
print "Epoch %d\tLoss %f" % (last_epoch, mean_loss) | |
for i in xrange(len(x_train)): | |
x = Variable(x_train[i]) | |
h = F.sigmoid(model.l1(x)) | |
y = F.sigmoid(model.l2(h)) | |
loss = forward(x_train[i], y_train[i]) | |
print loss.data, y_train[i], y.data | |
print last_epoch, mean_loss | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment