LoginSignup
20
21

More than 5 years have passed since last update.

pytorchでCNNのlossが毎回変わる問題の対処法 (on gpu)

Last updated at Posted at 2018-08-14

はじめに

pytorchでCNNのlossが毎回変わる問題の対処法 (on cpu)の続きでgpu versionです。

pytorchのライブラリ以外は他の方が既に記事にされてます。
ChainerでGPUを使うと毎回結果が変わる理由と対策
TensorFlowでGPUを使うと毎回結果が変わる理由と対策

検証環境

ubuntu 16.04
python 2.7.15
pytorch 0.4.0
torchvision 0.2.1
cuda 8.0
cudnn 5.1

検証

cpu実行で同じ結果になるようになったプログラムで比較(長いので省略)
https://github.com/chatflip/qiita_code/blob/master/deterministic/deterministic_cpu.py

terminal
~/qiita_code/deterministic$ python deterministic_cpu.py
deterministic_cpu.py
                                    1回目           2回目            3回目
Train Epoch: 5 [0/60000 (0%)]   Loss: 0.143671  Loss: 0.068791  Loss: 0.094007
Train Epoch: 5 [64/60000 (0%)]  Loss: 0.118837  Loss: 0.269800  Loss: 0.131437
Train Epoch: 5 [128/60000 (0%)] Loss: 0.176576  Loss: 0.265202  Loss: 0.206423
Train Epoch: 5 [192/60000 (0%)] Loss: 0.420969  Loss: 0.569533  Loss: 0.391462

Epoch1の始めの方はlossの値が同じですが、Epoch2以降から違う値になりました。原因はcudnnによる最適化なんですが、プログラムが辿れないの詳しくは分からないです…
ただtorch.backends.cudnn.deterministic = Trueとすると決定論的振る舞いをするらしいです。

torch.backends.cudnn
https://github.com/pytorch/pytorch/blob/v0.4.0/torch/backends/cudnn/__init__.py
cudnn.deterministic関連issue
https://github.com/pytorch/pytorch/issues/6351

再検証

torch.backends.cudnn.deterministic = Trueを加えてプログラムを実行
https://github.com/chatflip/qiita_code/blob/master/deterministic/deterministic_gpu.py

terminal
~/qiita_code/deterministic$ python deterministic_gpu.py
deterministic_gpu.py
                                    1回目           2回目            3回目
Train Epoch: 5 [0/60000 (0%)]   Loss: 0.062156  Loss: 0.062156  Loss: 0.062156
Train Epoch: 5 [64/60000 (0%)]  Loss: 0.133375  Loss: 0.133375  Loss: 0.133375
Train Epoch: 5 [128/60000 (0%)] Loss: 0.212103  Loss: 0.212103  Loss: 0.212103
Train Epoch: 5 [192/60000 (0%)] Loss: 0.430716  Loss: 0.430716  Loss: 0.430716

lossが同じ値になりました

おわりに

pytorch on gpuでlossが毎回変わる問題の対処法は

  • torch.backends.cudnn.deterministic = True

cpu部分の対処法はpytorchでCNNのlossが毎回変わる問題の対処法 (on cpu)

20
21
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
20
21