pytorch 之 manual_seed

 pytorch  之 manual_seed   的源程序如下,作用:产生固定的随机数

def manual_seed(seed):
    r"""Sets the seed for generating random numbers. Returns a
    `torch.Generator` object.

    Args:
        seed (int): The desired seed.
    """
    seed = int(seed)
    import torch.cuda

    if not torch.cuda._in_bad_fork:
        torch.cuda.manual_seed_all(seed)

    return default_generator.manual_seed(seed)

例子:

import torch
print(torch.rand(2))
torch.manual_seed(2)

print(torch.rand(2))

运行多次后就知道结果

发布了234 篇原创文章 · 获赞 61 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/weixin_42528089/article/details/103840754