評價此頁

torch.random#

創建於: 2019 年 8 月 7 日 | 最後更新於: 2025 年 6 月 18 日

torch.random.fork_rng(devices=None, enabled=True, _caller='fork_rng', _devices_kw='devices', device_type='cuda')[原始碼]#

Forked RNG,這樣當你返回時,RNG 會重置為之前的狀態。

引數
  • devices (可迭代的裝置 ID) – for which to fork the RNG. CPU RNG state is always forked. By default, fork_rng() operates on all devices, but will emit a warning if your machine has a lot of devices, since this function will run very slowly in that case. If you explicitly specify devices, this warning will be suppressed

  • enabled (bool) – if False, the RNG is not forked. This is a convenience argument for easily disabling the context manager without having to delete it and unindent your Python code under it.

  • device_type (str) – device type str, default is cuda. As for supported device, see details in accelerator

返回型別

生成器

torch.random.get_rng_state()[原始碼]#

將隨機數生成器狀態作為torch.ByteTensor返回。

注意

返回的狀態僅用於 CPU 上的預設生成器。

另請參閱: torch.random.fork_rng()

返回型別

張量

torch.random.initial_seed()[原始碼]#

返回生成隨機數的初始種子,作為Python long型別。

注意

返回的種子僅用於 CPU 上的預設生成器。

返回型別

int

torch.random.manual_seed(seed)[原始碼]#

設定在所有裝置上生成隨機數的種子。返回一個 torch.Generator 物件。

引數

seed (int) – The desired seed. Value must be within the inclusive range [-0x8000_0000_0000_0000, 0xffff_ffff_ffff_ffff]. Otherwise, a RuntimeError is raised. Negative inputs are remapped to positive values with the formula 0xffff_ffff_ffff_ffff + seed.

返回型別

生成器

torch.random.seed()[原始碼]#

將用於在所有裝置上生成隨機數的種子設定為非確定性隨機數。返回一個用於播種 RNG 的 64 位數字。

返回型別

int

torch.random.set_rng_state(new_state)[原始碼]#

設定隨機數生成器狀態。

注意

此函式僅適用於 CPU。對於 CUDA,請使用 torch.manual_seed(),它適用於 CPU 和 CUDA。

引數

new_state (torch.ByteTensor) – 所需的狀態