1.torch.exp()
torch.exp(张量名)
运算结果是 e^n ,即 e 的 n 次方 ,e是自然常数,n是单一元素值
最后的结果是e^n的集合,也就是一个新的张量, 如下:
x = torch.tensor([[4,3,2,1],[1,2,3,4]])
print(torch.exp(x))
tensor([[54.5981, 20.0855, 7.3891, 2.7183],
[ 2.7183, 7.3891, 20.0855, 54.5981]])
注意:exp可以是多维张量,但任意维度内张量形状必须相同,否则报错。
2.torch.stack
torch.stack((多个张量名),dim=x) 意义是连结张量,进行堆叠,增加新的维度。
stack可以连结多个且形状相同的张量。dim后“x”的意思是第x+1维度内进行连结
例子如下:
T1 = torch.tensor([[1, 2, 3],[4, 5, 6],[7, 8, 9]])
T2 = torch.tensor([[10, 20, 30],[40, 50, 60],[70, 80, 90]])
T3 = torch.tensor([[11, 22, 33],[44, 55, 66],[77, 88, 99]])
print(torch.stack((T1,T2,T3),dim=0))
print(torch.stack((T1,T2),dim=1))
print(torch.stack((T1,T2),dim=2))
tensor([[[ 1., 2., 3.],
[ 4., 5., 6.],
[ 7., 8., 9.]],
[[10., 20., 30.],
[40., 50., 60.],
[70., 80., 90.]],
[[11., 22., 33.],
[44., 55., 66.],
[77., 88., 99.]]])
tensor([[[ 1, 2, 3],
[10, 20, 30]],
[[ 4, 5, 6],
[40, 50, 60]],
[[ 7, 8, 9],
[70, 80, 90]]])
tensor([[[ 1, 10],
[ 2, 20],
[ 3, 30]],
[[ 4, 40],
[ 5, 50],
[ 6, 60]],
[[ 7, 70],
[ 8, 80],
[ 9, 90]]])