Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report