pytorch基本数学运算:加法 减法 乘法 除法 指数 对数 绝对值
生活随笔
收集整理的這篇文章主要介紹了
pytorch基本数学运算:加法 减法 乘法 除法 指数 对数 绝对值
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
加法
import torch import numpy as npprint('# 加法') a = torch.Tensor(np.arange(6).reshape(2, 3)) b = torch.Tensor(np.arange(6).reshape(2, 3)) res = torch.add(a, b) print(a) ''' tensor([[0., 1., 2.],[3., 4., 5.]]) ''' print(b) ''' tensor([[0., 1., 2.],[3., 4., 5.]]) ''' print(res) ''' tensor([[ 0., 2., 4.],[ 6., 8., 10.]]) '''減法
print('# 減法') a = torch.Tensor(np.arange(6).reshape(2, 3)) b = torch.Tensor(np.random.randint(0, 9, size=(2, 3))) res = torch.sub(a, b) print(a) ''' tensor([[0., 1., 2.],[3., 4., 5.]]) ''' print(b) ''' tensor([[0., 1., 5.],[3., 1., 3.]]) ''' print(res) ''' tensor([[ 0., 0., -3.],[ 0., 3., 2.]]) '''乘法
print('# 乘法') a = torch.Tensor(np.arange(6).reshape(2, 3)) b = torch.Tensor(np.arange(6).reshape(2, 3)) res = torch.mul(a, b) print(a) ''' tensor([[0., 1., 2.],[3., 4., 5.]]) ''' print(b) ''' tensor([[0., 1., 2.],[3., 4., 5.]]) ''' print(res) ''' tensor([[ 0., 1., 4.],[ 9., 16., 25.]]) '''除法
rint('# 除法') a = torch.Tensor(np.arange(6).reshape(2, 3)) b = torch.Tensor(np.random.randint(0, 9, size=(2, 3))) res = torch.div(a, b) print(a) ''' tensor([[0., 1., 2.],[3., 4., 5.]]) ''' print(b) ''' tensor([[8., 6., 8.],[8., 0., 4.]]) ''' print(res) # 當除數為0時,結果為inf ''' tensor([[0.0000, 0.1667, 0.2500],[0.3750, inf, 1.2500]]) '''指數
print('# 以e為底數的指數運算') a = torch.Tensor(np.arange(6).reshape(2, 3)) res = torch.exp(a) # 底為e的指數 print(a) ''' tensor([[0., 1., 2.],[3., 4., 5.]]) ''' print(res) ''' tensor([[ 1.0000, 2.7183, 7.3891],[ 20.0855, 54.5981, 148.4132]]) '''print('# n次冪,n次方') a = torch.randint(0, 9, (2, 3)) b = torch.randint(0, 9, (2, 3)) res = torch.pow(input=a, exponent=b) print(a) ''' tensor([[1, 5, 4],[8, 6, 0]]) ''' print(b) ''' tensor([[8, 8, 3],[8, 5, 8]]) ''' print(res) ''' tensor([[ 1, 390625, 64],[16777216, 7776, 0]]) '''對數
print('# 對數') a = torch.Tensor(np.arange(6).reshape(2, 3)) print(a) ''' tensor([[0., 1., 2.],[3., 4., 5.]]) '''# 計算以e為底的對數 res = torch.log(a) print(res) ''' tensor([[ -inf, 0.0000, 0.6931],[1.0986, 1.3863, 1.6094]]) '''# 計算以2為底的對數 res = torch.log2(a) print(res) ''' tensor([[ -inf, 0.0000, 1.0000],[1.5850, 2.0000, 2.3219]]) '''# 計算以10為底的對數 res = torch.log10(a) print(res) ''' tensor([[ -inf, 0.0000, 0.3010],[0.4771, 0.6021, 0.6990]]) '''# 計算以e為底,a+1的對數 res = torch.log1p(a) print(res) ''' tensor([[0.0000, 0.6931, 1.0986],[1.3863, 1.6094, 1.7918]]) '''絕對值
print('# 絕對值') a = torch.randint(-10, -1, (2, 3)) print(a) ''' tensor([[-5, -8, -3],[-6, -9, -5]]) ''' res = torch.abs(a) print(res) ''' tensor([[5, 8, 3],[6, 9, 5]]) '''總結
以上是生活随笔為你收集整理的pytorch基本数学运算:加法 减法 乘法 除法 指数 对数 绝对值的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 167-PHP 文本分割函数str_sp
- 下一篇: String,StringBuffer,