pytorch的Tensor基礎操作

一.數據類型

64位整型:torch.LongTensor
32位整型:torch.IntTensor
16位整型:torch.ShortTensor
64位浮點型:torch.DoubleTensor
import torch
import numpy as np
from torch.autograd import Variable

x0=torch.tensor([1,2,3,4])
x1=torch.FloatTensor([1,2,3,4])
x2=torch.IntTensor([1,2,3,4])
x3=torch.LongTensor([1,2,3,4])
x4=torch.ShortTensor([1,2,3,4])
x5=torch.DoubleTensor([1,2,3,4])

print(x0)
print(x1)
print(x2)
print(x3)
print(x4)
print(x5)
print(x0.dtype) #tensor函數的dtype默認torch.int64
print(x3.dtype)

輸出結果:

tensor([1, 2, 3, 4])
tensor([1., 2., 3., 4.])
tensor([1, 2, 3, 4], dtype=torch.int32)
tensor([1, 2, 3, 4])
tensor([1, 2, 3, 4], dtype=torch.int16)
tensor([1., 2., 3., 4.], dtype=torch.float64)
torch.int64
torch.int64
torch.float32

二.tensor 與numpy相互轉換

rt=torch.randn(2,3)
print(type(rt))
rt_to_np=rt.numpy()
print(type(rt_to_np))
np_to_rt=torch.from_numpy(rt_to_np)
print(type(np_to_rt))

輸出結果:

<class 'torch.Tensor'>
<class 'numpy.ndarray'>
<class 'torch.Tensor'>

 

三.tensor幾種計算

x=torch.FloatTensor([1,2,3,4,5])
y=torch.FloatTensor([1,2,3,4,5])
print(x+y)
print(x-y)
print(x*y)
print(x/y)

# 聚合函數

print(torch.max(x))  # 最大,若是多維,在max函數中加上維度參數即可
print(torch.sum(x))  # 求和
print(torch.median(x))  # 中位數
print(torch.mean(x)) # 均值

''' 輸出
tensor(5.)
tensor(15.)
tensor(3.)
tensor(3.)
'''

# 形狀

print(x.shape)
print(x.size())  # size要加括號
print(rt.shape)
print(rt.size())

''' 輸出
torch.Size([5])
torch.Size([5])
torch.Size([2, 3])
torch.Size([2, 3])
'''


# 求導

t=torch.FloatTensor([[1,2],[3,4]])
v=Variable(t,requires_grad=True)  # requires_grad 參數一定要
v2=torch.sum(v*v)
v2.backward()
print(v.grad)

''' 輸出爲
tensor([[2., 4.],
        [6., 8.]])

爲什麼,爲什麼?

!!!  v2爲標量,標量對矩陣的求導爲 標量對矩陣裏各元素的求導,

v2= x11^2 + x12^2 +x13^2 +x14^2 ,所以得到求導矩陣爲
[
   d(v2)/d(x11),d(v2)/d(x12)     
   d(v2)/d(x21),d(v2)/d(x22)
]

其中d(v2)/d(x11) = 2*x11=2 ,就這麼簡單 !

'''

 

四.隨機生成

print(torch.rand(2,3))
# 生成I array
print(torch.ones(2,3))
# 生成單位對角array
print(torch.eye(3,3))
# 生成0 array
print(torch.zeros(2,3))
# 生成與某個array相似的I矩陣
print(torch.ones_like(torch.rand(4,3)))
# 生成與某個array相似的極小矩陣
print(torch.empty_like(torch.rand(4,3)))
# 生成某個數的array
print(torch.full((2,5,3),fill_value=4))

輸出結果:

tensor([[0.0218, 0.6911, 0.8507],
        [0.3779, 0.0360, 0.4767]])
tensor([[1., 1., 1.],
        [1., 1., 1.]])
tensor([[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]])
tensor([[0., 0., 0.],
        [0., 0., 0.]])
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]])
tensor([[ 3.4162e-18,  3.0917e-41,  3.4162e-18],
        [ 3.0917e-41,  3.4162e-18,  3.0917e-41],
        [-3.7711e-34,  4.5804e-41, -3.5738e-34],
        [ 4.5804e-41, -1.3618e-38,  4.5804e-41]])
tensor([[[4., 4., 4.],
         [4., 4., 4.],
         [4., 4., 4.],
         [4., 4., 4.],
         [4., 4., 4.]],

        [[4., 4., 4.],
         [4., 4., 4.],
         [4., 4., 4.],
         [4., 4., 4.],
         [4., 4., 4.]]])

 

五.塑形

# view & reshape
test = torch.rand((4,1,2,2)) # 從外往裏面數
print(test.view(4,2*2))
print(test.reshape(4,2*2))  # 和view作用一樣

''' 輸出
tensor([[0.0976, 0.6314, 0.3482, 0.8822],
        [0.9157, 0.9412, 0.7420, 0.6484],
        [0.4440, 0.7826, 0.5866, 0.7577],
        [0.2833, 0.8003, 0.5582, 0.9833]])
tensor([[0.0976, 0.6314, 0.3482, 0.8822],
        [0.9157, 0.9412, 0.7420, 0.6484],
        [0.4440, 0.7826, 0.5866, 0.7577],
        [0.2833, 0.8003, 0.5582, 0.9833]])
'''

#unsqueeze,在參數之前增加一維

print(test.shape)
test1=test.unsqueeze(0) # 在第0維前插入一維  -1代表最後一維
print(test1.shape)

'''輸出
torch.Size([4, 1, 2, 2])
torch.Size([1, 4, 1, 2, 2])

'''

#squeeze 如果輸入參數的維爲1,則去掉該維

test1=test1.squeeze(2)
print(test1.shape)

'''輸出

torch.Size([1, 4, 2, 2])

'''

# repeat 維度倍數
test2=test.repeat(1,1,2,3) # 在原來維度基礎上乘上參數
print(test2.shape)

'''輸出
torch.Size([4, 1, 4, 6])

'''


# t,值針對二維

c=torch.rand(2,3)
print(c.t())


# 兩兩維度交換
print(test2.shape)
print(test2.transpose(1,3).shape)

'''輸出

torch.Size([4, 1, 4, 6])
torch.Size([4, 6, 4, 1])

'''

# 多個維度進行交換
print(test2.permute(3,2,1,0).shape)

'''輸出
torch.Size([6, 4, 1, 4])

'''


#contiguous,前面遇到permute or tranpose等操作,先進行contiguous,再進行view
print(test2.permute(3,2,1,0).contiguous())

'''輸出

tensor([[[[0.0976, 0.9157, 0.4440, 0.2833]],

         [[0.3482, 0.7420, 0.5866, 0.5582]],

         [[0.0976, 0.9157, 0.4440, 0.2833]],

         [[0.3482, 0.7420, 0.5866, 0.5582]]],


        [[[0.6314, 0.9412, 0.7826, 0.8003]],

         [[0.8822, 0.6484, 0.7577, 0.9833]],

         [[0.6314, 0.9412, 0.7826, 0.8003]],

         [[0.8822, 0.6484, 0.7577, 0.9833]]],


        [[[0.0976, 0.9157, 0.4440, 0.2833]],

         [[0.3482, 0.7420, 0.5866, 0.5582]],

         [[0.0976, 0.9157, 0.4440, 0.2833]],

         [[0.3482, 0.7420, 0.5866, 0.5582]]],


        [[[0.6314, 0.9412, 0.7826, 0.8003]],

         [[0.8822, 0.6484, 0.7577, 0.9833]],

         [[0.6314, 0.9412, 0.7826, 0.8003]],

         [[0.8822, 0.6484, 0.7577, 0.9833]]],


        [[[0.0976, 0.9157, 0.4440, 0.2833]],

         [[0.3482, 0.7420, 0.5866, 0.5582]],

         [[0.0976, 0.9157, 0.4440, 0.2833]],

         [[0.3482, 0.7420, 0.5866, 0.5582]]],


        [[[0.6314, 0.9412, 0.7826, 0.8003]],

         [[0.8822, 0.6484, 0.7577, 0.9833]],

         [[0.6314, 0.9412, 0.7826, 0.8003]],

         [[0.8822, 0.6484, 0.7577, 0.9833]]]])
'''

 

發佈了14 篇原創文章 · 獲贊 6 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章