torch.manual_seed(123456) - torch.cuda.manual_seed_all(123456)
PYTORCH DOCUMENTATION
https://pytorch.org/docs/master/index.html
import torch
torch.manual_seed(123456)
torch.cuda.manual_seed_all(123456)
1. torch.manual_seed(seed)
https://pytorch.org/docs/master/generated/torch.manual_seed.html
Sets the seed for generating random numbers. Returns a torch.Generator
object.
爲 CPU 設置種子用於生成隨機數,以使得結果是確定的。
Parameters
seed [int] - The desired seed.
2. torch.cuda.manual_seed(seed)
Sets the seed for generating random numbers for the current GPU. It’s safe to call this function if CUDA is not available; in that case, it is silently ignored.
爲當前 GPU 設置種子用於生成隨機數,以使得結果是確定的。
Parameters
seed [int] - The desired seed.
If you are working with a multi-GPU model, this function is insufficient to get determinism. To seed all GPUs, use manual_seed_all()
.
insufficient /ˌɪnsəˈfɪʃnt/:adj. 不足的,不能勝任的,缺乏能力的
3. torch.cuda.manual_seed_all(seed)
Sets the seed for generating random numbers on all GPUs. It’s safe to call this function if CUDA is not available; in that case, it is silently ignored.
爲所有的 GPU 設置種子用於生成隨機數,以使得結果是確定的。
Parameters
seed [int] - The desired seed.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# yongqiang cheng
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import torch
torch.manual_seed(123456)
torch.cuda.manual_seed_all(123456)
print(torch.rand([1, 5]))
print(torch.rand([1, 5]))
print(torch.rand([1, 5]))
print("9" * 16)
torch.manual_seed(123456)
torch.cuda.manual_seed_all(123456)
print(torch.rand([1, 5]))
print(torch.rand([1, 5]))
print(torch.rand([1, 5]))
/home/yongqiang/miniconda3/envs/pt-1.4_py-3.6/bin/python /home/yongqiang/pycharm_work/yongqiang.py
tensor([[0.5043, 0.8178, 0.4798, 0.9201, 0.6819]])
tensor([[0.6900, 0.6925, 0.3804, 0.4479, 0.4954]])
tensor([[0.0728, 0.9644, 0.5524, 0.0060, 0.1053]])
9999999999999999
tensor([[0.5043, 0.8178, 0.4798, 0.9201, 0.6819]])
tensor([[0.6900, 0.6925, 0.3804, 0.4479, 0.4954]])
tensor([[0.0728, 0.9644, 0.5524, 0.0060, 0.1053]])