(encoder, decoder, searcher, voc, sentence, max_length=MAX_LENGTH)
| 1182 | # |
| 1183 | |
| 1184 | def evaluate(encoder, decoder, searcher, voc, sentence, max_length=MAX_LENGTH): |
| 1185 | ### Format input sentence as a batch |
| 1186 | # words -> indexes |
| 1187 | indexes_batch = [indexesFromSentence(voc, sentence)] |
| 1188 | # Create lengths tensor |
| 1189 | lengths = torch.tensor([len(indexes) for indexes in indexes_batch]) |
| 1190 | # Transpose dimensions of batch to match models' expectations |
| 1191 | input_batch = torch.LongTensor(indexes_batch).transpose(0, 1) |
| 1192 | # Use appropriate device |
| 1193 | input_batch = input_batch.to(device) |
| 1194 | lengths = lengths.to("cpu") |
| 1195 | # Decode sentence with searcher |
| 1196 | tokens, scores = searcher(input_batch, lengths, max_length) |
| 1197 | # indexes -> words |
| 1198 | decoded_words = [voc.index2word[token.item()] for token in tokens] |
| 1199 | return decoded_words |
| 1200 | |
| 1201 | |
| 1202 | def evaluateInput(encoder, decoder, searcher, voc): |
no test coverage detected