我正在阅读有关使用 AllenNlp 框架的基于字符的神经网络的教程,目标是构建一个可以完成句子的模型。在我想训练我的模型之后,有一个实例构建步骤。我有下面的代码,我无法理解转发功能的作用,任何人都可以帮忙吗?有人可以举个例子吗
class RNNLanguageModel(Model):
def __init__(self,
embedder: TextFieldEmbedder,
hidden_size: int,
max_len: int,
vocab: Vocabulary) -> None:
super().__init__(vocab)
self.embedder = embedder
# initialize a Seq2Seq encoder, LSTM
self.rnn = PytorchSeq2SeqWrapper(
torch.nn.LSTM(EMBEDDING_SIZE, HIDDEN_SIZE, batch_first=True))
self.hidden2out = torch.nn.Linear(in_features=self.rnn.get_output_dim(), out_features=vocab.get_vocab_size('tokens'))
self.hidden_size = hidden_size
self.max_len = max_len
def forward(self, input_tokens, output_tokens):
'''
This is the main process of the Model where the actual computation happens.
Each Instance is fed to the forward method.
It takes dicts of tensors as input, with same keys as the fields in your Instance (input_tokens, output_tokens)
It outputs the results of predicted tokens and the evaluation metrics as a dictionary.
'''
mask = get_text_field_mask(input_tokens)
embeddings = self.embedder(input_tokens)
rnn_hidden = self.rnn(embeddings, mask)
out_logits = self.hidden2out(rnn_hidden)
loss = sequence_cross_entropy_with_logits(out_logits, output_tokens['tokens'], mask)
return {'loss': loss}