Last active
August 29, 2015 14:22
-
-
Save tomonari-masada/af7caa01383156f56f05 to your computer and use it in GitHub Desktop.
MLP sample code (Chainer)
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
import sys | |
import numpy as np | |
from scipy import special | |
from chainer import cuda, Function, FunctionSet, gradient_check, Variable, optimizers | |
import chainer.functions as F | |
def target(args, params): | |
return np.exp(special.gammaln(np.sum(params)) | |
- np.sum(special.gammaln(params)) | |
+ np.sum((params - 1.) * np.log(args))) | |
rng = np.random | |
rng.seed(0) | |
N = 1000 | |
s = rng.exponential(1., N*3) | |
s = s.reshape(3, N) | |
s /= np.sum(s, axis=0) | |
x = [] | |
y = [] | |
for i in range(N): | |
x.append(s[:, i]) | |
y.append([target(s[:, i], np.array([3., 3., 3.]))]) | |
x_all = np.array(x) | |
y_all = np.array(y) | |
data_dim = x_all.shape[1] | |
model = FunctionSet(l1=F.Linear(data_dim, data_dim*2), | |
l2=F.Linear(data_dim*2, data_dim*3), | |
l3=F.Linear(data_dim*3, data_dim*2), | |
l4=F.Linear(data_dim*2, data_dim), | |
l5=F.Linear(data_dim, 1)) | |
def forward(x_data, y_data): | |
x, t = Variable(x_data), Variable(y_data) | |
h1 = F.sigmoid(model.l1(x)) | |
h2 = F.sigmoid(model.l2(h1)) | |
h3 = F.sigmoid(model.l3(h2)) | |
h4 = F.sigmoid(model.l4(h3)) | |
y = model.l5(h4) | |
return F.mean_squared_error(y, t) | |
optimizer = optimizers.Adam() | |
optimizer.setup(model.collect_parameters()) | |
iteration = 0 | |
max_iteration = 1000000 | |
batchsize = 100 | |
while iteration < max_iteration: | |
indexes = np.random.permutation(N) | |
sum_loss = 0 | |
for i in xrange(0, N, batchsize): | |
x_batch = x_all[indexes[i : i + batchsize]] | |
y_batch = y_all[indexes[i : i + batchsize]] | |
optimizer.zero_grads() | |
loss = forward(x_batch, y_batch) | |
loss.backward() | |
optimizer.update() | |
sum_loss += loss.data * batchsize | |
if iteration % 1000 == 0: | |
print '{:.5f}'.format(np.sqrt(sum_loss[0] / N)) | |
sys.stdout.flush() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment