sgrvinod / a-PyTorch-Tutorial-to-Image-Captioning

Show, Attend, and Tell | a PyTorch Tutorial to Image Captioning
MIT License
2.75k stars 711 forks source link

how do I get Decoder_hidden??? #109

Open rohit-shekhar26 opened 4 years ago

rohit-shekhar26 commented 4 years ago

I appreciate your working and it helped me lot to understand the flow of image captioning.

I got stuck in the decoder part. Kindly help me to understand and debug it.

You have created the encoder_out which contains the extracted features of the image. I want to create the decoder_hidden which is not defined and I am getting the following error.

models.py code line 81

NameError Traceback (most recent call last)

in 1 att1 = encoder_att(encoder_out) # (batch_size, num_pixels, attention_dim) ----> 2 att2 = decoder_att(decoder_hidden) # (batch_size, attention_dim) 3 att = full_att(self.relu(att1 + att2.unsqueeze(1))).squeeze(2) # (batch_size, num_pixels) 4 alpha = softmax(att) # (batch_size, num_pixels) 5 attention_weighted_encoding = (encoder_out * alpha.unsqueeze(2)).sum(dim=1) NameError: name 'decoder_hidden' is not defined thanks and regards Rohit Shekhar
kmario23 commented 4 years ago

It seems like you've bug in your code. I guess you seem to be not binding the function to the object.

In the __init__, ensure you've the following:

self.decoder_att = nn.Linear(decoder_dim, attention_dim)

Once you have that, you can do the following in the forward function,

 att2 = self.decoder_att(decoder_hidden)  # (batch_size, attention_dim)

Once you have both of those in place, be sure to instantiate a callable and bind it the object as in:

self.attention = Attention(encoder_dim, decoder_dim, attention_dim)  # attention network

Once you do the above, you can call this which is usually done in the forward function,

self.attention(encoder_out[:batch_size_t], h[:batch_size_t])  

The 2nd argument in the above line is the hidden state.