日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

pytorch基本数学运算:加法 减法 乘法 除法 指数 对数 绝对值

發布時間:2024/10/8 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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基本数学运算:加法 减法 乘法 除法 指数 对数 绝对值的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。