Python查看Tensor尺寸及查看數(shù)據(jù)類型的實(shí)現(xiàn)
查看Tensor尺寸及查看數(shù)據(jù)類型
Tensor尺寸查看
命令:
x.shape
例子:
input = torch.randn(20,16,50,32) input.shape
輸出為:
注意調(diào)用的時候不要用x.shape()
否則會報錯:AttributeError: 'Tensor' object has no attribute 'get_shape'
數(shù)據(jù)類型查看
命令:
x.type()
例子:
input = torch.randn(20,16,50,32) input.type()
輸出:
Pytorch基本數(shù)據(jù)類型tensor
在Pytorch中必須使用Pytorch特有的張量(tensor)數(shù)據(jù)類型,本文介紹tensor的基本操作
Python和Pytorch數(shù)據(jù)類型對應(yīng)
以上數(shù)據(jù)是存儲在CPU中。
如果使用.cuda(),會返回一個GPU上的引用
import torch a = torch.tensor([1,2]) c = a.cuda print(c) print(type(c)) -----輸出------ D:\Users\Lenovo\anaconda3\python.exe C:/Users/Lenovo/Desktop/pythonProject2/main.py <built-in method cuda of Tensor object at 0x000002A03434CA00> <class 'builtin_function_or_method'> Process finished with exit code 0
創(chuàng)建tensor的方法
import torch import numpy a = numpy.array([1,2,3,4]) b = torch.from_numpy(a) #從numpy中引入 print(b) c = torch.tensor([1,2,3,4]) print(c) #直接創(chuàng)建 d = torch.Tensor([4,5,6,7]) print(d) #直接創(chuàng)建 e = torch.Tensor(2,3) print(e) #用shape創(chuàng)建隨機(jī)的指定維度的tensor
一些常用的生成tensor方法
import torch a = torch.rand(3, 3) #生成指定大小的,元素范圍[0,1]的tensor print(a) b = torch.rand_like(a) #生成與對象tensor大小一致的tensor print(b) c = torch.randint(1,10,(3,3)) #生成(3,3)大小,[1,10)范圍的tensor(包括1,但不包括10) print(c) d = torch.randn(2,4) #生成均值為0,方差為1的隨機(jī)tensor print(d) e = torch.full([2,5],0) #生成全部一樣的tensor print(e) f = torch.arange(0,10) #生成順序tensor print(f) g = torch.linspace(0,9,steps=8) #生成[0,9],等分成8個的tensor print(g) print(torch.ones(3,1), torch.zeros(4,5), torch.eye(6)) #生成全1,全0,單位矩陣tensor
tensor的切片與索引
import torch a = torch.rand(3, 7) #生成指定大小的,元素范圍[0,1]的tensor print(a) print("*"*100) print(a[0]) #取一行 print("*"*100) print(a[:2,4:]) #指定一塊子區(qū)域 print("*"*100) print(a[:,2]) #取一列 print("*"*100) print(a[:,0:7:2]) #[0,7]行隔2行取樣 print("*"*100) print(a.index_select(1,torch.tensor([2,6]))) #指定切片位置 print("*"*100) mask = a.ge(0.8) print(torch.masked_select(a,mask)) #通過掩碼條件切片(注意切片后會被flatten) print(mask) #看看mask矩陣
tensor的維度變換(重點(diǎn))
基本操作:
- view/reshape
- squeeze/unsqueeze
import torch a = torch.rand(3,4,2) #生成指定大小的,元素范圍[0,1]的tensor print(a) # view方法:變換tensor的形狀 print(a.view(3, 8)) #壓縮維度 print(a.view(3,2,2,2)) #擴(kuò)展維度 # unsqueeze方法:在指定地方插入一維(squeeze方法同理會消除一維) print(a.unsqueeze(1)) print(a.squeeze(1)) #squeeze只有在原有維度為1才有用,試試把上面改成rand(3,1,2)看看效果
tensor的疊加和分割
```python import torch #cat操作 a = torch.rand(4,1,3) b = torch.rand(3,1,3) # print(a) # print(b) c = torch.cat((a,b)) #合并tensor(只能在其他dimension一致的情況下才能合并) # print(c) # print(c.shape) #stack操作 d = torch.rand(4,1) e = torch.rand(4,1) # print(d) # print(e) f = torch.stack((d,e)) #合并tensor,與cat不同的是,stack會增加一個更高的維度 # print(f) # print(f.shape) #split操作 g = torch.rand(5,2,1) h,i = g.split([1,4]) # print(g) # print(h) # print(i)
tensor的數(shù)學(xué)運(yùn)算
import torch #基本加減乘除 a = torch.zeros(4,3) b = torch.ones(3) # print(a+b) #這樣會報錯,正確做法如下 # print(torch.add(a, b)) #結(jié)果全是1,broadcast運(yùn)算邏輯 c = torch.tensor([[1,2,3],[4,5,6],[7,8,9]]) d = torch.eye(3) # print(torch.add(c,d)) # print(c+d) #不采用broadcast邏輯時,這樣也可以 # print(c*d) # print(c/d) # print(c**2) #平方 # print(c**0.5) #開平方 #矩陣相乘 # print(torch.matmul(torch.ones(3,3),torch.ones(3,3))) # print(torch.ones(3,3)@torch.ones(3,3)) #用@的效果是一樣的 #高維度的矩陣相乘 e = torch.rand(4,3,2,3) f = torch.rand(4,3,3,5) g = e@f # print(g) # print(g.shape) #可以看到,本質(zhì)上也是二維矩陣相乘的規(guī)律 #clamp算法 print(c.clamp(4)) #把小于4的值全部都替換成4
tensor的統(tǒng)計(jì)相關(guān)操作
import torch #norm方法(求范數(shù)) a = torch.arange(10,dtype=float) b = a.view(2,5) print(a.norm(1)) print(b.norm(1)) print(a.norm(2,dtype=float)) #同理求二范數(shù) print(b) print(b.norm(1,dim=1,dtype=float)) #求指定維度的范數(shù) #求最大、最小、平均、求和 print(a.sum()) print(a.min()) print(a.max()) print(a.mean()) #top N的值 c = torch.tensor([1,2,3,3,4,4,4,5,5,5,5,6,8,8],dtype=float) print(c.topk(3)) print(c.topk(3, largest=False)) #找到前N最小的值 print(c.kthvalue(4)) #找到第k小的值 #比較 print(a>4) print(a!=8) #where cond = torch.tensor([[1,2],[3,4]],dtype=float) #用where組合2個tensor d = torch.zeros(2,2) e = torch.ones(2,2) print(torch.where(cond>2,d,e))
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
python 3利用Dlib 19.7實(shí)現(xiàn)攝像頭人臉檢測特征點(diǎn)標(biāo)定
這篇文章主要為大家詳細(xì)介紹了python 3利用Dlib 19.7實(shí)現(xiàn)攝像頭人臉檢測特征點(diǎn)標(biāo)定,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-02-02python發(fā)送byte數(shù)據(jù)組到tcp的server問題
這篇文章主要介紹了python發(fā)送byte數(shù)據(jù)組到tcp的server問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09python內(nèi)置HTTP Server如何實(shí)現(xiàn)及原理解析
這篇文章主要為大家介紹了python內(nèi)置HTTP Server如何實(shí)現(xiàn)及原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11Python 快速實(shí)現(xiàn)CLI 應(yīng)用程序的腳手架
本篇文章主要介紹了Python 快速實(shí)現(xiàn)CLI 應(yīng)用程序的腳手架,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12Python實(shí)現(xiàn)解析Bit Torrent種子文件內(nèi)容的方法
這篇文章主要介紹了Python實(shí)現(xiàn)解析Bit Torrent種子文件內(nèi)容的方法,結(jié)合實(shí)例形式分析了Python針對Torrent文件的讀取與解析相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2017-08-08