How to take shape [1,1,256] from [1,4,256] cuda.FloatTensor?
I have an batch of output hidden vector from GRU. It's shape is [1,4,256]

( 0 ,.,.) = 
-0.9944  1.0000  0.0000  ...  -1.0000  0.0000 -1.0000
-1.0000  1.0000  0.0000  ...  -1.0000  0.0000 -1.0000
-1.0000  1.0000  0.0000  ...  -1.0000  0.0000 -1.0000
-1.0000  1.0000  0.0000  ...  -1.0000  0.0000 -1.0000
[torch.cuda.FloatTensor of size (1,4,256) (GPU 0)]


I need a shape of [1,1,256] to pass to another model. How can I take it? Through this line I can only have a shape of [1,256]

decoder_hidden = encoder_hidden[:, index]


resize and wrap a new FloatTensor didn't work.

You can unsqueeze() in dimension 1 to achieve this.

encoder_hidden = torch.randn(1, 4, 256)
print(encoder_hidden.size())

for idx in range(encoder_hidden.size(1)):
    decoder_hidden = encoder_hidden[:, idx, :].unsqueeze(1)
    print(decoder_hidden.size())


It prints:

torch.Size([1, 4, 256])
torch.Size([1, 1, 256])
torch.Size([1, 1, 256])
torch.Size([1, 1, 256])
torch.Size([1, 1, 256])

