amy
amy

Reputation: 352

How to convert 3D tensor to 2D tensor in pytorch?

I am new to pytorch. I have 3D tensor (32,10,64) and I want a 2D tensor (32, 64). I tried view() and used after passing to linear layer squeeze() which converted it to (32,10).

Upvotes: 1

Views: 15897

Answers (1)

Rex Low
Rex Low

Reputation: 2157

Try this

t = torch.rand(32, 10, 64).permute(0, 2, 1)[:, :, -1]

or, as pointed out by Shai, you could also

t = torch.rand(32, 10, 64)[:, -1, :]


print(t.size()) # torch.Size([32, 64])

Upvotes: 3

Related Questions