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的張量。- 引數
- 關鍵字引數
generator (
torch.Generator, optional) – 用於取樣的偽隨機數生成器out (Tensor, optional) – 輸出張量。
dtype (torch.dtype, optional) – 返回張量的期望資料型別。預設值:如果
None,則此函式返回一個 dtype 為torch.int64的張量。layout (
torch.layout, 可選) – 返回張量的所需佈局。預設:torch.strided。device (
torch.device, 可選) – 返回張量的所需裝置。預設:如果為None,則使用當前裝置作為預設張量型別(參見torch.set_default_device())。對於 CPU 張量型別,device將是 CPU;對於 CUDA 張量型別,device將是當前的 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]])