一、数学运算
在PyTorch中,定义了数学运算,如:add加、sub减、div除、mul乘
import torch
import numpy as np
def detect_gpu():
print(torch.__version__)
print(torch.cuda.is_available())
def test():
detect_gpu()
a = torch.rand(3, 4)
b = torch.rand(4)
print(torch.eq(a + b, torch.add(a, b)))
print(torch.eq(a - b, torch.sub(a, b)))
print(torch.eq(a * b, torch.mul(a, b)))
print(torch.eq(a / b, torch.div(a, b)))
if __name__ == '__main__':
test()
1、matmul(按矩阵形式相乘)
矩阵形式相乘方法:
- torch.mm :only for 2D
- torch.matmul:推荐使用,对于多维度也适用
- @ :matmul的简洁表示方式
- 多维度tensor相乘,如果出现后两维度之前的维度不一致,使用broadcasting进行维度自动扩展
import torch
import numpy as np
def detect_gpu():
print(torch.__version__)
print(torch.cuda.is_available())
#降低维度案例——》784-512
def test():
detect_gpu()
# 对tensor降维
a = torch.rand(4, 1, 28, 28)
b = a.view(4, 1*28*28)
print("a.shape: {0}, b.shape: {1}".format(a.shape, b.shape))
#创建一个tensor,用于降低维度使用
w = torch.rand(512, 784) #(output, input)
print((b @ w.t()).shape)
#多维度tensor运算
def tensor_matmul():
a = torch.rand(4, 3, 28, 64)
b = torch.rand(4, 3, 64, 32)
#多维tensor运算 怎么算?
print(torch.matmul(a, b).shape)
#多维度tensor相乘,如果出现后两维度之前的维度不一致,使用broadcasting进行维度自动扩展
c = torch.rand(4, 1, 64, 32)
print(torch.matmul(a,c).shape)
if __name__ == '__main__':
test()
tensor_matmul()
2、pow 次方运算
import torch
import numpy as np
def detect_gpu():
print(torch.__version__)
print(torch.cuda.is_available())
#降低维度案例——》784-512
def test():
detect_gpu()
a = torch.full([2,2], 3)
b = a.pow(2) #平方
c = a**2
print(torch.all(torch.eq(b, c)))
#开方
print(a.sqrt())
print(a.rsqrt()) #开方得到数值的倒数
if __name__ == '__main__':
test()
3、exp(以e为底的幂函数)、log
import torch
import numpy as np
def detect_gpu():
print(torch.__version__)
print(torch.cuda.is_available())
#降低维度案例——》784-512
def test():
detect_gpu()
a = torch.exp(torch.ones(2, 2)) #exp
print(a)
print(torch.log(a)) #log
if __name__ == '__main__':
test()
4、Approximation(近似值)
- floor() 、ceil():floor向下取整、ceil向上取整
- round():四舍五入
- trunc()、 frac():截取取值,trunc是整数部分、frac是小数部分
import torch
import numpy as np
def detect_gpu():
print(torch.__version__)
print(torch.cuda.is_available())
#近似值
def test():
detect_gpu()
a = torch.tensor(3.14)
print("a.floor: {0}, a.ceil: {1}, a.trunc: {2}, a.frac: {3}"
.format(a.floor(), a.ceil(), a.trunc(), a.frac()))
#四舍五入
a = torch.tensor(3.499)
print(a.round())
a = torch.tensor(3.5)
print(a.round())
if __name__ == '__main__':
test()
5、clamp(裁剪功能)——》梯度离散(gradient clipping)
#在最大值和最小值之间对input进行裁剪 torch.clamp(input, min, max, out=None)→ Tensor
import torch
import numpy as np
def detect_gpu():
print(torch.__version__)
print(torch.cuda.is_available())
#裁剪
def test():
detect_gpu()
grad = torch.rand(2, 3) * 15
print(grad)
print(grad.max()) #取出最大值
print(grad.median()) #取出中位数
#小于10 的都变为10
#(min):一个参数表示裁剪小于min 的数将其变为min
print(grad.clamp(10))
#(min,max):两个参数表示裁剪不在(min,max)范围内的数,大于max将其变为max,小于min 的改为min
print(torch.tensor([-1., 10., 8., 40.]).clamp(0, 10))
if __name__ == '__main__':
test()
二、属性统计
- norm:范数
- mean sum
- prod:累乘
- max min argmin argmax (dim=维度值)
- max min 取出指定维度上的最大值最小值
- argmax argmin 取出指定维度上最大值最小值的索引
- kthvalue topk
1、求范数 (?)
import torch
import numpy as np
def detect_gpu():
print(torch.__version__)
print(torch.cuda.is_available())
#求范数
def test():
detect_gpu()
a = torch.full([8], 1)
b = a.view(2, 4)
c = a.view(2, 2, 2)
print(b)
print(c)
print("a.norm: {0}, b.norm: {1}, c.norm: {2}"
.format(a.norm(1), b.norm(1), c.norm(1)))
print("a.norm: {0}, b.norm: {1}, c.norm: {2}"
.format(a.norm(2), b.norm(2), c.norm(2)))
#指定维度
print(b.norm(1, dim=1))
print(b.norm(2, dim=1))
print(c.norm(1, dim=0))
print(c.norm(2, dim=0))
if __name__ == '__main__':
test()
2、mean、sum、min、max、prod、argmin、argmax
import torch
import numpy as np
def detect_gpu():
print(torch.__version__)
print(torch.cuda.is_available())
#求范数
def test():
detect_gpu()
a = torch.arange(8).view(2, 4).float()
print(a)
print("min: {0}, max: {1}, mean: {2}, prod: {3}"
.format(a.min(), a.max(), a.mean(), a.prod()))
print("sum: {0}".format(a.sum()))
print("argmin: {0}, argmax: {1}".format(a.argmin(), a.argmax()))
#按照维度取最大最小值的索引
b = torch.randn(4, 10)
print(b[0])
print(b.argmax(dim=1))
if __name__ == '__main__':
test()
3、dim、keepdim参数
import torch
import numpy as np
def detect_gpu():
print(torch.__version__)
print(torch.cuda.is_available())
#求范数
def test():
detect_gpu()
#按照维度取最大最小值的索引
b = torch.randn(4, 10)
#max 返回最大值+最大值对应的位置
print("max(dim=1): {0}".format(b.max(dim=1)))
print()
#argmax仅返回最大值
print("argmax(dim=1): {0}".format(b.argmax(dim=1)))
#加了 keepdim参数会把每一项当作一维输出
print("\nmax(dim=1, keepdim=True):")
print(b.max(dim=1, keepdim=True))
print("\nargmax(dim=1, keepdim=True):")
print(b.argmax(dim=1, keepdim=True))
if __name__ == '__main__':
test()
4、top-k or k-th
- topk:取出指定维度排名前k的元素
- kthvalue:找出指定维度位列第k的元素
import torch
import numpy as np
def detect_gpu():
print(torch.__version__)
print(torch.cuda.is_available())
#求范数
def test():
detect_gpu()
#按照维度取最大最小值的索引
b = torch.randn(4, 10)
print("topk(3, dim=1)")
print(b.topk(3, dim=1))
#largest参数:指定是否是从大到小排序(降序)
print("\ntopk(3, dim=1, largest=False)")
print(b.topk(3, dim=1, largest=False))
print("\nkthvalue(8, dim=1)")
print(b.kthvalue(8, dim=1))
if __name__ == '__main__':
test()
5、compare
- > >= < <= != ==
- 它会把tensor中每一个元素都进行比较,满足就是1,不满足就是0
import torch
import numpy as np
def detect_gpu():
print(torch.__version__)
print(torch.cuda.is_available())
#求范数
def test():
detect_gpu()
a = torch.rand(4,10)
print(a)
print(a>0)
print(torch.gt(a, 0))
print(torch.eq(a, a))
print(torch.equal(a, a))
if __name__ == '__main__':
test()
三、高阶操作
1、where
import torch
import numpy as np
def detect_gpu():
print(torch.__version__)
print(torch.cuda.is_available())
#求范数
def test():
detect_gpu()
cond = torch.rand(2, 2)
print(cond)
a = torch.zeros(2, 2)
b = torch.ones(2, 2)
#逻辑时序控制:根据需求给tensor对应的位置赋值
print(torch.where(cond > 0.5, a, b))
if __name__ == '__main__':
test()
2、gather:其实就是一个查表的过程
- 快速从 relative labeling 到 globel labeling
- 一般来说有三个参数:输入的变量input、指定在某一维上聚合的dim、聚合的使用的索引index,输出为Tensor类型的结果(index必须为LongTensor类型)。
#参数介绍: input (Tensor) – The source tensor dim (int) – The axis along which to index index (LongTensor) – The indices of elements to gather out (Tensor, optional) – Destination tensor #当输入为三维时的计算过程: out[i][j][k] = input[index[i][j][k]][j][k] # dim=0 out[i][j][k] = input[i][index[i][j][k]][k] # dim=1 out[i][j][k] = input[i][j][index[i][j][k]] # dim=2 #样例: t = torch.Tensor([[1,2],[3,4]]) torch.gather(t, 1, torch.LongTensor([[0,0],[1,0]])) # 1 1 # 4 3 #[torch.FloatTensor of size 2x2]
import torch
import numpy as np
def detect_gpu():
print(torch.__version__)
print(torch.cuda.is_available())
#gather
def test():
detect_gpu()
prob = torch.randn(4, 10)
idx = prob.topk(dim=1, k=3)
idx = idx[1]
print(idx)
label = torch.arange(10) + 100
print(label)
print(label.expand(4, 10))
#idx.long():将tensor转换为LongTensor类型
print(idx.long())
print(idx.long().type())
#表扩展为4*10, 在1维上查表,
print(torch.gather(label.expand(4,10), dim=1, index=idx.long()))
if __name__ == '__main__':
test()