17. # 構築
x = Variable(‘x’)
y = Variable(‘y’)
z = x + 2 * y
# 評価
for xi, yi in data:
eval(z, (xi, yi))
# 構築と評価が同時
for xi, yi in data:
x = Variable(xi)
y = Variable(yi)
z = x + 2 * y
データを見ながら
違う処理をしてもよい
Define-and-Run Define-by-Run
18. Convolutional Networkを書く方法1
import chainer
import chainer.links as L
import chainer.functions as F
class LeNet5(chainer.Chain):
def __init__(self):
super(LeNet5, self).__init__()
with self.init_scope():
self.conv1 = L.Convolution2D(1, 6, 5, 1)
self.conv2 = L.Convolution2D(6, 16, 5, 1)
self.conv3 = L.Convolution2D(16, 120, 4, 1)
self.fc4 = L.Linear(None, 84)
self.fc5 = L.Linear(84, 10)
S
※V2対応コードへ変更したため発表
時の資料とは若干異なります
19. Convolutional Networkを書く方法2
class LeNet5(chainer.Chain):
def __init__(self):
super(LeNet5, self).__init__()
net = [('conv1', L.Convolution2D(1, 6, 5, 1))]
net += [('_sigm1', F.Sigmoid())]
net += [('_mpool1', F.MaxPooling2D(2, 2))]
net += [('conv2', L.Convolution2D(6, 16, 5, 1))]
net += [('_sigm2', F.Sigmoid())]
net += [('_mpool2', F.MaxPooling2D(2, 2))]
net += [('conv3', L.Convolution2D(16, 120, 4, 1))]
net += [('_sigm3', F.Sigmoid())]
net += [('_mpool3', F.MaxPooling2D(2, 2))]
net += [('fc4', L.Linear(None, 84))]
net += [('_sigm4', F.Sigmoid())]
net += [('fc5', L.Linear(84, 10))]
net += [('_sigm5', F.Sigmoid())]
for name, layer in net:
if not name.startswith('_'):
with self.init_scope():
setattr(self, name, layer)
self.forward = net
def __call__(self, x):
for n, f in self.forward:
if not n.startswith('_'):
x = getattr(self, n)(x)
else:
x = f(x)
return x
※V2対応コードへ変更したため発表
時の資料とは若干異なります
27. Intel Chainer Chainer with NumPy (MKL-Build)
Alexnet Forward 429.16 ms 5041.91 ms
Alexnet Backward 841.73 ms 5569.49 ms
Alexnet Total 1270.89 ms 10611.40 ms
およそ8.35倍の高速化!