Я пытаюсь реализовать свой собственный механизм MT, следуя инструкциям в https://github.com/bentrevett/pytorch-seq2seq/blob/master/1%20-%20Sequence%20to%20Sequence%20Learning%20with%20Neural%20Networks.ipynb
SRC = Field(tokenize=tokenize_en,
init_token='<sos>',
eos_token='<eos>',
lower=True)
TRG = Field(tokenize=tokenize_de,
init_token='<sos>',
eos_token='<eos>',
lower=True)
После обучения модели ссылка дает только способ пакетной оценки, но я хочу попробовать одну строку и получить результаты перевода. например, я хочу, чтобы моя модель переводила ввод Boys и получала немецкие переводы.
savedfilemodelpath='./pretrained_model/2020-09-27en-de.pth'
model.load_state_dict(torch.load(savedfilemodelpath))
model.eval()
inputstring = 'Boys'
processed=SRC.process([SRC.preprocess(inputstring)]).to(device)
output=model(processed,processed)
output_dim = output.shape[-1]
outputs = output[1:].view(-1, output_dim)
for item in outputs:
print('item shape is {} and item.argmax is {}, and words is {}'.format(item.shape,item.argmax(),TRG.vocab.itos[item.argmax()]))
Итак, мой вопрос в том, что правильно получать результаты перевода:
Во-первых: преобразовать строку в тензор
inputstring = 'Boys'
processed=SRC.process([SRC.preprocess(inputstring)]).to(device)
Второе: отправить тензор в модель. Поскольку у модели есть параметр TRG. Я должен указать тензор, могу ли я не дать тензор TRG?
output=model(processed,processed)
output_dim = output.shape[-1]
outputs = output[1:].view(-1, output_dim)
В-третьих: через тензор возврата я использую argmax для получения результатов перевода? это правильно?
Или как я могу получить правильные результаты перевода?
for item in outputs:
print('item shape is {} and item.argmax is {}, and words is {}'.format(item.shape,item.argmax(),TRG.vocab.itos[item.argmax()+1]))