歡迎關(guān)注『youcans的深度學(xué)習(xí)』系列,持續(xù)更新中…
【youcans的深度學(xué)習(xí) 01】安裝環(huán)境之 miniconda
【youcans的深度學(xué)習(xí) 02】PyTorch CPU版本安裝與環(huán)境配置
【youcans的深度學(xué)習(xí) 03】PyTorch CPU版本安裝與環(huán)境配置
【youcans的深度學(xué)習(xí) 04】PyTorch入門教程:基礎(chǔ)知識(shí)
【youcans的深度學(xué)習(xí) 05】PyTorch入門教程:快速入門
【youcans的深度學(xué)習(xí) 06】PyTorch入門教程:張量的基本操作 1
【youcans的深度學(xué)習(xí) 07】PyTorch入門教程:張量的基本操作 2
【youcans的深度學(xué)習(xí) 08】PyTorch入門教程:張量的就地操作和廣播機(jī)制
【youcans的深度學(xué)習(xí) 09】PyTorch入門教程:張量的逐點(diǎn)運(yùn)算
【youcans的深度學(xué)習(xí) 10】PyTorch入門教程:張量的統(tǒng)計(jì)運(yùn)算與比較運(yùn)算
PyTorch 中支持 100 多種張量操作,包括轉(zhuǎn)置、索引、切?、數(shù)學(xué)運(yùn)算、線性代數(shù)、隨機(jī)數(shù)等等,詳見【PyTorch官方文檔】。
3. 張量的統(tǒng)計(jì)分析運(yùn)算
張量的歸約運(yùn)算,是對(duì)張量進(jìn)行某種聚合總結(jié),而得到計(jì)算結(jié)果的函數(shù)。規(guī)約運(yùn)算主要包含數(shù)據(jù)科學(xué)領(lǐng)域內(nèi)的諸多統(tǒng)計(jì)分析函數(shù),如均值、極值、方差、中位數(shù)函數(shù)等等。
3.1 均值、方差與求和
函數(shù)原型:
torch.sum(input, dim, keepdim=False, *, dtype=None) → Tensor # 張量求和
torch.mean(input, dim, keepdim=False, *, dtype=None, out=None) → Tensor # 張量的均值
torch.nanmean(input, dim=None, keepdim=False, *, dtype=None) → Tensor # 非Nan元素的均值
torch.var(input, dim=None, *, correction=1, keepdim=False, out=None) → Tensor # 張量的方差
torch.std(input, dim=None, *, correction=1, keepdim=False, out=None) → Tensor # 張量的標(biāo)準(zhǔn)差
torch.var_mean(input, dim=None, *, correction=1, keepdim=False, out=None) # 張量的方差和均值
參數(shù)說明:
- input,張量,輸入張量。
- dim,整數(shù)或整型元組,指定維度。
- out,張量,輸出張量。
- keepdim,布爾值,輸出張量是否保留 dim,可選項(xiàng),默認(rèn)為 False
- correction,整數(shù),可選項(xiàng),默認(rèn)值為 1 表示 Bessel 校正
函數(shù)說明:
(1)函數(shù)返回張量沿指定維度 dim 的所有元素的均值、方差與求和。參數(shù) dim 缺省時(shí),函數(shù)默認(rèn)對(duì)張量的所有元素進(jìn)行計(jì)算,返回 0 維張量。參數(shù) dim 為整數(shù)或整型元組時(shí),按照 dim 指定維度的所有元素進(jìn)行計(jì)算。
(2)函數(shù) torch.var_mean、torch.std_mean 返回一個(gè)元組,元組的元素分別是方差和均值。
(3)函數(shù) torch.nanmean 返回張量沿指定維度的非Nan元素的平均值。
# (10) 張量的統(tǒng)計(jì)分析運(yùn)算:均值、方差與求和
# 張量求和
x = torch.arange(1, 16).reshape(3, 5) # torch.Size([3, 5])
print(x)
s1 = torch.sum(x) # 張量所有元素求和
s2 = torch.sum(x, dim=0) # 按指定dim求和
s3 = torch.sum(x, dim=1) # 按指定dim求和
print("sum(x):", s1) # torch.Size([])
print("sum(x,0):", s2) # torch.Size([5])
print("sum(x,1):", s3) # torch.Size([3])
print(s1.shape, s2.shape, s3.shape)
# tensor([[ 1, 2, 3, 4, 5],
# [ 6, 7, 8, 9, 10],
# [11, 12, 13, 14, 15]])
# sum(x): tensor(120)
# sum(x,0): tensor([18, 21, 24, 27, 30])
# sum(x,1): tensor([15, 40, 65])
# torch.Size([]) torch.Size([5]) torch.Size([3])
# 張量均值
x = torch.arange(1.0, 16.0).reshape(3,5) # torch.Size([3, 5])
m1 = torch.mean(x) # 張量所有元素求均值
m2 = torch.mean(x, dim=0) # 按指定dim求均值
m3 = torch.mean(x, dim=1) # 按指定dim求均值
print("mean(x):", m1) # torch.Size([])
print("mean(x,0):", m2) # torch.Size([5])
print("mean(x,1):", m3) # torch.Size([3])
print(m1.shape, m2.shape, m3.shape)
# mean(x): tensor(8.)
# mean(x,0): tensor([ 6., 7., 8., 9., 10.])
# mean(x,1): tensor([ 3., 8., 13.])
# torch.Size([]) torch.Size([5]) torch.Size([3])
# 張量方差
x = torch.arange(1.0, 16.0).reshape(3,5) # torch.Size([3, 5])
v1 = torch.var(x) # 張量所有元素求方差
v2 = torch.var(x, dim=0) # 按指定dim求方差
v3 = torch.var(x, dim=1) # 按指定dim求方差
print("var(x):", v1) # torch.Size([])
print("var(x,0):", v2) # torch.Size([5])
print("var(x,1):", v3) # torch.Size([3])
print(v1.shape, v2.shape, v3.shape)
# var(x): tensor(20.)
# var(x,0): tensor([25., 25., 25., 25., 25.])
# var(x,1): tensor([2.5000, 2.5000, 2.5000])
# torch.Size([]) torch.Size([5]) torch.Size([3])
# 張量標(biāo)準(zhǔn)差
x = torch.arange(1.0, 16.0).reshape(3,5) # torch.Size([3, 5])
s1 = torch.std(x) # 張量所有元素求標(biāo)準(zhǔn)差
s2 = torch.std(x, dim=0) # 按指定dim求標(biāo)準(zhǔn)差
s3 = torch.std(x, dim=1) # 按指定dim求標(biāo)準(zhǔn)差
print("std(x):", s1) # torch.Size([])
print("std(x,0):", s2) # torch.Size([5])
print("std(x,1):", s3) # torch.Size([3])
print(s1.shape, s2.shape, s3.shape)
# std(x): tensor(4.4721)
# std(x,0): tensor([5., 5., 5., 5., 5.])
# std(x,1): tensor([1.5811, 1.5811, 1.5811])
# torch.Size([]) torch.Size([5]) torch.Size([3])
3.2 最大值與最小值
函數(shù)原型:
torch.max(input, dim, keepdim=False, *, out=None) → out # 張量的最大值
torch.max(input, other, *, out=None) → out # 兩個(gè)張量的最大值
torch.min(input, dim, keepdim=False, *, out=None) → out # 張量的最小值
torch.min(input, other, *, out=None) → out # 兩個(gè)張量的最小值
torch.argmax(input, dim, keepdim=False) → Tensor # 張量的最大值索引
torch.argmin(input, dim, keepdim=False) → Tensor # 張量的最小值索引
torch.topk(input, k, dim=None, largest=True, sorted=True, *, out=None) # k 個(gè)最大/小值
torch.median(input, dim, keepdim=False, *, out=None) → out # 張量的中值
參數(shù)說明:
- input,張量,輸入張量。
- dim,整數(shù)或整型元組,指定維度。
- out,張量,輸出張量。
- keepdim,布爾值,輸出張量是否保留 dim,可選項(xiàng),默認(rèn)為 False
- other,張量,另一個(gè)輸入張量,形狀與 input 相同
- k,整數(shù),top-k 的 k 值
- largest,布爾值,控制返回最大值或最小值
- sorted,布爾值,控制返回元素是否排序
函數(shù)說明:
(1)函數(shù)返回張量沿指定維度 dim 的所有元素的最大值或最小值。
(2)參數(shù) dim 缺省時(shí),函數(shù)默認(rèn)對(duì)張量的所有元素進(jìn)行計(jì)算,返回 0 維張量。參數(shù) dim 為整數(shù)或整型元組時(shí),按照 dim 指定維度的所有元素進(jìn)行計(jì)算,返回值是帶有 2 個(gè) key( values, indices)的字典。
(3)函數(shù) torch.topk 的返回值是帶有 2 個(gè) key( values, indices)的字典。
(4)對(duì)于具有偶數(shù)個(gè)元素的輸入張量,中值不是唯一的,函數(shù) torch.median 返回兩個(gè)中值中較小的一個(gè)。
# (11) 張量的統(tǒng)計(jì)分析運(yùn)算: 最大值與最大值索引
# 張量最大值
x = torch.arange(1.0, 16.0).reshape(3, 5) # torch.Size([3, 5])
max1 = torch.max(x) # 張量所有元素求最大值
print("max(x):", max1) # torch.Size([])
# max(x): tensor(15.)
max2 = torch.max(x, dim=0) # 按指定dim求最大值
print(max2) # 返回值是字典, 有兩個(gè)key: values和indices
# torch.return_types.max(
# values=tensor([11., 12., 13., 14., 15.]),
# indices=tensor([2, 2, 2, 2, 2]))
print(max2.values) # torch.Size([5])
# tensor([11., 12., 13., 14., 15.])
print(max2.indices) # torch.Size([5])
# tensor([2, 2, 2, 2, 2])
# 兩個(gè)張量的最大值
a = torch.tensor((1, 2, -1))
b = torch.tensor((3, 0, 6))
print(torch.max(a, b)) # tensor([3, 2, 6])
print(torch.maximum(a, b)) # tensor([3, 2, 6])
# 張量的最大值索引
x = torch.arange(1.0, 16.0).reshape(3, 5) # torch.Size([3, 5])
arg1 = torch.argmax(x) # 張量所有元素求最大值索引
print("argmax(x):", arg1) # torch.Size([])
# argmax(x): tensor(14)
arg2 = torch.argmax(x, dim=0) # 按指定dim求最大值
print("argmax(x,0):", arg2) # torch.Size([5])
# argmax(x,0): tensor([2, 2, 2, 2, 2])
arg3 = torch.argmax(x, dim=1) # 按指定dim求最大值
print("argmax(x,1):", arg3) # torch.Size([3])
# argmax(x,1): tensor([4, 4, 4])
# 返回 k 個(gè)最大值
x = torch.arange(1.0, 10.0) # torch.Size([3, 5])
x_topk = torch.topk(x, k=2) # 返回值是字典, 有兩個(gè)key: values和indices
print(x_topk)
# torch.return_types.topk(
# values=tensor([9., 8.]),
# indices=tensor([8, 7]))
print(x_topk.values) # tensor([9., 8.])
print(x_topk.indices) # tensor([8, 7])
3.3 累積乘與累積和
函數(shù)原型:
torch.cumprod(input, dim, *, dtype=None, out=None) → Tensor # 張量的累積乘
torch.cumsum(input, dim, *, dtype=None, out=None) → Tensor # 張量的累積和
張量累積乘/累積和函數(shù),返回維度dim中輸入元素的累積乘積/累積和。如果輸入是大小為N的向量,則結(jié)果也將是大小為N的向量,其中包含元素。
# (12) 張量的統(tǒng)計(jì)分析運(yùn)算: 累積乘與累積和
x = torch.arange(1.0, 6.0)
x_cumprod = torch.cumprod(x, dim=0)
x_cumsum = torch.cumsum(x, dim=0)
print("x_cumprod:", x_cumprod)
# x_cumprod: tensor([ 1., 2., 6., 24., 120.])
print("x_cumsum:", x_cumsum)
# x_cumsum: tensor([ 1., 3., 6., 10., 15.])
4. 張量的比較操作
張量的比較操作,是指對(duì)多個(gè)張量進(jìn)行比較運(yùn)算的方法。
函數(shù)原型:
torch.equal(input, other) → bool # 比較兩個(gè)張量是否相等
torch.eq(input, other, *, out=None) → Tensor # 比較兩個(gè)張量的元素是否相同
torch.ne(input, other, *, out=None) → Tensor # 比較兩個(gè)張量的元素是否相同
torch.gt(input, other, *, out=None) → Tensor # 比較兩個(gè)張量的元素是否滿足大于關(guān)系
torch.ge(input, other, *, out=None) → Tensor # 比較兩個(gè)張量的元素是否滿足大于等于關(guān)系
torch.lt(input, other, *, out=None) → Tensor # 比較兩個(gè)張量的元素是否滿足小于關(guān)系
torch.le(input, other, *, out=None) → Tensor # 比較兩個(gè)張量的元素是否滿足小于等于關(guān)系
torch.isnan(input) → Tensor # 檢查張量的元素是否為NaN
torch.sort(input, dim=-1, descending=False, stable=False, *, out=None) # 對(duì)張量的元素按升序排列
參數(shù)說明:
- input,張量,輸入張量。
- other,張量或浮點(diǎn)數(shù),第二個(gè)輸入張量。
- out,張量,輸出張量,布爾類型。
函數(shù)說明:
(1)函數(shù) torch.equal 比較兩個(gè)張量是否相等,結(jié)果是一個(gè)布爾值。函數(shù) torch.eq 比較兩個(gè)張量的逐個(gè)元素,結(jié)果是一個(gè)布爾型張量。
(2)函數(shù) torch.gt、torch.ge、torch.lt、torch.le 逐個(gè)元素比較張量 input 與 other 的大小,結(jié)果是一個(gè)布爾型張量。
(3)函數(shù) torch.sort 將輸入張量的元素沿給定維度按值升序排列,返回值是帶有 2 個(gè) key( values, indices)的字典。
# (13) 張量的比較操作
x = torch.tensor([[1,2,3],[4,5,6]])
y = torch.tensor([[1,2,3],[4,5,6]])
z = torch.tensor([[0,2,3],[0,5,6]])
# 比較兩個(gè)張量是否相等
x_equal_y = torch.equal(x, y)
x_equal_z = torch.equal(x, z)
print("x equal y:", x_equal_y) # x equal y: True
print("x equal z:", x_equal_z) # x equal z: False
# 比較兩個(gè)張量的逐個(gè)元素是否相等
x_eq_y = torch.eq(x, y)
x_eq_z = torch.eq(x, z)
print("x eq y:", x_eq_y)
# x eq y: tensor([[True, True, True], [True, True, True]])
print("x eq z:", x_eq_z)
# x eq z: tensor([[False, True, True], [False, True, True]])
# 檢查張量元素的性質(zhì)
x = torch.tensor([1, float('nan'), 2])
print("isnan(x):", torch.isnan(x)) # 檢查是否為 NaN
# isnan(x): tensor([False, True, False])
print("isfinite(x):", torch.isfinite(x)) # 檢查是否為有界數(shù)值
# isfinite(x): tensor([ True, False, True])
print("isinf(x):", torch.isinf(x)) # 檢查是否為無界數(shù)值
# isinf(x): tensor([False, False, False])
# 對(duì)張量的元素排序
x = torch.tensor([[1, 4, 7], [3, 0, 6]])
print(x)
print(torch.sort(x))
# torch.return_types.sort(
# values=tensor([[1, 4, 7], [0, 3, 6]]),
# indices=tensor([[0, 1, 2], [1, 0, 2]]))
print(torch.sort(x, dim=0)) #
# torch.return_types.sort(
# values=tensor([[1, 0, 6], [3, 4, 7]]),
# indices=tensor([[0, 1, 1], [1, 0, 0]]))
print(torch.sort(x, dim=1)) #
# torch.return_types.sort(
# values=tensor([[1, 4, 7], [0, 3, 6]]),
# indices=tensor([[0, 1, 2], [1, 0, 2]]))
5. 張量的線性代數(shù)運(yùn)算
BLAS(Basic Linear Algeria Subprograms)和 LAPACK(Linear Algeria Package)模塊提供了完整的線性代數(shù)基本方法,包括:文章來源:http://www.zghlxwxcb.cn/news/detail-474232.html
- 矩陣的形變及特殊矩陣的構(gòu)造方法:包括矩陣的轉(zhuǎn)置、對(duì)角矩陣的創(chuàng)建、單位矩陣的創(chuàng)建、上/下三角矩陣的創(chuàng)建等;
- 矩陣的基本運(yùn)算:包括矩陣乘法、向量內(nèi)積、矩陣和向量的乘法等,當(dāng)然,此處還包含了高維張量的基本運(yùn)算,將著重探討矩陣的基本方法
- 運(yùn)算拓展至三維張量中的基本方法;
- 矩陣的線性代數(shù)運(yùn)算:包括矩陣的跡、矩陣的秩、逆矩陣的求解、伴隨矩陣和廣義逆矩陣等;
- 矩陣分解運(yùn)算:特征分解、奇異值分解和SVD分解等。
版權(quán)聲明:
歡迎關(guān)注『youcans的深度學(xué)習(xí)』系列,轉(zhuǎn)發(fā)請(qǐng)注明原文鏈接:
【youcans的深度學(xué)習(xí) 10】PyTorch入門教程:張量的統(tǒng)計(jì)運(yùn)算(https://youcans.blog.csdn.net/article/details/130745989)
Copyright 2023 youcans, XUPT
Crated:2023-05-18文章來源地址http://www.zghlxwxcb.cn/news/detail-474232.html
到了這里,關(guān)于【youcans的深度學(xué)習(xí) 10】PyTorch入門教程:張量的統(tǒng)計(jì)運(yùn)算與比較運(yùn)算的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!