How can I print the shape of a tensor inside the forward function?

I have an attention decoder whose forward function is as follows.

def forward(self, input, hidden, encoder_outputs):
    embedded = self.embedding(input).view(1, 1, -1)
    embedded = self.drop(embedded)
    attn_weights = F.softmax(self.attn(torch.cat((embedded[0], hidden[0]), 1)))

How can I print the shape of embedded tensor inside the forward function? I checked, simple print() statement doesn’t work.

Any suggestion?

2 Likes

A print statement (print(embedded.size())) should work. What do you mean when you say it doesn’t work (nothing happens?)

5 Likes

You can do print(embedded.size()). Works for me even in the forward function.

2 Likes

The .size() solution didn’t work for me when I had created an embedding using nn.Embedding. However, just printing the embeddings does show me size at the end of it:
print(embedded).
Hope this helps someone

does this return a tuple?
I saw it is torch.Size([19, 25, 2100])

torch.Size is essentially a tuple, and can do the same things

Use print(embedded) to see the shape, or embedded.eval()

If you want to see the content, embedded.weight will show you the tensor and if it requires grad.