1 torch.rand:構造均勻分布張量
torch.rand
是用于生成均勻隨機分布張量的函數,從區間[0,1)
的均勻分布中隨機抽取一個隨機數生成一個張量,其調用方法如下所示:
torch.rand(sizes, out=None) ?? Tensor
參數:
-
sizes
:用于定義輸出張量的形狀
示例代碼:
import torch# 生成一個每個元素服從0-1均勻分布的4行3列隨機張量
random_tensor = torch.rand(4, 3)
print('tensor:', random_tensor)
print('type:', random_tensor.type())
print('shape:', random_tensor.shape)
運行代碼顯示:
tensor: tensor([[0.4349, 0.8567, 0.7321],[0.4057, 0.0222, 0.3444],[0.9679, 0.0980, 0.8152],[0.1998, 0.7888, 0.5478]])
type: torch.FloatTensor
shape: torch.Size([4, 3])
2 torch.randn:構造標準正態分布張量
torch.randn()
是用于生成正態隨機分布張量的函數,從標準正態分布中隨機抽取一個隨機數生成一個張量,其調用方法如下所示:
torch.randn(sizes, out=None) ?? Tensor
參數:
-
sizes
:用于定義輸出張量的形狀
示例代碼:
import torch# 生成一個每個元素均為標準正態分布的4行3列隨機張量
random_tensor = torch.randn(4, 3)
print('tensor:', random_tensor)
print('type:', random_tensor.type())
print('shape:', random_tensor.shape)
運行代碼顯示:
tensor: tensor([[ 0.7776, 0.6305, 0.1961],[ 0.1831, -0.4187, 0.1245],[ 0.3092, -1.0463, -0.6656],[-1.0098, 1.3861, -0.2600]])
type: torch.FloatTensor
shape: torch.Size([4, 3])
3?torch.randn_like:構造與輸入形狀相同正態分布張量
torch.randn_like()用于生成一個與輸入張量大小相同的張量,其中填充了均值為 0 方差為 1 的正態分布的隨機值,其調用方法如下所示:
torch.randn_like(input_tensor, dtype=None, layout=None, device=None, requires_grad=False) ?? Tensor
參數:
-
input_tensor(必需)- 其大小將用于生成輸出張量的輸入張量。
-
dtype(可選)- 輸出張量所需的數據類型。默認為None,這意味著將使用輸入張量的數據類型。
-
layout(可選)- 輸出張量所需的內存布局。默認為None,這意味著將使用輸入張量的內存布局。
-
device(可選)- 輸出張量所需的設備。默認為None,這意味著將使用輸入張量的設備。
-
requires_grad(可選)- 輸出張量是否應該在反向傳播期間計算其梯度。默認為False。
示例代碼:
import torch# 生成一個每個元素均為標準正態分布的4行3列隨機張量
tensor_x = torch.randn(4, 3)
tensor_y = torch.randn_like(tensor_x)print('tensor_x:', tensor_x)
print('type:', tensor_x.type())
print('shape:', tensor_x.shape)print('tensor_y:', tensor_y)
print('type:', tensor_y.type())
print('shape:', tensor_y.shape)
運行代碼顯示:
tensor_x: tensor([[ 5.5292e-01, 6.5111e-01, -6.0329e-04],[ 1.0402e+00, -7.4630e-01, 7.5701e-01],[ 8.8160e-02, -1.2581e+00, -1.8089e-01],[-4.2769e-01, -8.5043e-01, -5.8388e-01]])
type: torch.FloatTensor
shape: torch.Size([4, 3])
tensor_y: tensor([[ 0.2308, 0.3297, -0.6633],[ 1.7389, 0.6372, -1.1069],[-0.2415, -0.8585, 0.3343],[-1.2581, -0.5001, 0.0317]])
type: torch.FloatTensor
shape: torch.Size([4, 3])
4 torch.randint:構造區間分布張量
torch.randint()
是用于生成任意區間分布張量的函數,從標準正態分布中隨機抽取一個隨機數生成一個張量,其調用方法如下所示:
torch.randint(low=0, high, sizes, out=None) ?? Tensor
參數:
-
low
~high
:隨機數的區間范圍 -
sizes
:用于定義輸出張量的形狀
示例代碼:
import torch# 生成一個每個元素均為[1-10]均勻分布的4行3列隨機張量
tensor_int = torch.randint(1, 10, (4, 3))
print('tensor_int:', tensor_int)
print('type:', tensor_int.type())
print('shape:', tensor_int.shape)
運行代碼顯示:
tensor_int: tensor([[1, 7, 1],[3, 8, 7],[5, 2, 1],[5, 3, 6]])
type: torch.LongTensor
shape: torch.Size([4, 3])
5?torch.randperm:根據生成的隨機序號對張量進行隨機排序
torch.randint()
是用于對張量序號進行隨機排序的函數,根據生成的隨機序列進行隨機排序,其調用格式如下所示:
torch.randperm(n, out=None, dtype=torch.int64) ?? LongTensor
參數:
-
n
:一個整數,可以理解為張量某個方向的維度 -
dtype
:返回的數據類型(torch.int64
)
示例代碼:
import torch# 生成一個0~3的隨機整數排序
idx = torch.randperm(4)# 生成一個4行3列的張量
tensor_4 = torch.Tensor(4, 3)# 為了方便對比,首先輸出tensor_4的結果
print("原始張量\n", tensor_4)# 下面輸出隨機生成的行序號
print("\n生成的隨機序號\n", idx)# 下面的指令實現了在行的方向上,對tensor_4進行隨機排序,并輸出結果
print("\n隨機排序后的張量\n", tensor_4[idx])
運行代碼顯示:
原始張量tensor([[0., 0., 0.],[0., 0., 0.],[0., 0., 0.],[0., 0., 0.]])生成的隨機序號tensor([3, 0, 2, 1])隨機排序后的張量tensor([[0., 0., 0.],[0., 0., 0.],[0., 0., 0.],[0., 0., 0.]])