mirror of
https://github.com/yunjey/pytorch-tutorial.git
synced 2025-07-27 03:53:47 +08:00
image captioning completed'
This commit is contained in:
@ -53,14 +53,14 @@ class DecoderRNN(nn.Module):
|
||||
return outputs
|
||||
|
||||
def sample(self, features, states):
|
||||
"""Samples captions for given image features."""
|
||||
"""Samples captions for given image features (Greedy search)."""
|
||||
sampled_ids = []
|
||||
inputs = features.unsqueeze(1)
|
||||
for i in range(20):
|
||||
hiddens, states = self.lstm(inputs, states) # (batch_size, 1, hidden_size)
|
||||
outputs = self.linear(hiddens.unsqueeze()) # (batch_size, vocab_size)
|
||||
outputs = self.linear(hiddens.squeeze(1)) # (batch_size, vocab_size)
|
||||
predicted = outputs.max(1)[1]
|
||||
sampled_ids.append(predicted)
|
||||
inputs = self.embed(predicted)
|
||||
sampled_ids = torch.cat(sampled_ids, 1) # (batch_size, 20)
|
||||
return sampled_ids
|
||||
sampled_ids = torch.cat(sampled_ids, 1) # (batch_size, 20)
|
||||
return sampled_ids.squeeze()
|
Reference in New Issue
Block a user