自然言語処理に適した
ニューラルネットのフレームワーク
- - - DyNet - - -
2.24,2017
Masaya Ogushi
1
注意
• このスライドはニューラルネットについて知って
いる人向けです。
• 説明のために簡単なC++のコードを使っていま
す。
• もしニューラルネットについて知らない場合は下
記を見てください。
• http://www.slideshare.net/unnonouno/ss-4
3844132
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?
• 深層学習に使えるフレームワークは多い・・・
1
0
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 ?
1
6
What is DyNet ?
• Computation Graph
• Expressions( nodes in the graph)
• Parameters
• Model
• a collection of parameters
• Trainer
17
What is DyNet ?
• Computation Graph
• Expressions( nodes in the graph)
• Parameters
• Model
• a collection of parameters
• Trainer
18
What is DyNet ?
• Case1 掛け算
1
9
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
• Trainer
22
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
• Trainer
26
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/DyNe
t_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

自然言語処理に適した ニューラルネットのフレームワーク - - - DyNet - - -

  • 1.
  • 2.
    注意 • このスライドはニューラルネットについて知って いる人向けです。 • 説明のために簡単なC++のコードを使っていま す。 •もしニューラルネットについて知らない場合は下 記を見てください。 • http://www.slideshare.net/unnonouno/ss-4 3844132 2
  • 3.
    Agenda • Self Introduction •Why do you use the DyNet ? • What is DyNet ? • How to apply the DyNet for Natural language processing ? 3
  • 4.
    Agenda • Self Introduction •Why do you use the DyNet ? • What is DyNet ? • How to apply the DyNet for natural language processing ? 4
  • 5.
  • 6.
    Self Introduction • 名前:大串 正矢 •@SnowGushiGit • Kabuku Inc. • 3次元の画像処理をCNNでやっていま す。 • 興味:健康関連 6
  • 7.
    Self Introduction Kabuku Inc.Hiring Engineer • サーバーサイド • Python • フロント • Type Script, Angular 7
  • 8.
    Why do youuse the DyNet ? 8
  • 9.
    Agenda • Self Introduction •Why do you use the DyNet ? • What is DyNet ? • How to apply the DyNet for natural language processing ? 9
  • 10.
    Why do youuse the DyNet? • 深層学習に使えるフレームワークは多い・・・ 1 0
  • 11.
    Why do youuse the DyNet? • DyNet が他のフレームワークよりも優れている 点 • Python と C++(高速化) • 動的なネットワークの操作(NLPにおいて重 要) • 自然言語処理のための使いやすいインター フェース 11
  • 12.
    Why do youuse the DyNet? • CPUでのパフォーマンス 12
  • 13.
    Why do youuse the DyNet? • GPUでのパフォーマンス もし他のパフォーマンスにも気になる人は論文を参 照ください。 https://arxiv.org/pdf/1701.03980v1.pdf 13
  • 14.
    Why do youuse the DyNet? • 動的な操作は自然言語処理において重要 • 言語の特徴 • 連続的、ツリー、グラフ 14
  • 15.
    Why do youuse the DyNet? • 下記のような状況の場合はDyNetがオススメで す。 • 自然言語処理 • 性能向上が必要 15
  • 16.
    Agenda • Self Introduction •Why do you use the DyNet ? • What is DyNet ? • How to apply the DyNet for natural language processing ? 1 6
  • 17.
    What is DyNet? • Computation Graph • Expressions( nodes in the graph) • Parameters • Model • a collection of parameters • Trainer 17
  • 18.
    What is DyNet? • Computation Graph • Expressions( nodes in the graph) • Parameters • Model • a collection of parameters • Trainer 18
  • 19.
    What is DyNet? • Case1 掛け算 1 9
  • 20.
    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
  • 21.
    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
  • 22.
    What is DyNet? • Computation Graph • Expressions( nodes in the graph) • Parameters • Model • a collection of parameters • Trainer 22
  • 23.
    What is DyNet? • Case2 xor計算 23
  • 24.
    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
  • 25.
    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
  • 26.
    What is DyNet? • Computation Graph • Expressions( nodes in the graph) • Parameters • Model • a collection of parameters • Trainer 26
  • 27.
    What is DyNet? 27 • Trainer • Trainer に先ほどのモデルを与えて初期化します。 SimpleSGDTrainer sgd(m); Code Cpp
  • 28.
    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
  • 29.
    Agenda • Self Introduction •Why do you use the DyNet ? • What is DyNet ? • How to apply the DyNet for natural language processing ? 29
  • 30.
    How to applythe DyNet for natural language processing ? 30 ipython notebook を用いて説明します。 https://github.com/SnowMasaya/DyNe t_Study_Docker/blob/master/dynet-py thon/RNNS%20DyNet.ipynb
  • 31.
    • 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