調べ物した結果

現役SEが仕事と直接関係ないことを調べた結果とか感想とか

gpt-2のソースを眺めてみる①(の前に)~彼がミサイルを発射すると爆発します~

今回のお相手

https://github.com/openai/gpt-2
お借りします。
GPT-2の詳しいことを知りたくてこのページを見に来た人すみません。回れ右です。

MIT ライセンスのようなので、ライセンスリンクも張っておきます。
https://github.com/openai/gpt-2/blob/master/LICENSE

すこしだけReadmeを読んでみる。

拝借


Code and models from the paper "Language Models are Unsupervised Multitask Learners".

You can read about GPT-2 and its staged release in our original blog post, 6 month follow-up post, and final post.

We have also released a dataset for researchers to study their behaviors.

Note that our original parameter counts were wrong due to an error (in our previous blog posts and paper). Thus you may have seen small referred to as 117M and medium referred to as 345M.

https://github.com/openai/gpt-2#gpt-2


翻訳かけてもびっくりするぐらいわからん。
なにかの論文?教師がどうのなので機械学習関連かもしらんが。ぜーんぜんわからん。
もうちょっと論文について詳細をググってみたが

https://paperswithcode.com/paper/language-models-are-unsupervised-multitask


やっぱりわからん。機械学習関連ってことぐらいだな。

https://cocon-corporation.com/cocontoco/gpt-2-language-model/


ここがすごく詳しかった・・・これやべーやつじゃん。
ここまで一切ソースさわってない。タイトルに偽りありすぎる。面白そうだから
やってみよう。
さきのサイトによると、Docker環境があればいーじに起動できる。
Windowsに入れる人は以下のURLを参考にどうか。MacというかUnixほしくなってきたなぁ。本当に。

https://ops.jig-saw.com/techblog/docker-for-windows-install/


とりあえずDocker buildをしていく。思いのほか時間がかかる。30分ぐらいは覚悟しておこう。
とりあえずゴー
docker run -it gpt-2:gpu python src/generate_unconditional_samples.py

f:id:couraeg:20191119223200p:plain


うーごかねんだけど。途中のエラーを見る限り、 pipのupgradeとパーミッションの問題のようす。
pip install --upgrade pip, そして読み取り専用になっていたフォルダを開放する。
もう一回buildだ。

ゴー
docker run -it gpt-2:gpu python src/generate_unconditional_samples.py
が。だめ。

もう少し真面目にエラーをよむとTensorFlowがなんちゃか。とある。もしや。

Dockerfile.gpuを見てみる。


仕組みはいまいち理解できていないが、おそらくDockerFileでTensorFlowが組み込まれていないと考えた。

FROM tensorflow/tensorflow:1.12.0-gpu-py3

# nvidia-docker 1.0
LABEL com.nvidia.volumes.needed="nvidia_driver"
LABEL com.nvidia.cuda.version="${CUDA_VERSION}"

# nvidia-container-runtime
ENV NVIDIA_VISIBLE_DEVICES=all \
    NVIDIA_DRIVER_CAPABILITIES=compute,utility \
    NVIDIA_REQUIRE_CUDA="cuda>=8.0" \
    LANG=C.UTF-8

RUN mkdir /gpt-2
WORKDIR /gpt-2
ADD . /gpt-2
RUN pip3 install -r requirements.txt
RUN python3 download_model.py 124M
RUN python3 download_model.py 355M
RUN python3 download_model.py 774M
RUN python3 download_model.py 1558M


うーん。先頭におもいっきり、TensorFlowってあるんだよな。
よーわからん。TensorFlowもRunにぶち込んだれ

FROM tensorflow/tensorflow:1.12.0-gpu-py3

# nvidia-docker 1.0
LABEL com.nvidia.volumes.needed="nvidia_driver"
LABEL com.nvidia.cuda.version="${CUDA_VERSION}"

# nvidia-container-runtime
ENV NVIDIA_VISIBLE_DEVICES=all \
    NVIDIA_DRIVER_CAPABILITIES=compute,utility \
    NVIDIA_REQUIRE_CUDA="cuda>=8.0" \
    LANG=C.UTF-8

RUN mkdir /gpt-2
WORKDIR /gpt-2
ADD . /gpt-2
RUN pip3 install -r requirements.txt
RUN pip3 install tensorflow
RUN pip3 install --upgrade tensorflow
RUN python3 download_model.py 124M
RUN python3 download_model.py 355M
RUN python3 download_model.py 774M
RUN python3 download_model.py 1558M


まつこと30分。
ゴーだ!(3度目)
docker run -it gpt-2:gpu python src/generate_unconditional_samples.py

f:id:couraeg:20191119233333p:plain


これは・・・いけてるんじゃないか。なんかゴリゴリ文章を生成している。
ひゅー。

interactive_conditional_samples.py で文章を生成。


大好きアイアンマンから冒頭の文章をAmazonPriveより拝借。
アフガニスタンで自社兵器のデモ実験に参加したトニー・スタークは、テロ組織に襲われ拉致されてしまう。
google翻訳する。
Tony Stark, who participated in a demonstration experiment with his own weapons in Afghanistan, is attacked and abducted by terrorist organizations.
いくぜ.


f:id:couraeg:20191119234529p:plain
いける。いけてるね。>>>で入力待ちになっているので、さっきの英文を張り付けてみてみる。
ずらずら英文ができているが、とりあえず先頭をPickup。

The fighter becomes airborne and kills several of them as they flee the attack and escapes and once again escapes and kills Iron Man. Marvel TV
ばれとるやないか・・・学習データはWebページから回収しているらしいから、そーゆーこともあるかなぁ。
日本語に直すと
「戦闘機は空襲になり、攻撃から逃げて逃げるときに一部を殺し、再び逃げてアイアンマンを殺します。マーベルTV」
となって、どうやら文法が少々おかしい模様。最新モデルの学習データをつかってもう一度実行してみる。
Docker Runを変更して引数を調整すればよさそうだけど、やり方がいまいちわからなかったので、
interactive_conditional_samples.pyをいじって再実行。

f:id:couraeg:20191119235616p:plain

同じくダラダラ文が出てくる。適当なところをPick。
It is written in the lyrics:

When a missile he shoots explodes (!), then sithly Sim boasted that the entire world was going to throw each other into their arms, and that we are going to fing each other's hair too

それは歌詞に書かれています:
彼がミサイルを発射すると爆発します(!)、それからシムは全世界がお互いの腕に投げ入れ、お互いの髪の毛を飛ばすことを自慢しました

わけがわかんねぇぜ。文法的にへんなところは減ったけど。わけがわからんぜ・・・
お遊びはこの辺にして、次こそソース触ろうかな。