網站首頁 編程語言 正文
查看Tensor尺寸及查看數據類型
Tensor尺寸查看
命令:
x.shape
例子:
input = torch.randn(20,16,50,32) input.shape
輸出為:
注意調用的時候不要用x.shape()
否則會報錯:AttributeError: 'Tensor' object has no attribute 'get_shape'
數據類型查看
命令:
x.type()
例子:
input = torch.randn(20,16,50,32) input.type()
輸出:
Pytorch基本數據類型tensor
在Pytorch中必須使用Pytorch特有的張量(tensor)數據類型,本文介紹tensor的基本操作
Python和Pytorch數據類型對應
以上數據是存儲在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
創建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) #直接創建 d = torch.Tensor([4,5,6,7]) print(d) #直接創建 e = torch.Tensor(2,3) print(e) #用shape創建隨機的指定維度的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的隨機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:]) #指定一塊子區域 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的維度變換(重點)
基本操作:
- 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)) #擴展維度 # 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的數學運算
import torch #基本加減乘除 a = torch.zeros(4,3) b = torch.ones(3) # print(a+b) #這樣會報錯,正確做法如下 # print(torch.add(a, b)) #結果全是1,broadcast運算邏輯 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) #可以看到,本質上也是二維矩陣相乘的規律 #clamp算法 print(c.clamp(4)) #把小于4的值全部都替換成4
tensor的統計相關操作
import torch #norm方法(求范數) 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)) #同理求二范數 print(b) print(b.norm(1,dim=1,dtype=float)) #求指定維度的范數 #求最大、最小、平均、求和 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))
原文鏈接:https://blog.csdn.net/ShuqiaoS/article/details/88744813
相關推薦
- 2022-12-15 Go字典使用詳解_Golang
- 2022-07-02 如何對numpy?矩陣進行通道間求均值_python
- 2022-12-08 python求兩個時間的時間差(實例代碼)_python
- 2022-07-30 Python?excel合并居中值相同的單元格實例代碼_python
- 2022-07-30 使用?gomonkey?Mock?函數及方法示例詳解_Golang
- 2023-05-14 python隨機獲取列表中某一元素的方法_python
- 2022-09-05 Involution: Inverting the Inherence of Convolution
- 2023-04-14 python使用pyodbc連接sqlserver_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支