502 字
3 分钟
pytorch学习10:统计运算
范数 norm
import torch
a = torch.ones(8)b = torch.ones(2, 4)c = torch.ones(2, 3, 4)
print("a:\n{}".format(a))print("b:\n{}".format(b))print("c:\n{}".format(c))
# norm1 = sum(|x|)print("a.norm(1): {}, b.norm(1): {}, c.norm(1): {}".format( a.norm(1), b.norm(1), c.norm(1)))
# norm2 = sqrt(sum(x**2))print("a.norm(2): {}\nb.norm(2): {}\nc.norm(2): {}".format( a.norm(2), b.norm(2), c.norm(2)))
print("b.norm(1, dim=1): ", b.norm(1, dim=1))print("b.norm(2, dim=1): ", b.norm(2, dim=1))print("c.norm(1, dim=0): \n", c.norm(1, dim=0))print("c.norm(2, dim=0): \n", c.norm(2, dim=1))
- dim指定哪个维度就会在哪个维度上求范数
- 如:[2, 3, 4]
- dim = 0 → [3, 4]
- dim = 1 → [2, 4]
- dim = 2 → [2, 3]
平均、求和、最小、最大、累乘
import torch
a = torch.arange(8).view(2, 4).float() + 1print("a:\n{}\n".format(a))
# 最小值print("a.min(): ", a.min())# 最小值print("a.max(): ", a.max())# 平均值print("a.mean(): ", a.mean())# 累乘print("a.prod(): ", a.prod())# 求和print("a.sum(): ", a.sum())# 最大值的索引# 若不带参数,argmax和argmin会把矩阵打平print("a.argmax(): ", a.argmax())# 最小值的索引print("a.argmin(): ", a.argmin())
argmin、argmax
import torch
a = torch.arange(8).view(2, 4).float() + 1print("a:\n{}\n".format(a))
print("a.argmax(): ",a.argmax())# dim 指定在哪一位上取最大值或最小值索引print("a.argmax(dim=0): ", a.argmax(dim=0))print("a.argmin(dim=1): ", a.argmin(dim=1))
dim和keepdim
import torch
a = torch.randn(4, 10)print("a.max(dim=1):\n{}\n".format(a.max(dim=1)))print("a.argmax(dim=1): ", a.argmax(dim=1), "\n")
# keepdim=True 会使结果维数不变# 有时可以方便运算print("a.max(dim=1, keepdim=True):\n{}\n".format( a.max(dim=1, keepdim=True)))
print("a.argmax(dim=0, keepdim=True):\n{}\n".format( a.argmax(dim=0, keepdim=True)))
top-k 和 k-th
import torch
a = torch.randn(4, 10)# 在第二维最大前三个print("a.topk(3, dim=1):\n{}\n".format(a.topk(3, dim=1)))# 在第二维取最小前三个print("a.topk(3, dim=1, largest=False):\n{}\n".format( a.topk(3, dim=1, largest=False)))
# 在第二维上取第八大的值print("a.kthvalue(8, dim=1):\n{}\n".format( a.kthvalue(8, dim=1)))
# 在第一维上取第三大的值print("a.kthvalue(3, dim=0):\n{}\n".format( a.kthvalue(3, dim=0)))
比较运算
import torch
a = torch.arange(-4, 4).view(2, 4)
print("a:\n{}\n".format(a))print("a>0:\n{}\n".format(a>0))# gt -> greater 大于print("torch.equal(a>0, torch.gt(a, 0)):", torch.equal(a>0, torch.gt(a, 0)))
print("a==0:\n{}\n".format(a==0))# eq(a, b) 会返回一个矩阵,矩阵每个元素表示该位置的元素是否相等# equal(a, b) 会返回一个 布尔值,表示两矩阵是否完全相等print("torch.equal(a==0, torch.eq(a, 0)):", torch.equal(a==0, torch.eq(a, 0)))
print("a>=0:\n{}\n".format(a>=0))# ge greater or equalprint("torch.equal(a>=0, torch.ge(a, 0)):", torch.equal(a>=0, torch.ge(a, 0)))
- 小于和小于等于可直接使用符号
< <=或在ge gt时将矩阵反转
原文链接:CSDN
pytorch学习10:统计运算
https://blog.homura.work/posts/csdn/119360285/