見たことのあるテストで満点
新しいモデルが、有名な数学の問題集で95%を取りました。1週間後、その問題が答えつきで、モデルが読んだかもしれないウェブページに載っているのが見つかりました。
考えて解いたのか、覚えていたのか。評価チームがするように、テストの問題を訓練の文章と比べてみます。
このレッスンのゴール
メモ
n-gram の重なりで、ベンチマークの汚染を確かめることができる。
キーワード
| ことば | いみ | れい |
|---|---|---|
| ベンチマーク | モデルを比べるための、答えつきの決まった問題の集まり | |
| 飽和 | 上位のモデルがみな満点近くを取る状態 | |
| 汚染 | テストの問題が訓練データに入っていること | |
| n-gram | 続けて並んだ n 個の単語 | 正午 に 出る |
| 重なり | 問題の n-gram のうち訓練の文章にあるものの割合 | |
| 非公開セット | 公開せずにしまっておく問題 |
学生が入力するセルは、このプレビューでは空白です。
ウォームアップ
やってみよう
ウォームアップ
4問同じ条件でなければ比べられない
ベンチマークは、どのモデルにも同じ問題を出し、正解を数えます。2つの点数を比べてよいのは、指示の出し方、試す回数、採点の仕方が同じときだけです。
問題が少ないと誤差が大きく、50問のうち1問は2点にあたります。上位のモデルがみな満点近くになると、その問題集は飽和し、もうモデルを見分けられません。
例: 点数の差の意味
| 問題数 | A の正解 | B の正解 | 差 |
|---|---|---|---|
| 50 | 45 | 44 | 2点、1問分 |
| 1,000 | 900 | 880 | 2点、20問分 |
| 500、ほぼ飽和 | 496 | 495 | 小さすぎて意味は薄い |
学生が入力するセルは、このプレビューでは空白です。
確認
やってみよう
確認
3問重なりで読んだことのある問題を見つける
テストの問題がコーパスに入っていたなら、高い点数は力ではなく記憶を測っているかもしれません。これが汚染です。
よくある確かめ方は、問題を単語の n-gram に分け、1つずつ訓練の文章から探すことです。見つかった割合が重なりです。重なりが大きい問題には印を付け、その点数を分けて報告します。
試そう: n-gram と重なり
def ngrams(text, n):
words = text.split()
grams = []
for k in range(len(words) - n + 1):
grams.append(" ".join(words[k:k + n]))
return grams
# 日本語は空白で区切った単語を使う。
training = "公園 行き の バス は 正午 に 出る 毎日"
question = "海 行き の バス は 正午 に 出る"
seen = ngrams(training, 3)
mine = ngrams(question, 3)
shared = 0
for g in mine:
if g in seen:
shared = shared + 1
print("見た:", g)
print("割合", round(shared / len(mine), 2))
# 2か所の 3 を 5 に変えてみましょう。割合はいくつになりますか。
汚染のスクリプトを書き込む
docker exec jupyter sh -c "printf '%s\n' 'corpus = \"\"\"a train leaves the city at eight and reaches the coast at ten so the journey takes two hours' 'quiz night question seven a shop sells pens at 40 pence each how much do five pens cost the answer is 2 pounds' 'the canteen sells soup for 2 pounds and bread for 50 pence' 'quiz night question nine if a bus leaves at nine and the trip takes 45 minutes when does it arrive the answer is 9 45' 'the library opens at nine and shuts at six on weekdays\"\"\"' 'questions = [' ' \"a shop sells pens at 40 pence each how much do five pens cost\",' ' \"a shop sells rulers at 30 pence each how much do four rulers cost\",' ' \"if a bus leaves at nine and the trip takes 45 minutes when does it arrive\",' ' \"if the bus leaves at nine and the journey takes 45 minutes what time does it get there\",' > steps/contam.py"ここでは表示のみで、実行されません。「ラボを開始」を押すと、自分のラボのターミナルに入力できます。
スクリプトの残りを書き込む
docker exec jupyter sh -c "printf '%s\n' ' \"a train leaves at eight and the journey takes two hours when does it arrive\",' ' \"the gym opens at seven and shuts at ten how many hours is it open\",' ']' 'right = [1, 0, 1, 1, 0, 1]' 'N = 5' 'def grams(text):' ' w = text.split()' ' return [\" \".join(w[k:k + N]) for k in range(len(w) - N + 1)]' 'seen = set(grams(corpus.replace(chr(10), \" \")))' 'flags = []' 'for k, q in enumerate(questions):' ' g = grams(q)' ' share = sum(1 for x in g if x in seen) / len(g)' ' flags.append(share >= 0.5)' ' print(\"question\", k + 1, \"share seen\", round(share, 2), \"FLAGGED\" if flags[-1] else \"\")' 'clean = [r for r, f in zip(right, flags) if not f]' 'print(\"score on all\", sum(right), \"of\", len(right), \"score on clean\", sum(clean), \"of\", len(clean))' >> steps/contam.py"ここでは表示のみで、実行されません。「ラボを開始」を押すと、自分のラボのターミナルに入力できます。
6問を訓練の文章と比べる
docker exec jupyter python3 steps/contam.pyここでは表示のみで、実行されません。「ラボを開始」を押すと、自分のラボのターミナルに入力できます。
contam.py の表示 (5-gram)
| 問題 | 見た割合 | 印 | モデルの正誤 |
|---|---|---|---|
| 1 ペン1本40ペンス | 1.0 | あり | 正 |
| 2 定規1本30ペンス | 0.1 | なし | 誤 |
| 3 9時発のバス、45分 | 1.0 | あり | 正 |
| 4 3と同じ、言い換え | 0.14 | なし | 正 |
| 5 8時発の電車 | 0.09 | なし | 誤 |
| 6 ジムの営業時間 | 0.0 | なし | 正 |
学生が入力するセルは、このプレビューでは空白です。
確認
やってみよう
確認
3問よくある間違い
注意
重なりが小さければその問題は見られていない、とよく誤解されます。問題4は問題3の言い換えで、見た割合は0.14でした。n-gram が見つけるのはコピーで、言い換えではありません。汚染がないという結果は下限と考えましょう。
ベンチマークを正直に保つ
チームは、印の付いた問題と付いていない問題の点数を分けて報告します。非公開セットをしまっておき、公開されて読まれることを防ぎます。訓練データを集めた後に、新しい問題を書くこともします。
どれも完璧ではありません。点数は、モジュール85と同じように、重みをはかって使う証拠です。
確認
やってみよう
確認
3問課題: 汚染を確かめる
レッスンの汚染チェックを作りましょう。課題のデータはラボと同じ英語の文です。
ngrams(text, n) は、続けて並んだ n 個の単語を空白でつないで、すべて並べます。overlap(question, corpus, n) は、問題の n-gram のうちコーパスにあるものの割合です。flagged(questions, corpus, n, threshold) は、しきい値以上の問題の位置を返します。clean_score(results, flags) は、印のない問題のうち正解の割合です。
def ngrams(text, n):
# Every run of n words in a row, each joined with one space.
return []
def overlap(question, corpus, n):
# The share of the n-grams of the question that also appear in the corpus.
# 0 if the question has no n-grams.
return 0
def flagged(questions, corpus, n, threshold):
# The positions of the questions whose overlap is at least the threshold.
return []
def clean_score(results, flags):
# results holds 1 or 0 per question; flags is a list of positions.
# The share of 1s among the questions not flagged, or 0 if none are left.
return 0
終わりのクイズ
やってみよう