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 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