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

Method forward

intermediate_source/seq2seq_translation_tutorial.py:382–402  ·  view source on GitHub ↗
(self, encoder_outputs, encoder_hidden, target_tensor=None)

Source from the content-addressed store, hash-verified

380 self.out = nn.Linear(hidden_size, output_size)
381
382 def forward(self, encoder_outputs, encoder_hidden, target_tensor=None):
383 batch_size = encoder_outputs.size(0)
384 decoder_input = torch.empty(batch_size, 1, dtype=torch.long, device=device).fill_(SOS_token)
385 decoder_hidden = encoder_hidden
386 decoder_outputs = []
387
388 for i in range(MAX_LENGTH):
389 decoder_output, decoder_hidden = self.forward_step(decoder_input, decoder_hidden)
390 decoder_outputs.append(decoder_output)
391
392 if target_tensor is not None:
393 # Teacher forcing: Feed the target as the next input
394 decoder_input = target_tensor[:, i].unsqueeze(1) # Teacher forcing
395 else:
396 # Without teacher forcing: use its own predictions as the next input
397 _, topi = decoder_output.topk(1)
398 decoder_input = topi.squeeze(-1).detach() # detach from history as input
399
400 decoder_outputs = torch.cat(decoder_outputs, dim=1)
401 decoder_outputs = F.log_softmax(decoder_outputs, dim=-1)
402 return decoder_outputs, decoder_hidden, None # We return `None` for consistency in the training loop
403
404 def forward_step(self, input, hidden):
405 output = self.embedding(input)

Callers

nothing calls this directly

Calls 1

forward_stepMethod · 0.95

Tested by

no test coverage detected