a-PyTorch-Tutorial-to-Image-Captioning icon indicating copy to clipboard operation
a-PyTorch-Tutorial-to-Image-Captioning copied to clipboard

how do I get Decoder_hidden???

Open rohit-shekhar26 opened this issue 5 years ago • 1 comments

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

rohit-shekhar26 avatar Apr 17 '20 08:04 rohit-shekhar26

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.

kmario23 avatar Apr 17 '20 19:04 kmario23