torch.max() and torch.view()

import torch
import numpy as np

np_a = np.array([[1,2,3],[4,5,6],[7,8,9]])

tensor_a = torch.from_numpy(np_a.astype('float32'))

print(tensor_a)
# ()括號中的1/0 表示獲取 行/列 的最值
# []中的0,1 表示 獲取的是最值 還是 最值的索引
print(torch.max(tensor_a,1)[0])
print(torch.max(tensor_a,0)[1])

print(tensor_a.shape)
# -1表示佔位符,表示把原tensor維度轉爲目標需要的維度
# 即 x*1*3=9  ==> x=3 所以轉換之後的維度是3,1,3
print(tensor_a.view(-1,1,3).shape)
# 此處爲 1*x*3=9  ==> x=3 所以轉換之後的維度是1,3,3
print(tensor_a.view(1,-1,3).shape)
# 此處爲 1*3*x=9  ==> x=3 所以轉換之後的維度是1,3,3
print(tensor_a.view(1,3,-1).shape)
# 此處爲 9*x=9  ==> x=1 所以轉換之後的維度是9,1
print(tensor_a.view(9,-1).shape)
# 此處爲 x*9=9  ==> x=1 所以轉換之後的維度是1,9
print(tensor_a.view(-1,9).shape)
# 此處爲 x*1=9  ==> x=9 所以轉換之後的維度是9,1
print(tensor_a.view(-1,1).shape)

 

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