Tensor张量
import torch
1.torch.stack(): 对tensors沿指定维度拼接,但返回的Tensor会多一维
a=torch.rand(2,3)
b=torch.randn(2,3)
d=torch.stack((a,b), dim=0)
print(d)
print(d.size())
tensor([[[ 0.6726, 0.1213, 0.2705],
[ 0.6798, 0.1086, 0.5244]],
[[-0.5596, 0.2247, -0.7475],
[-0.4650, -0.5764, 1.7613]]])
torch.Size([2, 2, 3])
2.torch.sum(),torch.mean(),torch.max() :求和,平均,最值操作
a=torch.tensor([[1.0,2.0,3.0],[4.0,5.0,6.0]])
print(a)
print(torch.sum(a,dim=0))
c=torch.mean(a,0)#只能对浮点数
print(c)
max_value,maxindex=torch.max(a,1)#返回每一行的最大值及其在该行的索引
print(max_value)
print(maxindex)
tensor([[1., 2., 3.],
[4., 5., 6.]])
tensor([5., 7., 9.])
tensor([2.5000, 3.5000, 4.5000])
tensor([3., 6.])
tensor([2, 2])
3.squeeze,unsqueeze:降维与升维
a=torch.tensor([[1,2,3],[4,5,6