コード例 #1
0
    def __init__(self, opt):
        self.opt = opt
        self.tt = torch.cuda if opt.cuda else torch

        checkpoint = torch.load(opt.model,
                                map_location=lambda storage, loc: storage)
        model_opt = checkpoint['settings']
        if 'use_ctx' not in model_opt.__dict__:
            model_opt.use_ctx = False
        self.model_opt = model_opt

        model = Transformer(model_opt.src_vocab_size,
                            model_opt.tgt_vocab_size,
                            model_opt.max_token_seq_len,
                            proj_share_weight=model_opt.proj_share_weight,
                            embs_share_weight=model_opt.embs_share_weight,
                            d_k=model_opt.d_k,
                            d_v=model_opt.d_v,
                            d_model=model_opt.d_model,
                            d_word_vec=model_opt.d_word_vec,
                            d_inner_hid=model_opt.d_inner_hid,
                            n_layers=model_opt.n_layers,
                            n_head=model_opt.n_head,
                            dropout=model_opt.dropout,
                            use_ctx=model_opt.use_ctx)

        prob_projection = nn.LogSoftmax()

        model.load_state_dict(checkpoint['model'])

        # New max_token_seq_len for position encoding
        model = self.change_position_embedings(model, opt.max_token_seq_len,
                                               model_opt.d_word_vec,
                                               model_opt.use_ctx)
        model_opt.max_token_seq_len = opt.max_token_seq_len
        print('[Info] Trained model state loaded.')

        if opt.cuda:
            model.cuda()
            prob_projection.cuda()
        else:
            model.cpu()
            prob_projection.cpu()

        model.prob_projection = prob_projection

        self.model = model
        self.model.eval()
コード例 #2
0
ファイル: Translator.py プロジェクト: jshi31/NAFAE
    def __init__(self, opt):
        self.opt = opt
        self.tt = torch.cuda if opt.cuda else torch

        checkpoint = torch.load(opt.model)
        model_opt = checkpoint['settings']
        self.model_opt = model_opt

        model = Transformer(
            model_opt.src_vocab_size,
            model_opt.tgt_vocab_size,
            model_opt.max_token_seq_len,
            proj_share_weight=model_opt.proj_share_weight,
            embs_share_weight=model_opt.embs_share_weight,
            d_k=model_opt.d_k,
            d_v=model_opt.d_v,
            d_model=model_opt.d_model,
            d_word_vec=model_opt.d_word_vec,
            d_inner_hid=model_opt.d_inner_hid,
            n_layers=model_opt.n_layers,
            n_head=model_opt.n_head,
            dropout=model_opt.dropout)

        prob_projection = nn.LogSoftmax()

        model.load_state_dict(checkpoint['model'])
        print('[Info] Trained model state loaded.')

        if opt.cuda:
            model.cuda()
            prob_projection.cuda()
        else:
            model.cpu()
            prob_projection.cpu()

        model.prob_projection = prob_projection

        self.model = model
        self.model.eval()