22.
計算グラフ構築のパラダイム:Define-and-Run
• 計算グラフを構築した後に、データを計算グラフに順伝播する
• 計算グラフ構築⽅方法はフレームワークにより異異なる
• prototxt, yamlファイル, Luaスクリプト etc.
• 多くの深層学習フレームワークが採⽤用
• Caffe/Torch/Theanoベースのフレームワーク
• ⻑⾧長所
• メモリ管理理の必要がほとんどない
• 計算グラフの最適化を暗黙的に⾏行行える
• 短所
• 1訓練ループの中では計算グラフを変更更できない
f g
x f g
計算グラフ構築
データフィード
23.
計算グラフ構築のパラダイム:Define-by-Run
• データの順伝播とそのデータに対する計算グラフの
構築を同時に⾏行行う
• ⻑⾧長所
• 順伝播を通常のプログラムで記述できる
• コントロールフロー(条件分岐、forループ)
を計算グラフ構築に利利⽤用可能
• 設定ファイル⽤用のミニ⾔言語を作る必要がない
• 訓練データごとに異異なる計算グラフを変更更可能
• 短所
• 訓練データ全体に渡る最適化は⾃自明ではない
• 計算グラフを動的に構築するので、メモリ管理理が
必要
x yf
x = chainer.Variable(...)
y = f(x)
z = g(x)
zg
データフィード
= 計算グラフ構築
Chainerはこの
パラダイムを採⽤用
24.
Forwardと同時に計算グラフを構築
x = chainer.Variable(np.array(1))
y = chainer.Variable(np.array(1))
z = x**2 + 2*x*y + y
z.backward()
実際には Splitノードが⾃自動的に挿⼊入される
x
y
_ ** 2
2 * _ _ * _
_ + _ z
_ + _
chainer.Variable
chainer.Function
25.
Forwardと同時に計算グラフを構築
実際には Splitノードが⾃自動的に挿⼊入される
x
y
_ ** 2
2 * _ _ * _
_ + _ z
_ + _
x = chainer.Variable(np.array(1))
y = chainer.Variable(np.array(1))
z = x**2 + 2*x*y + y
z.backward()
26.
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%
27.
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%
28.
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%
29.
順伝播を定義
# 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%
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.
柔軟なグラフ操作:制御構⽂文を⽤用いた計算グラフ構築
• ネットワーク構築時に、通常の
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
訓練
テスト
……
……
32.
柔軟なグラフ操作:グラフの切切り落落とし
• 変数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で逆伝播時に遡る時刻を途中で打ち切切る⼿手法
33.
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
34.
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を選択
35.
まとめ
• ChainerはDefine-by-Runのコンセプトに基づ
いたPythonベースのディープラーニングフ
レームワークです
• 動的な計算グラフ構築を⾏行行うことで、柔軟な計
算グラフ構築をPythonプログラムとして記述
可能です
• CuPyを利利⽤用することでCPUとGPUの統⼀一的な
コード記述をサポートします
• 公式HP:http://chainer.org
• レポジトリ: https://github.com/pfnet/chainer
• Twitter:@ChainerOfficial
• Google Group:Chainer Uesr Group
• Contribution Guide:
http://docs.chainer.org/en/stable/contribution.html
git clone https://github.com/pfnet/chainer.git
Your Contribution is Welcomed!!
Be the first to comment