快捷方式

torch.randint

torch.randint(low=0, high, size, *, generator=None, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) Tensor

回傳一個張量,其數值為介於 low (包含) 與 high (不包含) 之間均勻隨機生成的整數。

張量的形狀由變數參數 size 定義。

注意

使用全域預設的 dtype (torch.float32),此函數會回傳一個 dtype 為 torch.int64 的張量。

參數
  • low (int, optional) – 從分佈中提取的最小整數。預設值:0。

  • high (int) – 從分佈中提取的最大整數之上的一個整數。

  • size (tuple) – 定義輸出張量形狀的元組。

關鍵字參數
  • generator (torch.Generator, optional) – 用於採樣的偽隨機數生成器

  • out (Tensor, optional) – 輸出張量。

  • dtype (torch.dtype, optional) – 如果為 None,則此函數回傳一個 dtype 為 torch.int64 的張量。

  • layout (torch.layout, optional) – 回傳張量所需的 layout。預設值:torch.strided

  • device (torch.device, optional) – 回傳張量所需的裝置。預設值:如果為 None,則使用預設張量類型的當前裝置(請參閱 torch.set_default_device())。對於 CPU 張量類型,device 將為 CPU;對於 CUDA 張量類型,則為當前的 CUDA 裝置。

  • requires_grad (bool, optional) – 如果 autograd 應記錄回傳張量的運算。預設值:False

範例

>>> torch.randint(3, 5, (3,))
tensor([4, 3, 4])


>>> torch.randint(10, (2, 2))
tensor([[0, 2],
        [5, 5]])


>>> torch.randint(3, 10, (2, 2))
tensor([[4, 5],
        [6, 7]])

文件

取得 PyTorch 的完整開發者文件

檢視文件

教學課程

取得針對初學者和進階開發者的深入教學課程

檢視教學課程

資源

尋找開發資源並獲得您問題的解答

檢視資源