0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PyTorch 乱数固定

Posted at

PyTorchで深層学習を行ったときの再現性を担保するために乱数固定を行うベストプラクティス.

import os
import random

seed = 0

def seed_everything(seed):
    random.seed(seed)
    os.environ['PYTHONHASHSEED'] = str(seed) # pythonのハッシュベース操作の再現性担保
    np.random.seed(seed)
    torch.manual_seed(seed) # ネットワーク重みの初期値を固定
    torch.cuda.manual_seed(seed) # ネットワーク重みの初期値を固定 (GPU)

    # cuDNN: NVIDIAのConvolution高速化ライブラリ.
    torch.backends.cudnn.deterministic = True # cuDNNによる最適化プロセス固定
    torch.backends.cudnn.benchmark = True # input_size固定CNNなら高速化が期待できる.iterationごとに変わるならFalseの方が速い.

seed_everything(seed=seed)
0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?