pytorch torch運(yùn)算操作示例詳解
torch運(yùn)算
基本運(yùn)算
創(chuàng)建張量:
import torch # 直接從列表或數(shù)組創(chuàng)建張量 x = torch.tensor([1, 2, 3])
創(chuàng)建特定值的張量:
# 全零張量 zeros = torch.zeros(3, 3) # 全一張量 ones = torch.ones(3, 3) # 單位張量(對(duì)角線(xiàn)為1,其余為0) eye = torch.eye(3) # 隨機(jī)張量 rand = torch.rand(3, 3) # 從均值為0,標(biāo)準(zhǔn)差為1的正態(tài)分布中抽取的隨機(jī)張量 randn = torch.randn(3, 3)
創(chuàng)建等差數(shù)列張量:
# 從0到10(不包括10)的等差數(shù)列,步長(zhǎng)為2 arange = torch.arange(0, 10, 2)
創(chuàng)建特定間隔的數(shù)列張量:
# 從0到10均勻分布的5個(gè)數(shù) linspace = torch.linspace(0, 10, 5)
2.加法和減法:
# 加法 z = x + y # torch.add(x, y) print(z) # 輸出: tensor([5, 7, 9]) # 減法 z = x - y # torch.sub(x, y) print(z) # 輸出: tensor([-3, -3, -3])
3.乘法和除法:
# 元素乘法 z = x * y # torch.mul(x, y) print(z) # 輸出: tensor([ 4, 10, 18]) # 元素除法 z = x / y # torch.div(x, y) print(z) # 輸出: tensor([0.2500, 0.4000, 0.5000])
4.矩陣乘法:
a = torch.tensor([[1, 2], [3, 4]]) b = torch.tensor([[5, 6], [7, 8]]) # 矩陣乘法 z = torch.matmul(a, b) # 或者 a @ b print(z) # 輸出: tensor([[19, 22], [43, 50]])
形狀操作
1.改變形狀:view()
a = torch.tensor([[1, 2, 3], [4, 5, 6]]) # 改變形狀 b = a.view(3, 2) print(b) # 輸出: # tensor([[1, 2], # [3, 4], # [5, 6]])
自動(dòng)調(diào)整size (參數(shù)-1)
view中一個(gè)參數(shù)指定為-1,代表自動(dòng)調(diào)整這個(gè)維度上的元素個(gè)數(shù),以保證元素的總數(shù)不變。
import torch x1 = torch.arange(0,16) print(x1) #a1: tensor([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]) ------------------------------------------------------------------------------------------------------ x2 = x1.view(-1, 16) x3 = x1.view(-1, 8) x4 = x1.view(-1, 4) x5 = x1.view(-1, 2) print(x2) print(x3) print(x4) print(x5) x2: tensor([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]]) x3: tensor([[ 0, 1, 2, 3, 4, 5, 6, 7], [ 8, 9, 10, 11, 12, 13, 14, 15]]) x4: tensor([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15]]) x5: tensor([[ 0, 1], [ 2, 3], [ 4, 5], [ 6, 7], [ 8, 9], [10, 11], [12, 13], [14, 15]])
2.拼接:
x = torch.tensor([[1, 2], [3, 4]]) y = torch.tensor([[5, 6], [7, 8]]) # 沿著0軸拼接(垂直拼接) z = torch.cat((x, y), dim=0) print(z) # 輸出: # tensor([[1, 2], # [3, 4], # [5, 6], # [7, 8]]) # 沿著1軸拼接(水平拼接) z = torch.cat((x, y), dim=1) print(z) # 輸出: # tensor([[1, 2, 5, 6], # [3, 4, 7, 8]])
3.切片:
a = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # 切片 b = a[:, 1] # 第二列 print(b) # 輸出: tensor([2, 5, 8])
數(shù)學(xué)運(yùn)算
求和:sum()
x = torch.tensor([1, 2, 3, 4]) # 求和 sum_x = torch.sum(x) print(sum_x) # 輸出: tensor(10)
均值:mean()
# 均值 mean_x = torch.mean(x.float()) # 轉(zhuǎn)換為浮點(diǎn)數(shù)類(lèi)型 print(mean_x) # 輸出: tensor(2.5000)
最大值和最小值:max(),min()
# 最大值 max_x = torch.max(x) print(max_x) # 輸出: tensor(4) # 最小值 min_x = torch.min(x) print(min_x) # 輸出: tensor(1)
邏輯運(yùn)算
比較運(yùn)算:
x = torch.tensor([1, 2, 3]) y = torch.tensor([2, 2, 2]) # 大于 print(x > y) # 輸出: tensor([False, False, True]) # 小于 print(x < y) # 輸出: tensor([ True, False, False])
邏輯與、或:
a = torch.tensor([True, False, True]) b = torch.tensor([False, False, True]) # 邏輯與 c = torch.logical_and(a, b) print(c) # 輸出: tensor([False, False, True]) # 邏輯或 c = torch.logical_or(a, b) print(c) # 輸出: tensor([ True, False, True])
常見(jiàn)的高級(jí)操作
廣播機(jī)制:
a = torch.tensor([1, 2, 3]) b = torch.tensor([[1], [2], [3]]) # 廣播機(jī)制 c = a + b print(c) # 輸出: # tensor([[2, 3, 4], # [3, 4, 5], # [4, 5, 6]])
自動(dòng)梯度計(jì)算:
x = torch.tensor([1.0, 2.0, 3.0], requires_grad=True) # 前向傳播 y = x + 2 z = y * y * 2 out = z.mean() # 反向傳播 out.backward() print(x.grad) # 輸出: tensor([4.6667, 6.0000, 7.3333])
隨機(jī)數(shù)生成
從標(biāo)準(zhǔn)正態(tài)分布中生成隨機(jī)張量:
randn_tensor = torch.randn(3, 3) # 生成一個(gè)形狀為 (3, 3) 的隨機(jī)張量,服從標(biāo)準(zhǔn)正態(tài)分布 print(randn_tensor) #tensor([[ 1.2335, -0.3941, 0.8990], # [ 0.0470, -1.2671, 0.3248], # [-0.4062, -0.6862, 0.1314]])
生成隨機(jī)排列:
randperm_tensor = torch.randperm(10) # 生成一個(gè)從 0 到 9 的隨機(jī)排列 print(randperm_tensor) #tensor([2, 0, 5, 1, 8, 6, 3, 4, 7, 9])
生成等差數(shù)列張量:
arange_tensor = torch.arange(0, 10, 2) # 生成從 0 到 10(不包括 10)的等差數(shù)列,步長(zhǎng)為 2 print(arange_tensor) #tensor([0, 2, 4, 6, 8])
到此這篇關(guān)于pytorch torch運(yùn)算操作示例詳解的文章就介紹到這了,更多相關(guān)pytorch torch運(yùn)算內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python神經(jīng)網(wǎng)絡(luò)pytorch中BN運(yùn)算操作自實(shí)現(xiàn)
- python神經(jīng)網(wǎng)絡(luò)學(xué)習(xí)利用PyTorch進(jìn)行回歸運(yùn)算
- pytorch中常用的乘法運(yùn)算及相關(guān)的運(yùn)算符(@和*)
- PyTorch中Tensor的數(shù)據(jù)類(lèi)型和運(yùn)算的使用
- PyTorch使用cpu加載模型運(yùn)算方式
- Pytorch Tensor基本數(shù)學(xué)運(yùn)算詳解
- Pytorch 多維數(shù)組運(yùn)算過(guò)程的索引處理方式
相關(guān)文章
Python實(shí)現(xiàn)wav和pcm的轉(zhuǎn)換方式
這篇文章主要介紹了Python實(shí)現(xiàn)wav和pcm的轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05Python定時(shí)任務(wù)工具之APScheduler使用方式
APScheduler (advanceded python scheduler)是一款Python開(kāi)發(fā)的定時(shí)任務(wù)工具。這篇文章主要介紹了Python定時(shí)任務(wù)工具--APScheduler的使用方式,需要的朋友可以參考下2019-07-07Python基于docker部署的Mysql備份查詢(xún)腳本
這篇文章主要來(lái)和大家分享Python基于docker部署的Mysql備份查詢(xún)的腳本,文中的示例代碼講解詳細(xì),有需要的小伙伴可以跟隨小編一起了解下2024-04-04Python?Django源碼運(yùn)行過(guò)程解析
這篇文章主要介紹了Python?Django源碼運(yùn)行過(guò)程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08python makedirs() 遞歸創(chuàng)建目錄
os.makedirs()函數(shù)用于在Python中遞歸地創(chuàng)建目錄,支持設(shè)置權(quán)限和處理目錄已存在的情況,下面就來(lái)具體介紹一下,感興趣的可以了解一下2024-12-12Python實(shí)現(xiàn)的批量修改文件后綴名操作示例
這篇文章主要介紹了Python實(shí)現(xiàn)的批量修改文件后綴名操作,涉及Python目錄文件的遍歷、重命名等相關(guān)操作技巧,需要的朋友可以參考下2018-12-12