Chapter7

Chapter7-3 Transformerを用いたquestion-answeringプログラム

DockerとECSを駆使した機械学習アプリケーションを実装して実行しよう

なお今回は学習済みのモデルを用いるので以下のコマンドでDocker imageをローカルにダウンロードしよう

$ docker pull tomomano/qabot:latest

ダウンロードしたら
文章と質問の2つを入力していきます

$ context="Albert Einstein (14 March 1879 - 18 April 1955) was a German-born theoretical physicist
who developed the theory of relativity, one of the two pillars of modern physics (alongside quantum mechanics).
His work is also known for its influence on the philosophy of science. He is best known to the general public for his mass-energy
equivalence formula E = mc2, which has been dubbed the world's most famous equation.
He received the 1921 Nobel Prize in Physics for his services to theoretical physics, and especially for his discovery of the law of the photoelectric
effect, a pivotal step in the development of quantum theory."
$ question="In what year did Einstein win the Nobel prize ?"

入力したら次のコマンドでコンテナを実行します

$ docker run tomomano/qabot "${context}" "${question}" foo --no_save

実行結果は

”score”は正解の自信度を表す数字で[0~1]の範囲で表示される
"start"と”end”は、context中の何文字目が正解に相当うするかを示しており
"answer"が正解と予想された文字列

つまり画像から読み取るに、正解の自信は限りなく高く、answerも1921とちゃんと正解を当てている

 

今度はもう少し難しい質問を投げてみる
新しい質問を入力して

$ question="Why did Einstein win the Nobel prize ?"

実行

$ docker run tomomano/qabot "${context}" "${question}" foo --no_save

で実行

自信度は0.5784725546836853
先ほどと比べて自信度が下がりましたがanswerを見てみると正解しています

7-5 スタックのデプロイ

以下のコマンドを順番に実行してスタックをデプロイしていこう

# プロジェクトのディレクトリに移動
$ cd handson/qa-bot

# venvを作成し、依存ライブラリのインストールを行う
$python3 -m venv .env
$source .env/bin/activate
$pip install -r requirements.txt

# デプロイを実行
$ cdk deploy

デプロイできるとAWSのECSコンソール上でクラスター定義された

7-6 タスクの実行

先程デプロイで作成したクラスターに、次のコマンドで質問を投入することができる

$ python run_task.py ask "A giant peach was flowing in the river. She picked it up and brought it home. Later, a healthy baby was born from the peach. She named the baby Momotaro." "What is the name of the baby?"

これを実行したら結果、出力結果は「Answer:Momotaro」と出て正しい答えが出力された

7-7 タスクの同時実行

$ python run_task.py ask_many

このコマンドを実行したあとにECSコンソールに行き、タスクの一覧を見てみたら合計9個のインスタンスが起動された

そして

$ python run_task.py list_answers

実行

そうしたら

合計9個分の質問の答えが出た

7-8 スタックの削除

最後に使い終わったスタックを以下のコマンドで削除

$ cdk destroy