プリンキピア

第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 n-gram (Section 9.3.1.1) in the predicted sequence, BLEU evaluates whether this n-gram appears in the target sequence.
英語のヒント
BLEU(Bilingual Evaluation Understudy)は、もともと機械翻訳の結果を評価するために提案されたが(Papineni et al., 2002)、さまざまな応用で出力系列の質を測るのに広く使われてきた。原理的には、予測した系列のどのnグラム(9.3.1.1節)についても、BLEUはこのnグラムが目標系列に現れるかどうかを評価する。
Denote by pn the precision of an n-gram, defined as the ratio of the number of matched n-grams in the predicted and target sequences to the number of n-grams in the predicted sequence. To explain, given a target sequence A, B, C, D, E, F, and a predicted sequence A, B, B, C, D, we have p1=4/5, p2=3/4, p3=1/3, and p4=0. Now let lenlabel and lenpred be the numbers of tokens in the target sequence and the predicted sequence, respectively. Then, BLEU is defined as
英語のヒント
nグラムの適合率をpnと表す。これは、予測した系列と目標系列で一致したnグラムの数の、予測した系列のnグラムの数に対する比として定義される。説明すると、目標系列A、B、C、D、E、Fと、予測した系列A、B、B、C、Dが与えられると、p1=4/5、p2=3/4、p3=1/3、p4=0となる。ここで、lenlabelとlenpredを、それぞれ目標系列と予測した系列のトークンの数とする。すると、BLEUは次のように定義される。
exp⁡(min(0,1−lenlabellenpred))∏n=1kpn1/2n,(10.7.4)
where k is the longest n-gram for matching.
英語のヒント
ここで、kは一致を調べる最長のnグラムである。
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 n-grams is more difficult, BLEU assigns a greater weight when a longer n-gram has high precision. Specifically, when pn is fixed, pn1/2n increases as n grows (the original paper uses pn1/n). Furthermore, since predicting shorter sequences tends to yield a higher pn value, the coefficient before the multiplication term in (10.7.4) penalizes shorter predicted sequences. For example, when k=2, given the target sequence A, B, C, D, E, F and the predicted sequence A, B, although p1=p2=1, the penalty factor exp⁡(1−6/2)≈0.14 lowers the BLEU.
英語のヒント
(10.7.4)のBLEUの定義にもとづけば、予測した系列が目標系列と同じときはいつも、BLEUは1である。さらに、長いnグラムの一致はより難しいので、BLEUは、長いnグラムの適合率が高いときに大きな重みを割り当てる。具体的には、pnを固定すると、nが大きくなるにつれてpn1/2nは大きくなる(元の論文はpn1/nを使っている)。さらに、短い系列を予測するとpnの値が高くなる傾向があるので、(10.7.4)の積の項の前の係数が、短い予測系列にペナルティを与える。たとえば、k=2のとき、目標系列A、B、C、D、E、Fと予測した系列A、Bが与えられると、p1=p2=1であるにもかかわらず、ペナルティの係数exp⁡(1−6/2)≈0.14が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 score
In 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