Different behavior when applying transpose operation on Variable and Tensor

(Xiucheng Li) #1

For a 3-D tensor z = torch.rand(2, 3, 5), z.t() raise an RuntimeError: t() expects a 2D tensor, but self is 3D. But when wrapping it as the Variable z = Variable(torch.rand(2, 3, 5)), z.t() works normally, is there any plausible explanation for this different behavior? Thanks.


what you need is z.transpose(dim_1, dim_2)

(Xiucheng Li) #4

@cswhjiang Thanks for your reply. Yes, just transposing the first two dimension. My confusion is that why this method has inconsistent results for Tensor and Variable.


This is a bug (i.e. we didn’t have checks).
I’ve added them in this PR: https://github.com/pytorch/pytorch/pull/1823

(Xiucheng Li) #6

@smth Thanks for fixing it. It is more reasonable now.