LoginSignup
2
0

More than 3 years have passed since last update.

BERTがクイズショルダータックルにチャレンジする

Posted at

はじめに

まずはこちらをご覧ください。
クイズショー
https://www.youtube.com/watch?v=m50H_0B78aQ&feature=emb_logo

動作環境

  • Google Colaboratory
  • Huggingface Transformers 2.5.1
  • Pytorch 1.5.0
  • wikipedia 1.4.0

検証内容

  • Wikipediaをコーパスとして学習したBERTが1000万相当の奴を獲得できるのかを検証する。
  • ただし下記の制約あり。
    • 画像、音声は文章化したうえで出題する。
    • 発言カードは使えない。

実装内容

  • ソースコードはほとんど下記サイトから引用。出題部を今回用に改変する。
  • 出題内容はこちら。
    1. 現在日本の首都は?
    2. アメリカの首都は?
    3. フランスの首都は?
    4. ドリンクを飲みますか?
    5. イギリスの首都は?
    6. ボツワナ共和国の首都は?
    7. オーストラリアの首都は?
    8. Noice or Roice?
  • 回答できない場合、BERTは黙ってしまう。

ソースコード(出題部のみ)

questions = [
    'Where is the capital of Japan?',
    'Where is the capital of the United States of America?',
    'Where is the capital of France?',
    'Would you like something to drink?',
    'Where is the capital of the United Kingdom?',
    'Where is the capital of Republic of Botswana?',
    'Where is the capital of Australia?',
    'Roise or Noise?'
]

reader = DocumentReader("deepset/bert-base-cased-squad2") 

# if you trained your own model using the training cell earlier, you can access it with this:
#reader = DocumentReader("./models/bert/bbu_squad2")


for question in questions:
    answer = ""
    print(f"Question: {question}")
    results = wiki.search(question)

    for result in results:
      try:
        #page = wiki.page(results[0])
        page = wiki.page(result)
        #print(f"Top wiki result: {page}")

        text = page.content

        reader.tokenize(question, text)
      except:
        print('PageError ocurred! Searching another page..')

      answer = reader.get_answer()
      if len(answer) >0:
        print(f"Answer: {answer}\n")
        break
      else:
        print("...")

実行結果

それでは早速クイズショルダータックルを始めてみましょう。

  • 【第1問】現在日本の首都は?
Question: Where is the capital of Japan?
...
Answer: Tokyo / Heian - kyō ( modern Kyoto ) / Tokyo / Tokyo / 

正解ですね。平安京も答えに出てくるとは思いませんでしたが。

  • 【第2問】アメリカの首都は?
Question: Where is the capital of the United States of America?
...
Answer: Washington , D . C . / The District of Columbia / 

正解です。

  • 【第3問】フランスの首都は?
Question: Where is the capital of France?
Answer: Saint - Denis / Paris / [CLS] Where is the capital of France ? [SEP]toile , while the working - class neighbourhood of the Faubourg Saint - Antoine on the eastern site of the city grew more and more crowded with poor migrant workers from other regions of France . Paris was the centre of an explosion of philosophic and scientific activity known as the Age of Enlightenment . Diderot and d ' Alembert published their Encyclopédie in 1751 , and the Montgolfier Brothers launched the first manned flight in a hot - air balloon on 21 November 1783 , from the gardens of the Château de la Muette . Paris was the financial capital of continental Europe / Paris / Paris / Paris ' 16th arrondissement / 

畳みかけるように回答されてしまいました。
サン=ドニと答えていますね。次点でParisを回答しているの△でしょうか。

  • 【第4問】ドリンクを飲みますか?
Question: Would you like something to drink?
Answer: juice or soda / 

はい、飲んでください。

  • 【第5問】イギリスの首都は?
Question: Where is the capital of the United Kingdom?
...
Answer: London / London / Edinburgh / 

正解です。

  • 【第6問】ボツワナ共和国の首都は?
Question: Where is the capital of Republic of Botswana?
Answer: Gaborone / Gaborone / 

難問ですが、正解です。

  • 【第7問】オーストラリアの首都は?
Question: Where is the capital of Australia?
Answer: Canberra / Canberra / Canberra / 

こちらも正解ですね。

  • 【第8問】Noice or Roice?
Question: Noice or Roice?
...

黙り込んでしまいました。
運命を変える質問への答えはWikipediaにはなかったのかもしれません。

まとめ

結局ポイントがどのくらい貯まったのか分からなかったので、1000万円の奴はまた次週にお預けとしましょう。

参考リンク

参考にさせていただきました。ありがとうございました。
Building a QA System with BERT on Wikipedia
チョコレートプラネットチャンネル

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0