pytorch 之 manual_seed

 The source of manual_seed pytorch follows, the role of: generating a fixed random number

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)

example:

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

print(torch.rand(2))

Run multiple times after to know the results

Published 234 original articles · won praise 61 · views 120 000 +

Guess you like

Origin blog.csdn.net/weixin_42528089/article/details/103840754