網(wǎng)站首頁 編程語言 正文
關(guān)于Tensor的數(shù)據(jù)類型說明
1. 32位浮點(diǎn)型:torch.FloatTensor
a=torch.Tensor( [[2,3],[4,8],[7,9]], )
print "a:",a
print "a.size():",a.size()
print "a.dtype:",a.dtype
b=torch.FloatTensor( [[2,3],[4,8],[7,9]] )
print "b:",b
print "b.shape:",b.shape
print "b.dtype:",b.dtype
可以看出?torch.FloatTensor 是32位float類型,并且torch.Tensor默認(rèn)的數(shù)據(jù)類型是32位float類型。
2. 64位浮點(diǎn)型:torch.DoubleTensor
b=torch.DoubleTensor( [[2,3],[4,8],[7,9]] )
print "b:",b
print "b.shape:",b.shape
print "b.dtype:",b.dtype
?3. 16位整型:torch.ShortTensor
b=torch.ShortTensor( [[2,3],[4,8],[7,9]] )
print "b:",b
print "b.shape:",b.shape
print "b.dtype:",b.dtype
4.? 32位整型:torch.IntTensor
b=torch.IntTensor( [[2,3],[4,8],[7,9]] )
print "b:",b
print "b.shape:",b.shape
print "b.dtype:",b.dtype
5. 64位整型:torch.LongTensor
b=torch.LongTensor( [[2,3],[4,8],[7,9]] )
print "b:",b
print "b.shape:",b.shape
print "b.dtype:",b.dtype
6. 快速創(chuàng)建Tensor
(1)?torch.zeros()
a=torch.zeros( size=(4,5),dtype=torch.float32 )
print a
print a.shape
print a.dtype
(2)?torch.randn()
a=torch.randn( size=(4,5),dtype=torch.float32 )
print a
print a.shape
print a.dtype
7. Tensor索引方式,參考numpy
8. Tensor和numpy數(shù)組轉(zhuǎn)換:
(1) Tensor轉(zhuǎn)numpy,
a=torch.randn( size=(4,5),dtype=torch.float32 )
print a
print a.shape
print a.dtype
b= a.numpy()
print b
print b.shape
print b.dtype
(2) numpy轉(zhuǎn)Tensor,
a=np.random.randn(4,3)
print a
print a.shape
print a.dtype
b=torch.from_numpy( a )
print b
print b.shape
print b.dtype
9.更改Tensor的數(shù)據(jù)類型,
a=torch.FloatTensor( (3,2) )
print a
print a.shape
print a.dtype
a.int()
print a
print a.shape
print a.dtype
10. GPU加速,如果pytorch支持GPU加速,可以加Tensor放到GPU執(zhí)行,
if torch.cuda.is_available():
a_cuda = a.cuda()
pytorch Tensor變形函數(shù)
view(), resize(), reshape() 在不改變?cè)璽ensor數(shù)據(jù)的情況下修改tensor的形狀,前后要求元素總數(shù)一致,且前后tensor共享內(nèi)存
如果想要直接改變Tensor的尺寸,可以使用resize_()的原地操作函數(shù)。
在resize_()函數(shù)中,如果超過了原Tensor的大小則重新分配內(nèi)存,多出部分置0,如果小于原Tensor大小則剩余的部分仍然會(huì)隱藏保留。
transpose()函數(shù)可以將指定的兩個(gè)維度的元素進(jìn)行轉(zhuǎn)置,而permute()函數(shù)則可以按照給定的維度進(jìn)行維度變換。
在實(shí)際的應(yīng)用中,經(jīng)常需要增加或減少Tensor的維度,尤其是維度為1的情況,這時(shí)候可以使用squeeze()與unsqueeze()函數(shù),前者用于去除size為1的維度,而后者則是將指定的維度的size變?yōu)?。
有時(shí)需要采用復(fù)制元素的形式來擴(kuò)展Tensor的維度,這時(shí)expand就派上用場(chǎng)了。
expand()函數(shù)將size為1的維度復(fù)制擴(kuò)展為指定大小,也可以使用expand_as()函數(shù)指定為示例Tensor的維度。
注意:在進(jìn)行Tensor操作時(shí),有些操作如transpose()、permute()等可能會(huì)把Tensor在內(nèi)存中變得不連續(xù),而有些操作如view()等是需要Tensor內(nèi)存連續(xù)的,這種情況下需要使用contiguous()操作先將內(nèi)存變?yōu)檫B續(xù)的。在PyTorch v0.4版本中增加了reshape()操作,可以看做是Tensor.contiguous().view()
Tensor的排序與取極值
排序函數(shù)sort(),選擇沿著指定維度進(jìn)行排序,返回排序后的Tensor及對(duì)應(yīng)的索引位置。
max()與min()函數(shù)則是沿著指定維度選擇最大與最小元素,返回該元素及對(duì)應(yīng)的索引位置。
Tensor與NumPy轉(zhuǎn)換
Tensor與NumPy可以高效地進(jìn)行轉(zhuǎn)換,并且轉(zhuǎn)換前后的變量共享內(nèi)存。在進(jìn)行PyTorch不支持的操作時(shí),甚至可以曲線救國(guó),將Tensor轉(zhuǎn)換為NumPy類型,操作后再轉(zhuǎn)為Tensor。
原文鏈接:https://blog.csdn.net/moshiyaofei/article/details/89703161
相關(guān)推薦
- 2022-05-22 部署ASP.NET?Core程序到Linux系統(tǒng)_基礎(chǔ)應(yīng)用
- 2022-11-11 Navicat?Premium自定義?sql?標(biāo)簽的創(chuàng)建方式_數(shù)據(jù)庫其它
- 2022-09-13 go語言中基本數(shù)據(jù)類型及應(yīng)用快速了解_Golang
- 2022-05-15 Element框架里日期選擇器限制時(shí)間,最多選31天
- 2022-05-20 C++實(shí)現(xiàn)簡(jiǎn)單學(xué)生信息管理系統(tǒng)_C 語言
- 2022-11-05 Android開發(fā)使用Databinding實(shí)現(xiàn)關(guān)注功能mvvp_Android
- 2022-07-08 python?根據(jù)csv表頭、列號(hào)讀取數(shù)據(jù)的實(shí)現(xiàn)_python
- 2022-06-22 Git的基礎(chǔ)文件操作初始化查看添加提交示例教程_其它綜合
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支