def __init__(self, model_source, cuda=False, beam_size=3):
self.torch = torch.cuda if cuda else torch
self.cuda = cuda
self.beam_size = beam_size
if self.cuda:
model_source = torch.load(model_source)
else:
model_source = torch.load(model_source, map_location=lambda storage, loc: storage)
self.src_dict = model_source["src_dict"]
self.tgt_dict = model_source["tgt_dict"]
self.src_idx2word = {v: k for k, v in model_source["tgt_dict"].items()}
self.args = args = model_source["settings"]
model = Transformer(args)
model.load_state_dict(model_source['model'])
if self.cuda: model = model.cuda()
else: model = model.cpu()
self.model = model.eval()
评论列表
文章目录