torch.nn.Softmax(-1)
X = torch.randn(3,2)
Y = nn.Softmax(dim = -1)(X)
print(X)
print('---')
print(Y)
tensor([[1.6717, 0.1819],
[1.3746, 1.0038],
[0.0052, 0.3082]])
---
tensor([[0.8161, 0.1839],
[0.5917, 0.4083],
[0.4248, 0.5752]])
可以看到通过dim可以对矩阵的某个维度求softmax,dim=-1表示最后一个维度,即对每一行求。
torch.cat()函数
C = torch.cat( (A,B),0 ) #按维数0拼接(竖着拼)
C = torch.cat( (A,B),1 ) #按维数1拼接(横着拼)
import torch
>>> A=torch.ones(2,3) #2x3的张量(矩阵)
>>> A
tensor([[ 1., 1., 1.],
[ 1., 1., 1.]])
>>> B=2*torch.ones(4,3) #4x3的张量(矩阵)
>>> B
tensor([[ 2., 2., 2.],
[ 2., 2., 2.],
[ 2., 2., 2.],
[ 2., 2., 2