python开方运算符_Pytorch Tensor基本数学运算详解
1. 加法運算
示例代碼:
import torch
# 這兩個Tensor加減乘除會對b自動進行Broadcasting
a = torch.rand(3, 4)
b = torch.rand(4)
c1 = a + b
c2 = torch.add(a, b)
print(c1.shape, c2.shape)
print(torch.all(torch.eq(c1, c2)))
輸出結果:
torch.Size([3, 4]) torch.Size([3, 4])
tensor(1, dtype=torch.uint8)
2. 減法運算
示例代碼:
a = torch.rand(3, 4)
b = torch.rand(4)
c1 = a - b
c2 = torch.sub(a, b)
print(c1.shape, c2.shape)
print(torch.all(torch.eq(c1, c2)))
輸出結果:
torch.Size([3, 4]) torch.Size([3, 4])
tensor(1, dtype=torch.uint8)
3. 哈達瑪積(element wise,對應元素相乘)
示例代碼:
c1 = a * b
c2 = torch.mul(a, b)
print(c1.shape, c2.shape)
print(torch.all(torch.eq(c1, c2)))
輸出結果:
torch.Size([3, 4]) torch.Size([3, 4])
tensor(1, dtype=torch.uint8)
4. 除法運算
示例代碼:
c1 = a / b
c2 = torch.div(a, b)
print(c1.shape, c2.shape)
print(torch.all(torch.eq(c1, c2)))
輸出結果:
torch.Size([3, 4]) torch.Size([3, 4])
tensor(1, dtype=torch.uint8)
5. 矩陣乘法
(1)二維矩陣相乘
二維矩陣乘法運算操作包括torch.mm()、torch.matmul()、@,
示例代碼:
import torch
a = torch.ones(2, 1)
b = torch.ones(1, 2)
print(torch.mm(a, b).shape)
print(torch.matmul(a, b).shape)
print((a @ b).shape)
輸出結果:
torch.Size([2, 2])
torch.Size([2, 2])
torch.Size([2, 2])
(2)多維矩陣相乘
對于高維的Tensor(dim>2),定義其矩陣乘法僅在最后的兩個維度上,要求前面的維度必須保持一致,就像矩陣的索引一樣并且運算操只有torch.matmul()。
示例代碼:
c = torch.rand(4, 3, 28, 64)
d = torch.rand(4, 3, 64, 32)
print(torch.matmul(c, d).shape)
輸出結果:
torch.Size([4, 3, 28, 32])
注意,在這種情形下的矩陣相乘,前面的"矩陣索引維度"如果符合Broadcasting機制,也會自動做廣播,然后相乘。
示例代碼:
c = torch.rand(4, 3, 28, 64)
d = torch.rand(4, 1, 64, 32)
print(torch.matmul(c, d).shape)
輸出結果:
torch.Size([4, 3, 28, 32])
6. 冪運算
示例代碼:
import torch
a = torch.full([2, 2], 3)
b = a.pow(2) # 也可以a**2
print(b)
輸出結果:
tensor([[9., 9.],
[9., 9.]])
7. 開方運算
示例代碼:
c = b.sqrt() # 也可以a**(0.5)
print(c)
d = b.rsqrt() # 平方根的倒數
print(d)
輸出結果:
tensor([[3., 3.],
[3., 3.]])
tensor([[0.3333, 0.3333],
[0.3333, 0.3333]])
8.指數與對數運算
注意log是以自然對數為底數的,以2為底的用log2,以10為底的用log10
示例代碼:
import torch
a = torch.exp(torch.ones(2, 2)) # 得到2*2的全是e的Tensor
print(a)
print(torch.log(a)) # 取自然對數
輸出結果:
tensor([[2.7183, 2.7183],
[2.7183, 2.7183]])
tensor([[1., 1.],
[1., 1.]])
9.近似值運算
示例代碼:
import torch
a = torch.tensor(3.14)
print(a.floor(), a.ceil(), a.trunc(), a.frac()) # 取下,取上,取整數,取小數
b = torch.tensor(3.49)
c = torch.tensor(3.5)
print(b.round(), c.round()) # 四舍五入
輸出結果:
tensor(3.) tensor(4.) tensor(3.) tensor(0.1400)
tensor(3.) tensor(4.)
10. 裁剪運算
即對Tensor中的元素進行范圍過濾,不符合條件的可以把它變換到范圍內部(邊界)上,常用于梯度裁剪(gradient clipping),即在發生梯度離散或者梯度爆炸時對梯度的處理,實際使用時可以查看梯度的(L2范數)模來看看需不需要做處理:w.grad.norm(2)。
示例代碼:
import torch
grad = torch.rand(2, 3) * 15 # 0~15隨機生成
print(grad.max(), grad.min(), grad.median()) # 最大值最小值平均值
print(grad)
print(grad.clamp(10)) # 最小是10,小于10的都變成10
print(grad.clamp(3, 10)) # 最小是3,小于3的都變成3;最大是10,大于10的都變成10
輸出結果:
tensor(14.7400) tensor(1.8522) tensor(10.5734)
tensor([[ 1.8522, 14.7400, 8.2445],
[13.5520, 10.5734, 12.9756]])
tensor([[10.0000, 14.7400, 10.0000],
[13.5520, 10.5734, 12.9756]])
tensor([[ 3.0000, 10.0000, 8.2445],
[10.0000, 10.0000, 10.0000]])
以上這篇Pytorch Tensor基本數學運算詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持我們。
本文標題: Pytorch Tensor基本數學運算詳解
本文地址: http://www.cppcns.com/jiaoben/python/296122.html
總結
以上是生活随笔為你收集整理的python开方运算符_Pytorch Tensor基本数学运算详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 云电脑密码是怎么样弄(云电脑怎么设置密码
- 下一篇: python cmd闪退_使用cmd p