10

More than 5 years have passed since last update.

posted at

# Chainerを使った多層パーセプトロン：関数フィッティング

Chainerの勉強を目的に、人工知能に関する断創録_多層パーセプトロンによる関数近似に記載の事柄をChainerを用いて実装してみました。

fitting_with_MLP.py
``````import numpy as np
import matplotlib.pyplot as plt
import chainer
from chainer import cuda, Function, gradient_check, Variable, optimizers, serializers, utils
from chainer import Link, Chain, ChainList
import chainer.functions as F

class MyChain(Chain):
def __init__(self):
super(MyChain, self).__init__(
l1 = L.Linear(1, 10),
l2 = L.Linear(10, 1),
)

def __call__(self, x):
h = F.sigmoid(self.l1(x))
out = self.l2(h)
return out

class MyModel_fitting(Chain):
def __init__(self, predictor):
super(MyModel_fitting, self).__init__(predictor=predictor)

def __call__(self, x, t):
y = self.predictor(x)
loss = F.mean_squared_error(y,t)*0.5
#        loss = (y-t)*(y-t)*0.5
return loss

def predict(self, x):
y = self.predictor(x)
return y

if __name__ == "__main__":
train_x = np.linspace(0.0, 1.0, num=1000, dtype=np.float32)
train_y = train_x*train_x
n_epoch = 1000
n_batch = 100

model = MyModel_fitting(MyChain())
optimizer.setup(model)

for epoch in range(n_epoch):
print 'epoch : ', epoch
indexes = np.random.permutation(np.size(train_x))
for i in range(n_batch):
x = Variable(np.array([[train_x[indexes[i]]]], dtype=np.float32))
t = Variable(np.array([[train_y[indexes[i]]]], dtype=np.float32))
loss = model(x, t)
loss.backward()
optimizer.update()
print 'loss : ', loss.data

learned_y = np.zeros_like(train_y)
for i in range(np.size(learned_y)):
x = Variable(np.array([[train_x[i]]], dtype=np.float32))
learned_y[i] = model.predict(x).data[0,0]

plt.plot(train_x, train_y, 'o')
plt.plot(train_x, learned_y)
plt.show()
serializers.save_hdf5('MyChain.model', model)
``````

Chainerはミニバッチ学習を前提とした実装となっているそうで、[[入力ベクトル1],[入力ベクトル2],・・・]と入れることでミニバッチ学習が行われます。
オンライン学習を行う場合は、[[入力ベクトル1]]のように、括弧をひとつ多くつける必要があります。

Register as a new user and use Qiita more conveniently

1. You get articles that match your needs
2. You can efficiently read back useful information
What you can do with signing up
10