Skip to content

Instantly share code, notes, and snippets.

@msakai
Last active Aug 29, 2015
Embed
What would you like to do?
#!/usr/bin/env python
import numpy as np
import six
import chainer
from chainer import computational_graph as c
from chainer import cuda
import chainer.functions as F
from chainer import optimizers
inputlayersize = 400
hiddenlayersize = 25
numlabel = 10
# Data files are available from the following URLs.
# * https://dl.dropboxusercontent.com/u/123796/gist/b553004a7ca76310e11c/data/ex4data1_X.csv.gz
# * https://dl.dropboxusercontent.com/u/123796/gist/b553004a7ca76310e11c/data/ex4data1_y.csv
#
# They are converted from ex4data1.mat using Octave as below.
#
# > load('ex4data1.mat');
# > csvwrite("ex4data1_X.csv", X);
# > csvwrite("ex4data1_y.csv", y);
x = np.loadtxt("ex4data1_X.csv", delimiter=",", dtype=np.float32)
y = np.loadtxt("ex4data1_y.csv", delimiter=",", dtype=np.int_)
y = np.vectorize(lambda x: 0 if x == 10 else x, otypes = [np.int_])(y)
# Prepare multi-layer perceptron model
model = chainer.FunctionSet(l1=F.Linear(inputlayersize, hiddenlayersize),
l2=F.Linear(hiddenlayersize, numlabel))
# Neural net architecture
def forward(x):
x = chainer.Variable(x)
return F.sigmoid(model.l2(F.sigmoid(model.l1(x))))
def cost(x, y):
o = forward(x)
y2 = np.zeros([len(y), numlabel], np.float32)
for i in xrange(y.shape[0]):
y2[i, y[i]] = 1
c = F.sum(F.sum(- y2 * F.log(o) - (1 - y2) * F.log(1 - o))) / len(y)
return c
def predict(x):
return forward(x).data.argmax(axis=1)
# Setup optimizer
optimizer = optimizers.SGD()
optimizer.setup(model.collect_parameters())
for i in xrange(1000):
o = cost(x, y)
print o.data
optimizer.zero_grads()
o.backward()
optimizer.update()
p = predict(x)
print("precision (training data): %f" % (sum(1.0 for i in xrange(y.shape[0]) if p[i] == y[i]) / y.shape[0]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment