27. MNISTによる多層パーセプトロンの訓練
# (1) Model definition
model = FunctionSet(
l1=F.Linear(784, 100),
l2=F.Linear(100, 100),
l3=F.Linear(100, 10)).to_gpu()
opt = optimizers.SGD()
opt.setup(model)
# (2) Forward computation
def forward(x, t):
h1 = F.relu(model.l1(x))
h2 = F.relu(model.l2(h1))
y = model.l3(h2)
return F.softmax_cross_entropy(y, t)
# (3) Training loop
for epoch in xrange(n_epoch):
for i in xrange(0, N, batchsize):
x = Variable(to_gpu(...))
t = Variable(to_gpu(...))
opt.zero_grads()
loss = forward(x, t)
loss.backward()
opt.update()
784 100 100 10
0:2%
1:5%
2:90%
・
・
9:1%
28. FunctionSetでモデル定義
# Model definition
model = FunctionSet(
l1=F.Linear(784, 100),
l2=F.Linear(100, 100),
l3=F.Linear(100, 10)).to_gpu()
opt = optimizers.SGD()
opt.setup(model)
# Forward computation
def forward(x, t):
h1 = F.relu(model.l1(x))
h2 = F.relu(model.l2(h1))
y = model.l3(h2)
return F.softmax_cross_entropy(y, t)
# Training loop
for epoch in xrange(n_epoch):
for i in xrange(0, N, batchsize):
x = Variable(to_gpu(...))
t = Variable(to_gpu(...))
opt.zero_grads()
loss = forward(x, t)
loss.backward()
opt.update()
パラメータ付き
Functionは
FunctionSetで
まとめる
784 100 100 10
0:2%
1:5%
2:90%
・
・
9:1%
29. Optimizerのセットアップ
# Model definition
model = FunctionSet(
l1=F.Linear(784, 100),
l2=F.Linear(100, 100),
l3=F.Linear(100, 10)).to_gpu()
opt = optimizers.SGD()
opt.setup(model)
# Forward computation
def forward(x, t):
h1 = F.relu(model.l1(x))
h2 = F.relu(model.l2(h1))
y = model.l3(h2)
return F.softmax_cross_entropy(y, t)
# Training loop
for epoch in xrange(n_epoch):
for i in xrange(0, N, batchsize):
x = Variable(to_gpu(...))
t = Variable(to_gpu(...))
opt.zero_grads()
loss = forward(x, t)
loss.backward()
opt.update()
Optimizerに
パラメータ・勾配を
セット
784 100 100 10
0:2%
1:5%
2:90%
・
・
9:1%
30. 順伝播を定義
# Model definition
model = FunctionSet(
l1=F.Linear(784, 100),
l2=F.Linear(100, 100),
l3=F.Linear(100, 10)).to_gpu()
opt = optimizers.SGD()
opt.setup(model)
# Forward computation
def forward(x, t):
h1 = F.relu(model.l1(x))
h2 = F.relu(model.l2(h1))
y = model.l3(h2)
return F.softmax_cross_entropy(y, t)
# Training loop
for epoch in xrange(n_epoch):
for i in xrange(0, N, batchsize):
x = Variable(to_gpu(...))
t = Variable(to_gpu(...))
opt.zero_grads()
loss = forward(x, t)
loss.backward()
opt.update()
順伝播を定義
順伝播時に計算グ
ラフを動的構築
784 100 100 10
0:2%
1:5%
2:90%
・
・
9:1%
31. パラメータのアップデート
# Model definition
model = FunctionSet(
l1=F.Linear(784, 100),
l2=F.Linear(100, 100),
l3=F.Linear(100, 10)).to_gpu()
opt = optimizers.SGD()
opt.setup(model)
# Forward computation
def forward(x, t):
h1 = F.relu(model.l1(x))
h2 = F.relu(model.l2(h1))
y = model.l3(h2)
return F.softmax_cross_entropy(y, t)
# Training loop
for epoch in xrange(n_epoch):
for i in xrange(0, N, batchsize):
x = Variable(to_gpu(...))
t = Variable(to_gpu(...))
opt.zero_grads()
loss = forward(x, t)
loss.backward()
opt.update()
計算グラフ上
で逆伝播 パラメータの
アップデート
784 100 100 10
0:2%
1:5%
2:90%
・
・
9:1%
32. 柔軟なグラフ操作:制御構⽂文を⽤用いた計算グラフ構築
• ネットワーク構築時に、通常の
Pythonの制御構⽂文を利利⽤用でき
る (if / for / while etc…)
• 応⽤用
• 訓練・テストで層を取り替
える
• For⽂文を⽤用いてRNNを構築
• 訓練データごとに異異なる計
算グラフ
def forward(x, t, train=True):
h = F.relu(model.l1(x))
y = model.l2(h)
if train:
loss = F.softmax_cross_entropy(y, t)
return loss
else:
prob = F.softmax(y)
acc = F.accuracy(y, t)
return acc
…… y sce
lo
ss
…… y sm
pr
ob
acc
ac
c
訓練
テスト
……
……
33. 柔軟なグラフ操作:グラフの切切り落落とし
• 変数yより前のグラフを切切り落落とす
• yより前にはエラーが伝搬しない
• truncated BPTTを実装するのに便便利利
x f y g z
y g z
y.unchain_backward()
x = Variable(…)
y = f(x)
z = g(y)
y.unchain_backward()
BPTT:Back Propagation Through Time
RNNを時間⽅方向に展開したネットワーク上で逆伝播
をする操作(通常最初の時刻まで逆伝播する)
truncated BPTT
BPTTで逆伝播時に遡る時刻を途中で打ち切切る⼿手法
34. Caffe Reference Modelサポート
• Caffe Model Zooで提供されている
BVLC Reference ModelをChainerの
functionとして利利⽤用可能
func =
CaffeFunction('path/to/bvlc_ref
erence_caffenet.caffemodel')
x = Variable(…)
y, = func(inputs={'data': x},
outputs=['fc8'])
Caffe:最も使われているディープラーニングフレー
ムワークの⼀一つ、C++で書かれ画像認識識に強い
Model Zoo:Caffeの学習済モデルを公開したWiki
35. CuPy:GPU版NumPy
• cupy.ndarray
• numpy.ndarray上の操作の
サブセットをサポート
• スライス・配列列作成・線形
代数 etc…
• Elementwiseカーネル、
Reduction カーネルを⽤用いて、
独⾃自のカーネルの記述も可能
• CPUコードとGPUコードの統⼀一
的な記述をサポート
def softmax(x)
xp = get_array_module(x)
y = x – x.max(axis=1, keepdims=True)
y = xp.exp(y)
return y / y.sum(axis=1,
keepdims=True)
xp = numpy/cupy
いずれでもOK
⼊入⼒力力に応じて
numpy/cupyを選択