MCPcopy Create free account
hub / github.com/pytorch/tutorials / forward

Method forward

beginner_source/chatbot_tutorial.py:837–857  ·  view source on GitHub ↗
(self, input_step, last_hidden, encoder_outputs)

Source from the content-addressed store, hash-verified

835 self.attn = Attn(attn_model, hidden_size)
836
837 def forward(self, input_step, last_hidden, encoder_outputs):
838 # Note: we run this one step (word) at a time
839 # Get embedding of current input word
840 embedded = self.embedding(input_step)
841 embedded = self.embedding_dropout(embedded)
842 # Forward through unidirectional GRU
843 rnn_output, hidden = self.gru(embedded, last_hidden)
844 # Calculate attention weights from the current GRU output
845 attn_weights = self.attn(rnn_output, encoder_outputs)
846 # Multiply attention weights to encoder outputs to get new "weighted sum" context vector
847 context = attn_weights.bmm(encoder_outputs.transpose(0, 1))
848 # Concatenate weighted context vector and GRU output using Luong eq. 5
849 rnn_output = rnn_output.squeeze(0)
850 context = context.squeeze(1)
851 concat_input = torch.cat((rnn_output, context), 1)
852 concat_output = torch.tanh(self.concat(concat_input))
853 # Predict next word using Luong eq. 6
854 output = self.out(concat_output)
855 output = F.softmax(output, dim=1)
856 # Return output and final hidden state
857 return output, hidden
858
859
860######################################################################

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected