Skip to content

Commit

Permalink
#52: 优化随机数设置
Browse files Browse the repository at this point in the history
  • Loading branch information
cjopengler committed Nov 8, 2021
1 parent 90bbda9 commit 4f4e484
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions easytext/utils/seed_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,17 @@ def set_seed(seed: int = 7) -> None:
:param seed: 随机数种子
:return: None
"""

# 随机数种子设定
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)

# CUDA中的一些运算,如对sparse的CUDA张量与dense的CUDA张量调用torch.bmm(),它通常使用不确定性算法。
# 为了避免这种情况,就要将这个flag设置为True,让它使用确定的实现。
torch.backends.cudnn.deterministic = True

# 设置这个flag可以让内置的cuDNN的auto-tuner自动寻找最适合当前配置的高效算法,来达到优化运行效率的问题。
# 但是由于噪声和不同的硬件条件,即使是同一台机器,benchmark都可能会选择不同的算法。为了消除这个随机性,设置为 False
torch.backends.cudnn.benchmark = False

0 comments on commit 4f4e484

Please sign in to comment.