網(wǎng)站首頁 編程語言 正文
一、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, 自動匹配個(gè)數(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é)
原文鏈接:https://blog.csdn.net/xu624735206/article/details/123929030
相關(guān)推薦
- 2021-12-13 linux系統(tǒng)AutoFs自動掛載服務(wù)安裝配置_Linux
- 2022-05-23 Android利用Palette實(shí)現(xiàn)提取圖片顏色_Android
- 2022-05-10 怎樣理解單項(xiàng)數(shù)據(jù)流
- 2022-11-16 python中如何使用函數(shù)改變list_python
- 2022-05-13 Ubuntu配置多版本Python運(yùn)行環(huán)境
- 2022-08-01 Qt實(shí)現(xiàn)矩形大小任意縮放的示例代碼_C 語言
- 2022-07-23 深度解析C語言中數(shù)據(jù)的存儲_C 語言
- 2022-07-31 python實(shí)現(xiàn)學(xué)員管理系統(tǒng)(面向?qū)ο蟀?_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- 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)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支