第10章現代のリカレントニューラルネットワーク
10.7.8. Evaluation of Predicted Sequences10.7.8 予測した系列の評価
We can evaluate a predicted sequence by comparing it with the target sequence (the ground truth). But what precisely is the appropriate measure for comparing similarity between two sequences?
英語のヒント
予測した系列は、目標系列(正解)と比べることで評価できる。しかし、二つの系列の類似度を比べるための適切な尺度は、正確には何だろうか。
Bilingual Evaluation Understudy (BLEU), though originally proposed for evaluating machine translation results (Papineni et al., 2002), has been extensively used in measuring the quality of output sequences for different applications. In principle, for any -gram (Section 9.3.1.1) in the predicted sequence, BLEU evaluates whether this -gram appears in the target sequence.
英語のヒント
BLEU(Bilingual Evaluation Understudy)は、もともと機械翻訳の結果を評価するために提案されたが(Papineni et al., 2002)、さまざまな応用で出力系列の質を測るのに広く使われてきた。原理的には、予測した系列のどのグラム(9.3.1.1節)についても、BLEUはこのグラムが目標系列に現れるかどうかを評価する。
Denote by the precision of an -gram, defined as the ratio of the number of matched -grams in the predicted and target sequences to the number of -grams in the predicted sequence. To explain, given a target sequence , , , , , , and a predicted sequence , , , , , we have , , , and . Now let and be the numbers of tokens in the target sequence and the predicted sequence, respectively. Then, BLEU is defined as
英語のヒント
グラムの適合率をと表す。これは、予測した系列と目標系列で一致したグラムの数の、予測した系列のグラムの数に対する比として定義される。説明すると、目標系列、、、、、と、予測した系列、、、、が与えられると、、、、となる。ここで、とを、それぞれ目標系列と予測した系列のトークンの数とする。すると、BLEUは次のように定義される。
(10.7.4)
where is the longest -gram for matching.
英語のヒント
ここで、は一致を調べる最長のグラムである。
Based on the definition of BLEU in (10.7.4), whenever the predicted sequence is the same as the target sequence, BLEU is 1. Moreover, since matching longer -grams is more difficult, BLEU assigns a greater weight when a longer -gram has high precision. Specifically, when is fixed, increases as grows (the original paper uses ). Furthermore, since predicting shorter sequences tends to yield a higher value, the coefficient before the multiplication term in (10.7.4) penalizes shorter predicted sequences. For example, when , given the target sequence , , , , , and the predicted sequence , , although , the penalty factor lowers the BLEU.
英語のヒント
We implement the BLEU measure as follows.
英語のヒント
BLEUの尺度を次のように実装する。
def bleu(pred_seq, label_seq, k): #@save
"""Compute the BLEU."""
pred_tokens, label_tokens = pred_seq.split(' '), label_seq.split(' ')
len_pred, len_label = len(pred_tokens), len(label_tokens)
score = math.exp(min(0, 1 - len_label / len_pred))
for n in range(1, min(k, len_pred) + 1):
num_matches, label_subs = 0, collections.defaultdict(int)
for i in range(len_label - n + 1):
label_subs[' '.join(label_tokens[i: i + n])] += 1
for i in range(len_pred - n + 1):
if label_subs[' '.join(pred_tokens[i: i + n])] > 0:
num_matches += 1
label_subs[' '.join(pred_tokens[i: i + n])] -= 1
score *= math.pow(num_matches / (len_pred - n + 1), math.pow(0.5, n))
return scoreIn the end, we use the trained RNN encoder–decoder to translate a few English sentences into French and compute the BLEU of the results.
英語のヒント
最後に、訓練したRNNのエンコーダ・デコーダを使って、いくつかの英語の文をフランス語に翻訳し、結果のBLEUを計算する。
engs = ['go .', 'i lost .', 'he\'s calm .', 'i\'m home .']
fras = ['va !', 'j\'ai perdu .', 'il est calme .', 'je suis chez moi .']
preds, _ = model.predict_step(
data.build(engs, fras), d2l.try_gpu(), data.num_steps)
for en, fr, p in zip(engs, fras, preds):
translation = []
for token in data.tgt_vocab.to_tokens(p):
if token == '<eos>':
break
translation.append(token)
print(f'{en} => {translation}, bleu,'
f'{bleu(" ".join(translation), fr, k=2):.3f}')go . => ['va', '!'], bleu,1.000
i lost . => ["j'ai", 'perdu', '.'], bleu,1.000
he's calm . => ['elle', 'court', '.'], bleu,0.000
i'm home . => ['je', 'suis', 'chez', 'moi', '.'], bleu,1.000