LoginSignup
5
6

More than 5 years have passed since last update.

ColaboratoryでChainerのサンプルを実行する

Posted at

準備

  1. [ファイル]-[Python3のノートブックを作成]
  2. [ランタイム]-[ランタイムのタイプを変更]
    ハードウェアアクセラレータを GPU にする。

インストールまで

行頭に ! を付ければ、だいたいのことはできる。

!apt-get install -y -qq libcusparse8.0 libnvrtc8.0 libnvtoolsext1
!ln -snf /usr/lib/x86_64-linux-gnu/libnvrtc-builtins.so.8.0 /usr/lib/x86_64-linux-gnu/libnvrtc-builtins.so
!pip install cupy-cuda80==4.0.0
!pip install chainer==4.0.0

サンプルを用意する

cdは特別で、 ! ではなく % を付ければOK。

!git clone https://github.com/chainer/chainer.git

%cd chainer
!git checkout -b v4.0.0 refs/tags/v4.0.0

サンプルを実行する

!python examples/mnist/train_mnist.py -g 0

もちろんログも出る。出力結果はこんな感じ。

/usr/local/lib/python3.6/dist-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
GPU: 0
# unit: 1000
# Minibatch-size: 100
# epoch: 20

Downloading from http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz...
Downloading from http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz...
Downloading from http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz...
Downloading from http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz...
epoch       main/loss   validation/main/loss  main/accuracy  validation/main/accuracy  elapsed_time
(略)
1           0.191875    0.0962698             0.941733       0.9684                    14.8566       
(略)
2           0.0749486   0.0723905             0.976982       0.9768                    20.2693       
(略)
20          0.00698326  0.100026              0.998082       0.9828                    118.724       
     total [##################################################] 100.00%
this epoch [..................................................]  0.00%
     12000 iter, 20 epoch / 20 epochs
    109.34 iters/sec. Estimated time to finish: 0:00:00.

実行時間は、初回のテストデータダウンロードを含めてもおよそ230秒だった。
2回目以降は、ログの最後に出てる時間とほぼ一致(110秒くらい)。

Macbook Proが発火しないか不安になりながら延々と待っていた自分がアホだった。
ここまでくれば、後はgit cloneを自分のリポジトリにして、実行するファイルを自分のものにすればいいだけ。

5
6
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
5
6