PyTorch學習:Tensor的操作

x = torch.ones(2, 2)
print(x) # 這是一個float tensor
 1  1
 1  1
[torch.FloatTensor of size 2x2]
print(x.type())
torch.FloatTensor
# 將其轉化爲整形
x = x.long()
# x = x.type(torch.LongTensor)
print(x)
1  1
 1  1
[torch.LongTensor of size 2x2]
# 再將其轉回 float
x = x.float()
# x = x.type(torch.FloatTensor)
print(x)
 1  1
 1  1
[torch.FloatTensor of size 2x2]
x = torch.randn(4, 3)
print(x)
# 沿着行取最大值
max_value, max_idx = torch.max(x, dim=1)
# 每一行的最大值
print("max_value:",max_value)
# 每一行最大值的下標
print("max_idx:",max_idx)
# 沿着行對 x 求和
sum_x = torch.sum(x, dim=1)
print("sum_x:",sum_x)

tensor([[-0.2049, -0.7280, -0.6476],
        [ 0.0815,  1.5821,  0.5055],
        [ 1.9508,  0.8439, -0.6849],
        [-1.8688, -0.1023, -0.1355]])
max_value: tensor([-0.2049,  1.5821,  1.9508, -0.1023])
max_idx: tensor([ 0,  1,  0,  1])
sum_x: tensor([-1.5805,  2.1691,  2.1098, -2.1066])

 

x = torch.randn(4, 3)
print(x)
# 增加維度或者減少維度
print(x.shape)
x = x.unsqueeze(0) # 在第一維增加
print("在第一維增加:",x.shape)
x = x.unsqueeze(1) # 在第二維增加
print("在第二維增加:",x.shape)
x = x.squeeze(0) # 減少第一維
print("減少第一維:",x.shape)
x = x.squeeze() # 將 tensor 中所有的一維全部都去掉
print("將 tensor 中所有的一維全部都去掉:",x.shape)

tensor([[-1.9672, -0.4869, -0.0786],
        [-1.3666,  2.5143, -1.8407],
        [ 0.4950,  0.7476, -1.8379],
        [ 0.4382,  0.0148,  0.9794]])
torch.Size([4, 3])
在第一維增加: torch.Size([1, 4, 3])
在第二維增加: torch.Size([1, 1, 4, 3])
減少第一維: torch.Size([1, 4, 3])
將 tensor 中所有的一維全部都去掉: torch.Size([4, 3])

 

x = torch.randn(3, 4, 5)
print(x.shape)
# 使用permute和transpose進行維度交換
x = x.permute(1, 0, 2) # permute 可以重新排列 tensor 的維度
print("permute 可以重新排列 tensor 的維度:",x.shape)
x = x.transpose(0, 2)  # transpose 交換 tensor 中的兩個維度
print("transpose 交換 tensor 中的兩個維度:",x.shape)
# 使用 view 對 tensor 進行 reshape

x = torch.randn(3, 4, 5)
print(x.shape)
x = x.view(-1, 5) # -1 表示任意的大小,5 表示第二維變成 5
print("-1 表示任意的大小,5 表示第二維變成 5:",x.shape)
x = x.view(3, 20) # 重新 reshape 成 (3, 20) 的大小
print("重新 reshape 成 (3, 20) 的大小:",x.shape)

torch.Size([3, 4, 5])
permute 可以重新排列 tensor 的維度: torch.Size([4, 3, 5])
transpose 交換 tensor 中的兩個維度: torch.Size([5, 3, 4])
torch.Size([3, 4, 5])
-1 表示任意的大小,5 表示第二維變成 5: torch.Size([12, 5])
重新 reshape 成 (3, 20) 的大小: torch.Size([3, 20])

 

x = torch.randn(3, 4)
y = torch.randn(3, 4)
# 兩個 tensor 求和
z = x + y
# z = torch.add(x, y)
print(x)
print(y)
print(z)

tensor([[ 0.5508, -0.2623, -1.6528,  0.5098],
        [-0.1954,  0.6015,  0.4941,  1.8010],
        [ 0.3290, -0.8695, -0.4135, -0.0048]])
tensor([[-0.9778,  1.7069,  0.3363,  0.0655],
        [ 0.7497,  0.7741, -0.4164,  0.0623],
        [-1.4515,  0.0408, -0.5595,  0.0705]])
tensor([[-0.4270,  1.4446, -1.3165,  0.5753],
        [ 0.5543,  1.3756,  0.0777,  1.8633],
        [-1.1225, -0.8287, -0.9731,  0.0657]])

 

x = torch.ones(3, 3)
print(x.shape)
# unsqueeze 進行 inplace
x.unsqueeze_(0)
print(x.shape)
# transpose 進行 inplace
x.transpose_(1, 0)
print(x.shape)
x = torch.ones(3, 3)
y = torch.ones(3, 3)
print(x)
# add 進行 inplace
x.add_(y)
print(x)

pytorch中大多數的操作都支持 inplace 操作,也就是可以直接對 tensor 進行操作而不需要另外開闢內存空間,方式非常簡單,一般都是在操作的符號後面加_

torch.Size([3, 3])
torch.Size([1, 3, 3])
torch.Size([3, 1, 3])
tensor([[ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.]])
tensor([[ 2.,  2.,  2.],
        [ 2.,  2.,  2.],
        [ 2.,  2.,  2.]])

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章