Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use the torch.manual_seed () method in pytorch

Shulou Source: shulou.com Published: 2022-06-02 09:54:28 10月03日 Update

Pytorch in the torch.manual_seed () method how to use, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Description

Set CPU to generate the seeds of random numbers to facilitate the reproduction of experimental results next time.

Grammar

Torch.manual_seed (seed) → torch._C.Generator

Parameters.

Seed (int)-CPU generates the seeds of random numbers. The value range is [- 0x8000000000000000, 0xffffffffffffffff], and the decimal system is [- 9223372036854775808, 18446744073709551615]. Beyond this range, RuntimeError will trigger an error.

Return

Returns a torch.Generator object.

Example sets random seed # test.pyimport torchtorch.manual_seed (0) print (torch.rand (1)) # returns a tensor containing a set of random numbers extracted from the uniform distribution of the interval [0,1)

The output is the same each time you run test.py:

Tensor ([0.4963]) has no random seed # test.pyimport torchprint (torch.rand (1)) # returns a tensor containing a set of random numbers extracted from the uniform distribution of the interval [0,1)

The output is different each time you run test.py:

Tensor ([0.2079])-- tensor ([0.6536])-- tensor ([0.2735]) Note

After setting the random seed, the output is the same every time you run the test.py file, not every time the random function generates the same result:

# test.pyimport torchtorch.manual_seed (0) print (torch.rand (1)) print (torch.rand (1))

Output:

Tensor ([0.4963]) tensor ([0.7682])

You can see that printing the torch.rand (1) function twice produces different results, but if you run test.py again, it will still print:

Tensor ([0.4963]) tensor ([0.7682])

However, if you just want the results to be the same every time you run a random function, you can set exactly the same random seed in front of each random function:

# test.pyimport torchtorch.manual_seed (0) print (torch.rand (1)) torch.manual_seed (0) print (torch.rand (1))

Output:

Tensor ([0.4963]) tensor ([0.4963]) is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Result seed generation output run function random number interval tensor range help extraction method clear same identical content decimal parameter for this Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Linux Shulou Technology macOS Xiaomi