-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate.py
57 lines (46 loc) · 1.65 KB
/
translate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import argparse
import torch
import torch.nn as nn
import models
import infer
#from infer import Beam
# build args parser
parser = argparse.ArgumentParser(description='Training NMT')
parser.add_argument('--checkpoint', required=True,
help='saved checkpoit.')
parser.add_argument('--input', required=True,
help='Text file to translate.')
parser.add_argument('--output', default='trans.bpe', help='output file')
parser.add_argument('--ref', default='', help='reference file')
parser.add_argument('--beam_size', default=5, type=int,
help="Beam size.")
parser.add_argument('--gpus', default=[], nargs='+', type=int,
help="Use CUDA")
args = parser.parse_args()
args.cuda = len(args.gpus)
#print(args)
if torch.cuda.is_available() and not args.cuda:
print("WARNING: You have a CUDA device, so you should probably run with -gpus 0")
if args.cuda:
torch.cuda.set_device(args.gpus[0])
checkpoint = torch.load(args.checkpoint)
saved_args = checkpoint['args']
encoder = models.Encoder(saved_args)
decoder = models.Decoder(saved_args)
generator = nn.Sequential(
nn.Linear(saved_args.rnn_size, saved_args.n_words_tgt),
nn.LogSoftmax()
)
model = models.NMT(encoder, decoder, generator)
if args.cuda:
model.cuda()
model.load_state_dict(checkpoint['params'])
# overwritten some options
saved_args.beam_size = args.beam_size
if args.beam_size == 0 and args.ref != '':
agent = infer.MyPolicy(saved_args, model)
#agent = infer.RefPolicy(saved_args, model)
agent.get_scores(args.input, args.ref)
else:
agent = infer.Beam(saved_args, model)
agent.translate(args.input, args.output)