自然言語処理に適した ニューラルネットのフレームワーク - - - dynet - - -
TRANSCRIPT
自然言語処理に適したニューラルネットのフレームワーク
- - - DyNet - - -
2.24,2017Masaya Ogushi
1
注意• このスライドはニューラルネットについて知っている人向けです。
• 説明のために簡単なC++のコードを使っています。
• もしニューラルネットについて知らない場合は下記を見てください。
• http://www.slideshare.net/unnonouno/ss-43844132
2
Agenda• Self Introduction
• Why do you use the DyNet ?
• What is DyNet ?
• How to apply the DyNet for Natural language processing ?
3
Agenda• Self Introduction
• Why do you use the DyNet ?
• What is DyNet ?
• How to apply the DyNet for natural language processing ?
4
Self Introduction
5
Self Introduction
• 名前:大串 正矢
• @SnowGushiGit
• Kabuku Inc.
• 3次元の画像処理をCNNでやっています。
• 興味:健康関連
6
Self Introduction
Kabuku Inc. Hiring Engineer
• サーバーサイド
• Python
• フロント
• Type Script, Angular
7
Why do you use the DyNet ?
8
Agenda• Self Introduction
• Why do you use the DyNet ?
• What is DyNet ?
• How to apply the DyNet for natural language processing ?
9
Why do you use the DyNet?
• 深層学習に使えるフレームワークは多い・・・
10
Why do you use the DyNet?
• DyNet が他のフレームワークよりも優れている点
• Python と C++(高速化)
• 動的なネットワークの操作(NLPにおいて重要)
• 自然言語処理のための使いやすいインターフェース
11
Why do you use the DyNet?• CPUでのパフォーマンス
12
Why do you use the DyNet?• GPUでのパフォーマンス
もし他のパフォーマンスにも気になる人は論文を参照ください。https://arxiv.org/pdf/1701.03980v1.pdf
13
Why do you use the DyNet?
• 動的な操作は自然言語処理において重要
• 言語の特徴
• 連続的、ツリー、グラフ
14
Why do you use the DyNet?
• 下記のような状況の場合はDyNetがオススメです。
• 自然言語処理
• 性能向上が必要
15
Agenda• Self Introduction
• Why do you use the DyNet ?
• What is DyNet ?
• How to apply the DyNet for natural language processing ?
16
What is DyNet ?
• Computation Graph
• Expressions( nodes in the graph)
• Parameters
• Model
• a collection of parameters
• Trainer17
What is DyNet ?
• Computation Graph
• Expressions( nodes in the graph)
• Parameters
• Model
• a collection of parameters
• Trainer18
What is DyNet ?
• Case1 掛け算
19
What is DyNet ?
20
• Computation Graph and Expression
• Graphを作成
• 下記のコードで下記のグラフを実現しています。
ComputationGraph cg; float ia, ib; Expression a = input(cg, &ia); Expression b = input(cg, &ib); Expression y = a * b;
Code Cpp
What is DyNet ?
21
• Computation Graph and Expression
• 値を設定します。
• 値を計算します。
1 2
2 ia = 1; ib = 2; cout << as_scalar(cg.forward(y)) << endl; // 2
Code Cpp
What is DyNet ?
• Computation Graph
• Expressions( nodes in the graph)
• Parameters
• Model
• a collection of parameters
• Trainer22
What is DyNet ?
• Case2 xor計算
23
What is DyNet ?
24
• Model and Parameters
• Parameters は最適化するもの(重み, 正則化項)
• 下記は出力層と1層目(xorの入力とユニット数)だけ定義 Model m;
Parameter p_W, p_b, p_V, p_a;
const unsigned HIDDEN_SIZE = 8;p_W = m.add_parameters({HIDDEN_SIZE, 2});p_b = m.add_parameters({HIDDEN_SIZE});p_V = m.add_parameters({1, HIDDEN_SIZE});p_a = m.add_parameters({1});
Code Cpp
What is DyNet ?
25
• グラフの構成
Expression W = parameter(cg, p_W);Expression b = parameter(cg, p_b);Expression V = parameter(cg, p_V);Expression a = parameter(cg, p_a);
vector<dynet::real> x_values(2);Expression x = input(cg, {2}, &x_values);
Expression h = tanh(W*x + b);
Expression y_pred = V*h + a;
dynet::real y_value;Expression y = input(cg, &y_value);Expression loss_expr = squared_distance(y_pred, y);
Code Cpp
x_values
y_values
W
b
a
tanh
+
V
+
x
y
squared_distance
What is DyNet ?
• Computation Graph
• Expressions( nodes in the graph)
• Parameters
• Model
• a collection of parameters
• Trainer26
What is DyNet ?
27
• Trainer
• Trainer に先ほどのモデルを与えて初期化します。
SimpleSGDTrainer sgd(m);
Code Cpp
What is DyNet ?
28
• Trainer Forward Backprop
• XORの入力値を設定します。(アドレス注意)
• XORの出力値を設定します。(アドレス注意)
• forward処理でlossを計算します。
• backward処理で勾配を計算します。
• parametersを更新します。
for (unsigned iter = 0; iter < ITERATIONS; ++iter) { double loss = 0; for (unsigned mi = 0; mi < 4; ++mi) { bool x1 = mi % 2; bool x2 = (mi / 2) % 2; x_values[0] = x1 ? 1 : -1; x_values[1] = x2 ? 1 : -1; y_value = (x1 != x2) ? 1 : -1; loss += as_scalar(cg.forward(loss_expr)); cg.backward(loss_expr); sgd.update(1.0); } sgd.update_epoch(); loss /= 4; cerr << "E = " << loss << endl;}
Code Cpp
Agenda• Self Introduction
• Why do you use the DyNet ?
• What is DyNet ?
• How to apply the DyNet for natural language processing ?
29
How to apply the DyNet for natural language processing ?
30
ipython notebookを用いて説明します。
https://github.com/SnowMasaya/DyNet_Study_Docker/blob/master/dynet-py
thon/RNNS%20DyNet.ipynb
• Reference• DyNet
• https://github.com/clab/dynet• Practical Neural Networks for NLP
• http://demo.clab.cs.cmu.edu/cdyer/emnlp2016-dynet-tutorial-part1.pdf• Practical Neural Networks for NLP
• http://demo.clab.cs.cmu.edu/cdyer/emnlp2016-dynet-tutorial-part2.pdf• ニューラルネットのフレームワークDyNetの紹介
• http://qiita.com/odashi_t/items/237a34d56e5d2a1df7ae• TensorFlow
• https://www.tensorflow.org/• A Powerful, Flexible, and Intuitive Framework for Neural Networks
• http://chainer.org/• Caffe
• http://caffe.berkeleyvision.org/• theano
• http://www.deeplearning.net/software/theano/
• RNNS tutorial• https://github.com/clab/dynet/blob/master/examples/python/tutorials/RNNs.ipynb
• 深層学習時代の自然言語処理 • http://www.slideshare.net/unnonouno/ss-43844132
31