x.size()[1]的作用
最近学pytorch,发现一行代码用法不是很明白就自己敲一下,查看输入输出,明白了他的用法,小小记录一下。
x = torch.Tensor([[1, 2, 3], [1, 2, 2]])
y = torch.rand(3, 1)
print(x, y)
batchsize_1 = x.size()[0]
batchsize_2 = x.size()[1]
batchsize_3 = y.size()[1]
print(batchsize_1,batchsize_2,batchsize_3)```
结果输出为下:
tensor([[1., 2., 3.],
[1., 2., 2.]])
tensor([[0.5492],
[0.4909],
[0.8353]])
2 3 1
分析:
由代码运行结果可知,x.size()[0]输出的是张量x的行数,x.size()[1]输出的是张量y的列数