pytorch使用-tensor的基本操作解讀
一、tensor加減乘除
加法操作
import torch x = torch.randn(2, 3) y = torch.randn(2, 3) z = x + y print(z) z = torch.add(x, y) print(z) y.add_(x) print(y)

其他操作類似:減法:sub(-), 乘法:mul(*), 除法:div(/)
二、tensor矩陣運(yùn)算
# 二維矩陣相乘 a = torch.full([2, 2], 3, dtype=torch.long) b = torch.ones(2, 2, dtype=torch.long) print(a) print(b) print(torch.mm(a, b))

# matmul 和 @ 可以用于二維矩陣計(jì)算,也可以是多維 # 四維,計(jì)算的時(shí)候就是前兩維不變,后兩維進(jìn)行計(jì)算。 a = torch.rand(1, 1, 3, 2) b = torch.rand(1, 1, 2, 4) c = torch.matmul(a, b) print(a) print(b) print(c)

pow
a = torch.full([2, 2], 6) print(a.pow(3)) a = torch.full([2, 2], 6) print(a**2)
sqrt: 平方根rsqrt: 平方根倒數(shù)
a = torch.full([2, 2], 1024) print(a.sqrt()) print(a.rsqrt()) print(a**0.5)
exp log
a = torch.ones(2, 2) print(torch.exp(a)) print(torch.log(a)) print(torch.log2(a))
.floor()——往下近似.ceil()——往上近似.trunc()——裁剪為整數(shù)部分.frac()——裁剪成小數(shù)部分
a = torch.tensor(3.1415926) print(a.floor()) print(a.ceil()) print(a.trunc()) print(a.frac())
torch.round()——四舍五入
a = torch.tensor(3.1415926) print(a.round())
.item() 轉(zhuǎn)化為python number
x = torch.randn(1) print(x) print(x.item())

四、tensor切片操作
a = torch.randn(4, 3) print(a) # 取第二列 print(a[:, 1]) # 取前兩列 print(a[:, :2])

五、tensor改變形狀
x = torch.randn(4, 4) y = x.view(16) # -1, 自動匹配個數(shù) z = x.view(-1, 8) print(x) print(y) print(z)

六、tensor 和 numpy.array相互轉(zhuǎn)換
# 底層內(nèi)存共享 x = torch.ones(5) print(x) y = x.numpy() print(y) x.add_(1) print(y)

import numpy as np x = np.ones(5) y = torch.from_numpy(x) print(y)

七、tensor 轉(zhuǎn)到GPU上
if torch.cuda.is_available():
device = torch.device("cuda")
x = torch.randn(2, 3)
print(x)
y = x.to(device)
print(y)
z = torch.randn(2, 3, device="cuda")
print(z)
# 同時(shí)在GPU上才能相加
print(y + z)
# 轉(zhuǎn)換會cpu
print(z.to("cpu"))

總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
- Pytorch四維Tensor轉(zhuǎn)圖片并保存方式(維度順序調(diào)整)
- Pytorch如何把Tensor轉(zhuǎn)化成圖像可視化
- 如何計(jì)算 tensorflow 和 pytorch 模型的浮點(diǎn)運(yùn)算數(shù)
- Python tensorflow與pytorch的浮點(diǎn)運(yùn)算數(shù)如何計(jì)算
- PyTorch中關(guān)于tensor.repeat()的使用
- Pytorch?和?Tensorflow?v1?兼容的環(huán)境搭建方法
- Pytorch實(shí)現(xiàn)List?Tensor轉(zhuǎn)Tensor,reshape拼接等操作
相關(guān)文章
Python使用os.listdir()和os.walk()獲取文件路徑與文件下所有目錄的方法
今天小編就為大家分享一篇關(guān)于Python使用os.listdir()和os.walk()獲取文件路徑與文件下所有目錄的方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-04-04
使用Pycharm在運(yùn)行過程中,查看每個變量的操作(show variables)
這篇文章主要介紹了使用Pycharm在運(yùn)行過程中,查看每個變量的操作(show variables),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
Pycharm無法打開雙擊沒反應(yīng)的問題及解決方案
這篇文章主要介紹了Pycharm無法打開,雙擊沒反應(yīng),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
Python實(shí)現(xiàn)希爾排序算法的原理與用法實(shí)例分析
這篇文章主要介紹了Python實(shí)現(xiàn)希爾排序算法,簡單講述了希爾排序的原理并結(jié)合具體實(shí)例形式分析了Python希爾排序的具體實(shí)現(xiàn)方法與使用技巧,需要的朋友可以參考下2017-11-11
python之openpyxl模塊的安裝和基本用法(excel管理)
這篇文章主要給大家介紹了關(guān)于python之openpyxl模塊的安裝和基本用法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
Python判斷和循環(huán)語句的分析與應(yīng)用
判斷語句是用來篩選條件,過濾條件的。循環(huán)語句是用來解決重復(fù)性代碼的問題,提高工作效率。今天的知識點(diǎn)不多,耐心看完吧2022-07-07

