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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

PyTorch 笔记(05)— Tensor 基本运算(torch.abs、torch.add、torch.clamp、torch.div、torch.mul、torch.pow等)

發布時間:2023/11/27 生活经验 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PyTorch 笔记(05)— Tensor 基本运算(torch.abs、torch.add、torch.clamp、torch.div、torch.mul、torch.pow等) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. 函數匯總

Tensor 的基本運算會對 tensor 的每一個元素進行操作,此類操作的輸入與輸出形狀一致,常用操作見下表所示。

對于很多操作,例如 divmulpowfmod、等, PyTorch 都實現了運算符重載,所以可以直接使用運算符,如 a ** 2 等價于 torch.pow(a, 2), a *2 等價于 torch.mul(a, 2)

2. 函數功能

2.1 torch.abs

將參數傳遞到 torch.abs 后返回輸入參數的絕對值作為輸出,輸入參數必須是一個 Tensor 數據類型的變量。

import torch
a = torch.randn(2, 3)
print a
b = torch.abs(a)
print b

輸出結果:

tensor([[ 0.4873,  0.8172,  0.2003],[ 0.6815,  1.5656, -0.8303]])
tensor([[0.4873, 0.8172, 0.2003],[0.6815, 1.5656, 0.8303]])

2.2 torch.add

將參數傳遞到 torch.add 后返回輸入參數的求和結果作為輸出,輸入參數既可以全部是 Tensor 數據類型的變量,也可以一個是 Tensor 數據類型的變量,另一個是標量。

a = torch.ones([2, 3])
print a
b = torch.add(a, 10)
print b
c = torch.randn([2, 3])
print c
d = torch.add(a, c)
print d

輸出結果:

tensor([[1., 1., 1.],[1., 1., 1.]])
tensor([[11., 11., 11.],[11., 11., 11.]])
tensor([[ 0.4276,  0.4618, -0.9449],[-1.4758, -1.3118, -0.2859]])
tensor([[ 1.4276,  1.4618,  0.0551],[-0.4758, -0.3118,  0.7141]])

2.3 torch.clamp

對輸入參數按照自定義的范圍進行裁剪,最后將參數裁剪的結果作為輸出。所以輸入參數一共有三個,分別是需要進行裁剪的 Tensor 數據類型的變量、裁剪的上邊界和裁剪的下邊界。

具體的裁剪過程是:使用變量中的每個元素分別和裁剪的上邊界及裁剪的下邊界的值進行比較,如果元素的值小于裁剪的下邊界的值,該元素就被重寫成裁剪的下邊界的值;同理,如果元素的值大于裁剪的上邊界的值,該元素就被重寫成裁剪的上邊界的值。

a = torch.randn(2,3)
print a
b = torch.clamp(a, -0.1, 0.1)
print b

輸出結果:

tensor([[-1.0129, -1.3094,  0.4644],[ 1.0542, -0.5259, -0.3575]])
tensor([[-0.1000, -0.1000,  0.1000],[ 0.1000, -0.1000, -0.1000]])

2.4 torch.div

將參數傳遞到 torch.div 后返回輸入參數的求商結果作為輸出,同樣,參與運算的參數可以全部是 Tensor 數據類型的變量,也可以是 Tensor 數據類型的變量和標量的組合。

a = torch.IntTensor([2,4,6,8,10])
print a
b = torch.div(a, 2)
print b
c = torch.IntTensor([2,4,6,8,10])
d = torch.div(a, c)
print d

輸出結果:

tensor([ 2,  4,  6,  8, 10], dtype=torch.int32)
tensor([1, 2, 3, 4, 5], dtype=torch.int32)
tensor([1, 1, 1, 1, 1], dtype=torch.int32)

2.5 torch.mul

將參數傳遞到 torch.mul 后返回輸入參數求積的結果作為輸出,參與運算的參數可以全部是 Tensor 數據類型的變量,也可以是 Tensor 數據類型的變量和標量的組合。

a = torch.IntTensor([2,4,6,8,10])
print a
b = torch.mul(a, 2)
print b
c = torch.IntTensor([2,4,6,8,10])
d = torch.mul(a, c)
print d

輸出結果:

tensor([ 2,  4,  6,  8, 10], dtype=torch.int32)
tensor([ 4,  8, 12, 16, 20], dtype=torch.int32)
tensor([  4,  16,  36,  64, 100], dtype=torch.int32)

2.6 torch.pow

將參數傳遞到 torch.pow 后返回輸入參數的求冪結果作為輸出,參與運算的參數可以全部是 Tensor 數據類型的變量,也可以是 Tensor 數據類型的變量和標量的組合。

a = torch.IntTensor([2,4,6,8,10])
print a
b = torch.pow(a, 2)
print b
c = torch.IntTensor([3,3,3,3,3])
d = torch.pow(a, c)
print d

輸出結果:

tensor([ 2,  4,  6,  8, 10], dtype=torch.int32)
tensor([  4,  16,  36,  64, 100], dtype=torch.int32)
tensor([   8,   64,  216,  512, 1000], dtype=torch.int32)

2.7 torch.mm

將參數傳遞到 torch.mm 后返回輸入參數的求積結果作為輸出,不過這個求積的方式和之前的 torch.mul 運算方式不太一樣, torch.mm 運用矩陣之間的乘法規則進行計算,所以被傳入的參數會被當作矩陣進行處理,參數的維度自然也要滿足矩陣乘法的前提條件,即前一個矩陣的行數必須和后一個矩陣的列數相等,否則不能進行計算。

a = torch.IntTensor([[1,2,3], [4,5,6]])
print a
b = torch.IntTensor([[1,2], [4,5], [7, 8]])
print b
c = torch.mm(a, b)
print c

輸出結果:

tensor([[1, 2, 3],[4, 5, 6]], dtype=torch.int32)
tensor([[1, 2],[4, 5],[7, 8]], dtype=torch.int32)
tensor([[30, 36],[66, 81]], dtype=torch.int32)

2.8 torch.mv

將參數傳遞到 torch.mv 后返回輸入參數的求積結果作為輸出,torch.mv 運用矩陣與向量之間的乘法規則進行計算,被傳入的參數中的第 1 個參數代表矩陣,第 2 個參數代表向量,順序不能顛倒。

a = torch.IntTensor([[1,0,0], [0,0,1]])
print a
b = torch.IntTensor([3,3,3])
print b
c = torch.mv(a, b)
print c

輸出結果:

tensor([[1, 0, 0],[0, 0, 1]], dtype=torch.int32)
tensor([3, 3, 3], dtype=torch.int32)
tensor([3, 3], dtype=torch.int32)

總結

以上是生活随笔為你收集整理的PyTorch 笔记(05)— Tensor 基本运算(torch.abs、torch.add、torch.clamp、torch.div、torch.mul、torch.pow等)的全部內容,希望文章能夠幫你解決所遇到的問題。

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