Pytorch學習手札(三)---張量的拼接分割、基本數學計算、屬性統計

pytorch張量的拼接分割、基本數學計算、屬性統計

1.Broadcasting:張量維度自動擴展

eg:張量a:[3,12,12,12]
偏置b:[12,1,1] > [1,12,1,1]>[3,12,12,12]
總結一下:
第一步:擴張一個維度;第二步:複製擴張成跟a一樣形狀。就可以和tensor a進行相乘相加等操作
結合以下實例理解:
在這裏插入圖片描述

2.tensor的拼接與分割

(1)合併API
1)Cat:對數據進行維度上的合併,不增加屬性
2)Stack:增加一個維度,增加一個屬性進行數據分類,不對數據進行簡單的合併
(2)拆分API
1)Split:按長度拆分(by len([1,2,3…]) or len(1))
2)Chunk:按數量拆分(by num)

3.數學運算

(1)基本的加減乘除
1)運算符形式(±*/)
2)add/sub/mul/div-pytorch的運算名稱
(2)次方函數power(a,n)
(a,n)表示a的n次方、指數exp和對數函數log函數
(3)矩陣相乘
torch.mm(僅僅適用於dim=2的情況)/torch.matmul()/@(三種形式)
(4)近似值函數:
a=torch.tensor(3.14)
print(a.floor()) #向下取整函數
print(a.ceil()) #向上取整函數
print(a.trunc()) #數據的整數部分
print(a.frac()) #數據的小數部分
print(a.round())
(5)clamp梯度裁剪

4.常見屬性計算與統計

(1)範數函數norm:
1範數:所有x的絕對值相加
2範數:所有x的平方和 ,再開根號
(2)其他常用屬性的計算與統計

 a=torch.randn(4,10)
print(a[0])
print(a.min())
print(a.max())
print(a.mean())
print(a.prod()) #所以元素累乘
print(a.std())
print(a.sum())
print(a.argmax(dim=0))
print(a.argsort())  #不指定維度變爲一維
print(a.argmin(dim=1)) #指定維度

(3)dim/keepdim函數的作用:主要用來結果輸出維度的變換
(4)topk函數:
topk(n,dim=None,largest=False)
求取某一維度數據上前n大的數據及其索引
參數:n:取幾個,dim:指定維度,largest:大或小排序,默認按大到小
kthvalue(求取第n小的數據及其索引)
(5)常見比較函數:
< > >= <= !=
torch.gt()
torch.eq(a,b)
torch.equal(a,b)

5.其他的高階操作

(1)where
where(condition,A,B):函數原型:拼接和組裝功能
condition:篩選條件
A,B:符合取A值,否則B值
(2)gather
gather(input,dim,index,out=NOne)
input:可以理解爲輸入概率值
dim:指定維度
index:下標

6.本節代碼demo

# -*- coding: utf-8 -*-
'''
@Author: Jason
@Desc  : 
'''
import torch
#1.cat拼接:參數dim 指定合併維度,除該指定維度外其他維度需要一樣
a=torch.rand(100,100,20)
b=torch.rand(200,100,20)
# print(torch.cat([a,b],dim=0).shape)#torch.Size([300, 100, 20])
# print(torch.cat([a,torch.rand(100,1,20)],dim=0).shape)#RuntimeError: invalid argument 0
# print(torch.cat([a,torch.rand(100,1,20)],dim=1).shape)#torch.Size([100, 101, 20])

#2.stack拼接:指定位置增加一個維度
a1 = torch.rand(100,100,20)
# print(torch.cat([a,a1],dim=2).shape)#torch.Size([100, 100, 40])
# print(torch.stack([a,a1],dim=2).shape) #torch.Size([100, 100, 2, 20])

#3.split拆分:按長度拆分
c = torch.stack([a,a1],dim=0)
# print(c.shape) #torch.Size([2, 100, 100, 20])
c1,c2 = c.split([1,1],dim=0)
# print(c1.shape,b.shape) #torch.Size([1, 100, 100, 20]) torch.Size([200, 100, 20])
c3  = torch.rand([6,100,20])
# 3.1 將第一維度指定拆分:eg成4:2
c31,c32 = c3.split([4,2],dim=0)
# print(c31.shape,c32.shape)#torch.Size([4, 100, 20]) torch.Size([2, 100, 20])
c33,c34,c35,c36 = c3.split([3,1,1,1],dim=0)
# print(c33.shape,c36.shape)#torch.Size([3, 100, 20]) torch.Size([1, 100, 20])
c37,c38,c39 = c3.split(2,dim=0)#將第一維度拆成指定n份,每份爲2
# print(c37.shape)#torch.Size([2, 100, 20])

#4.chunk按數量拆分
d = torch.rand([6,6,6])
d1 = d.chunk(3,dim=0)#將第一維度拆成3份
# print(d1[0].shape)#torch.Size([2, 6, 6])  分成每個第一維度都是 2

#5.tensor的數學運算加減乘除
a=torch.rand(2,2)
# print(a)
'''
tensor([[0.3758, 0.0518],
        [0.1825, 0.4583]])
'''
b=torch.rand(2)#tensor([0.8340, 0.5110])
# print(b)
# '''tensor([[1.2098, 0.5628],
#         [1.0165, 0.9693]])
#         '''
# print(a+b)
# '''tensor([[0.3135, 0.0265],
#         [0.1522, 0.2342]])'''
# print(a*b)#以下結果省略複製
# print(a-b)
# print(a/b)
# print(torch.add(a,b)) #與上面是等效的
# print(torch.mul(a,b))
# print(torch.sub(a,b))
# print(torch.div(a,b))
# #矩陣的運算函數-矩陣相乘-torch.mm(僅僅適用於dim=2的情況)/torch.matmul()/@(三種形式)
a=torch.ones(2,2)
b=torch.tensor([[1.,2.],[3.,4.]])
# print(torch.mm(a,b))#只能對2維度的
'''tensor([[4., 6.],
        [4., 6.]])
'''
# print(torch.matmul(a,b))
'''
tensor([[4., 6.],
        [4., 6.]])'''
# print(a@b) #三種運算等效
'''
tensor([[4., 6.],
        [4., 6.]])
'''
# 矩陣相乘實際應用:矩陣降維
# a=torch.rand(4,784)
# w=torch.rand(512,784)
# [email protected]() #這裏的轉置只適用於2維,多維的話使用之前用過的transpose
# print(b.shape)

# #次方函數power(a,n)表示a的n次方、指數exp和對數函數
a=torch.tensor([[1,3],[2,4]],dtype=float)
# print(a)
'''tensor([[1., 3.],
        [2., 4.]], dtype=torch.float64)
'''
# print(pow(a,3)) #a的立方
'''tensor([[ 1., 27.],
        [ 8., 64.]], dtype=torch.float64)
'''
# print(a.sqrt()) #a的平方根
'''tensor([[1.0000, 1.7321],
        [1.4142, 2.0000]], dtype=torch.float64)
'''
# print(a.rsqrt()) #a的平方根的倒數
'''tensor([[1.0000, 0.5774],
        [0.7071, 0.5000]], dtype=torch.float64)'''

a = torch.exp(torch.ones(2,2))
# print(a)
'''
tensor([[2.7183, 2.7183],
        [2.7183, 2.7183]])
'''
# print(torch.exp(a)) #指數函數e^e
'''tensor([[2.7183, 2.7183],
        [2.7183, 2.7183]])'''
# print(torch.log(a)) #對數函數log(e)a
'''
tensor([[1., 1.],
        [1., 1.]])
'''


# #近似值函數
a=torch.tensor(3.14)
# print(a.floor()) #向下取整函數tensor(3.)
# print(a.ceil()) #向上取整函數tensor(4.)
# print(a.trunc()) #數據的整數部分tensor(3.)
# print(a.frac()) #數據的小數部分tensor(0.1400)
# print(a.round()) #求取數據的四捨五入的數據tensor(3.)

# #clamp梯度裁剪
a=torch.rand(2,6)*15
# print(a)
# print(a.clamp(10)) #取10以上的數據,小於10的數據代替爲10
# print(a.clamp(8,10)) #取8-10的數據,將大於10的數據代替爲10,小於8替換爲8


#6.求取數據的統計屬性
#數據的範數norm函數
a=torch.full([8],1) #生成長度爲8,值全爲1的數據
# print(a)#tensor([1., 1., 1., 1., 1., 1., 1., 1.])
b=a.view(2,4)
c=a.view(2,2,2)#
# print(a.view(2,4))#tensor([[1., 1., 1., 1.],[1., 1., 1., 1.]])
# print(a.view(2,2,2))
'''tensor([[[1., 1.],
         [1., 1.]],

        [[1., 1.],
         [1., 1.]]])
'''
# print(a.norm(1),b.norm(1),c.norm(1))#tensor(8.) tensor(8.) tensor(8.)
# print(a.norm(2),b.norm(2),c.norm(2))#tensor(2.8284) tensor(2.8284) tensor(2.8284)
# print(b.norm(2,dim=1)) #指定維度  tensor([2., 2.])
# print(c.norm(2,dim=2))
'''tensor([[1.4142, 1.4142],
        [1.4142, 1.4142]])'''
# #其他常用屬性的計算與統計
# a=torch.randn(4,10)
# print(a[0])
# print(a.min())
# print(a.max())
# print(a.mean())
# print(a.prod())
# print(a.std())
# print(a.sum())
# print(a.argmax(dim=0))
# print(a.argsort())
# print(a.argmin(dim=1))

# #dim/keepdim函數的作用
# print(a.argmax(dim=1))
# print(a.argmax(dim=1,keepdim=True)) #主要用來數據的維度變換[4],轉換[4,1]
# #topk函數(求取某一維度數據上前n大的數據及其索引)/kthvalue(求取第n小的數據及其索引)
# a=torch.rand(4,10)
# print(a.topk(3,dim=1))
# x,y=a.topk(3,dim=1,largest=False)
# print(a.topk(3,dim=1,largest=False))
# print(x)
# print(a.kthvalue(8,dim=1))
# #常用比較函數compare
# a=torch.rand(4,10)
# print(a>0)
# print(a!=0)
# print(torch.gt(a,0))
# b=torch.rand(4,10)
# print(torch.eq(a,b)) #輸出每個元素對應位置上的相同與否
# print(torch.equal(a,b)) #表示是否完全一樣



#7.高階操作函數where和gather
# #where函數相比for循環來說可以實現GPU高度並行進行,可以提高數數據處理的速度
cond=torch.tensor([[0.4,0.1],[0.7,0.8]])
# print(cond)
A=torch.rand(2,2)
B=torch.rand(2,2)
# print(A,B)
'''
tensor([[0.7527, 0.8846],
        [0.8806, 0.6668]]) 
tensor([[0.0272, 0.0106],
        [0.3369, 0.1159]])
'''
# print(cond)
'''
tensor([[0.4000, 0.1000],
        [0.7000, 0.8000]])
'''
# print(torch.where(cond>0.5,A,B))#cond中數值大於0.5取A中值,否則取B中相應位置值
'''
tensor([[0.0272, 0.0106],
        [0.8806, 0.6668]])
'''


# #gather函數-查表操作,可以在GPU上實現,從而提高數據的處理速度,在前沿的一些數據查詢和加速方面比較常用
input1=torch.rand(4,10)#4行10列
print(input1)
'''
tensor([[0.9137, 0.9590, 0.0762, 0.7017, 0.5470, 0.9915, 0.1082, 0.2921, 0.5826,0.3139],
        [0.8550, 0.8030, 0.7478, 0.8831, 0.9125, 0.3933, 0.8986, 0.0966, 0.7523,0.7332],
        [0.7155, 0.0212, 0.4283, 0.9482, 0.2283, 0.3828, 0.7388, 0.1334, 0.9863,0.7742],
        [0.2813, 0.7313, 0.7587, 0.5425, 0.3005, 0.3247, 0.4904, 0.4449, 0.2057,0.5349]])
'''
print(input1.topk(3,dim=1)[1]) #最有可能的3種
'''
tensor([[5, 1, 0], #第一張照片概率最大的三個值,即可能是其對應的數字
        [4, 6, 3], #第二張...
        [8, 3, 9],
        [2, 1, 3]])
'''
label=torch.tensor(range(100,110))#映射 順序對應的數字爲
print(label)#tensor([100, 101, 102, 103, 104, 105, 106, 107, 108, 109])
print(label.shape)#torch.Size([10])
print(torch.gather(label.expand(4,10),dim=1,index=input1.topk(3,dim=1)[1])) #gather函數的經典案例幫助理解
'''
tensor([[105, 101, 100],
        [104, 106, 103],
        [108, 103, 109],
        [102, 101, 103]])
'''
if __name__ == "__main__":
    print(" ")

轉載請標明 轉自:https://leejason.blog.csdn.net/article/details/106865808

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