(model_opt, fields, gpu, checkpoint=None)
return model
Args:
        model_opt: the option loaded from checkpoint.
        fields: `Field` objects for the model.
        gpu(bool): whether to use gpu.
        checkpoint: the model gnerated by train phase, or a resumed snapshot
                    model from a stopped training.
    Returns:
        the NMTModel.
Args:
        model_opt: the option loaded from checkpoint.
        fields: `Field` objects for the model.
        gpu(bool): whether to use gpu.
        checkpoint: the model gnerated by train phase, or a resumed snapshot
                    model from a stopped training.
    Returns:
        the NMTModel.
Args : model_opt : the option loaded from checkpoint . fields : Field objects for the model . gpu ( bool ) : whether to use gpu . checkpoint : the model gnerated by train phase or a resumed snapshot model from a stopped training . Returns : the NMTModel .
def build_base_model(model_opt, fields, gpu, checkpoint=None):
    """
    Args:
        model_opt: the option loaded from checkpoint.
        fields: `Field` objects for the model.
        gpu(bool): whether to use gpu.
        checkpoint: the model gnerated by train phase, or a resumed snapshot
                    model from a stopped training.
    Returns:
        the NMTModel.
    """
    assert model_opt.model_type in ["text", "img", "audio"], \
        ("Unsupported model type %s" % (model_opt.model_type))

    # Build encoder.
    if model_opt.model_type == "text":
        src_dict = fields["src"].vocab
        feature_dicts = inputters.collect_feature_vocabs(fields, 'src')
        src_embeddings = build_embeddings(model_opt, src_dict, feature_dicts)
        encoder = build_encoder(model_opt, src_embeddings)
    elif model_opt.model_type == "img":
        if ("image_channel_size" not in model_opt.__dict__):
            image_channel_size = 3
        else:
            image_channel_size = model_opt.image_channel_size

        encoder = ImageEncoder(model_opt.enc_layers,
                               model_opt.brnn,
                               model_opt.rnn_size,
                               model_opt.dropout,
                               image_channel_size)
    elif model_opt.model_type == "audio":
        encoder = AudioEncoder(model_opt.enc_layers,
                               model_opt.brnn,
                               model_opt.rnn_size,
                               model_opt.dropout,
                               model_opt.sample_rate,
                               model_opt.window_size)

    model_parameters = filter(lambda p: p.requires_grad, encoder.parameters())
    params = sum([np.prod(p.size()) for p in model_parameters])
    # Build decoder.
    tgt_dict = fields["tgt"].vocab
    feature_dicts = inputters.collect_feature_vocabs(fields, 'tgt')
    tgt_embeddings = build_embeddings(model_opt, tgt_dict,
                                      feature_dicts, for_encoder=False)

    # Share the embedding matrix - preprocess with share_vocab required.
    if model_opt.share_embeddings:
        # src/tgt vocab should be the same if `-share_vocab` is specified.
        if src_dict != tgt_dict:
            raise AssertionError('The `-share_vocab` should be set during '
                                 'preprocess if you use share_embeddings!')

        tgt_embeddings.word_lut.weight = src_embeddings.word_lut.weight

    decoder = build_decoder(model_opt, tgt_embeddings)

    # Build NMTModel(= encoder + decoder).
    device = torch.device("cuda" if gpu else "cpu")
    model = onmt.models.NMTModel(encoder, decoder)
    model.model_type = model_opt.model_type

    # Build Generator.
    if not model_opt.copy_attn:
        if model_opt.generator_function == "sparsemax":
            gen_func = onmt.modules.sparse_activations.LogSparsemax(dim=-1)
        else:
            gen_func = nn.LogSoftmax(dim=-1)
        generator = nn.Sequential(
            nn.Linear(model_opt.rnn_size, len(fields["tgt"].vocab)), gen_func
        )
        if model_opt.share_decoder_embeddings:
            generator[0].weight = decoder.embeddings.word_lut.weight
    else:
        generator = CopyGenerator(model_opt.rnn_size,
                                  fields["tgt"].vocab)

    # Load the model states from checkpoint or initialize them.
    if checkpoint is not None:
        model.load_state_dict(checkpoint['model'])
        generator.load_state_dict(checkpoint['generator'])
    else:
        if model_opt.param_init != 0.0:
            for p in model.parameters():
                p.data.uniform_(-model_opt.param_init, model_opt.param_init)
            for p in generator.parameters():
                p.data.uniform_(-model_opt.param_init, model_opt.param_init)
        if model_opt.param_init_glorot:
            for p in model.parameters():
                if p.dim() > 1:
                    xavier_uniform_(p)
            for p in generator.parameters():
                if p.dim() > 1:
                    xavier_uniform_(p)

        if hasattr(model.encoder, 'embeddings'):
            model.encoder.embeddings.load_pretrained_vectors(
                model_opt.pre_word_vecs_enc, model_opt.fix_word_vecs_enc)
        if hasattr(model.decoder, 'embeddings'):
            model.decoder.embeddings.load_pretrained_vectors(
                model_opt.pre_word_vecs_dec, model_opt.fix_word_vecs_dec)

    # Add generator to model (this registers it as parameter of model).
    model.generator = generator
    model.to(device)

    return model
def build_base_model ( model_opt , fields , gpu , checkpoint = None ) : assert model_opt . model_type in [ "text" , "img" , "audio" ] , ( "Unsupported model type %s" % ( model_opt . model_type ) ) # Build encoder. if model_opt . model_type == "text" : src_dict = fields [ "src" ] . vocab feature_dicts = inputters . collect_feature_vocabs ( fields , 'src' ) src_embeddings = build_embeddings ( model_opt , src_dict , feature_dicts ) encoder = build_encoder ( model_opt , src_embeddings ) elif model_opt . model_type == "img" : if ( "image_channel_size" not in model_opt . __dict__ ) : image_channel_size = 3 else : image_channel_size = model_opt . image_channel_size encoder = ImageEncoder ( model_opt . enc_layers , model_opt . brnn , model_opt . rnn_size , model_opt . dropout , image_channel_size ) elif model_opt . model_type == "audio" : encoder = AudioEncoder ( model_opt . enc_layers , model_opt . brnn , model_opt . rnn_size , model_opt . dropout , model_opt . sample_rate , model_opt . window_size ) model_parameters = filter ( lambda p : p . requires_grad , encoder . parameters ( ) ) params = sum ( [ np . prod ( p . size ( ) ) for p in model_parameters ] ) # Build decoder. tgt_dict = fields [ "tgt" ] . vocab feature_dicts = inputters . collect_feature_vocabs ( fields , 'tgt' ) tgt_embeddings = build_embeddings ( model_opt , tgt_dict , feature_dicts , for_encoder = False ) # Share the embedding matrix - preprocess with share_vocab required. if model_opt . share_embeddings : # src/tgt vocab should be the same if `-share_vocab` is specified. if src_dict != tgt_dict : raise AssertionError ( 'The `-share_vocab` should be set during ' 'preprocess if you use share_embeddings!' ) tgt_embeddings . word_lut . weight = src_embeddings . word_lut . weight decoder = build_decoder ( model_opt , tgt_embeddings ) # Build NMTModel(= encoder + decoder). device = torch . device ( "cuda" if gpu else "cpu" ) model = onmt . models . NMTModel ( encoder , decoder ) model . model_type = model_opt . model_type # Build Generator. if not model_opt . copy_attn : if model_opt . generator_function == "sparsemax" : gen_func = onmt . modules . sparse_activations . LogSparsemax ( dim = - 1 ) else : gen_func = nn . LogSoftmax ( dim = - 1 ) generator = nn . Sequential ( nn . Linear ( model_opt . rnn_size , len ( fields [ "tgt" ] . vocab ) ) , gen_func ) if model_opt . share_decoder_embeddings : generator [ 0 ] . weight = decoder . embeddings . word_lut . weight else : generator = CopyGenerator ( model_opt . rnn_size , fields [ "tgt" ] . vocab ) # Load the model states from checkpoint or initialize them. if checkpoint is not None : model . load_state_dict ( checkpoint [ 'model' ] ) generator . load_state_dict ( checkpoint [ 'generator' ] ) else : if model_opt . param_init != 0.0 : for p in model . parameters ( ) : p . data . uniform_ ( - model_opt . param_init , model_opt . param_init ) for p in generator . parameters ( ) : p . data . uniform_ ( - model_opt . param_init , model_opt . param_init ) if model_opt . param_init_glorot : for p in model . parameters ( ) : if p . dim ( ) > 1 : xavier_uniform_ ( p ) for p in generator . parameters ( ) : if p . dim ( ) > 1 : xavier_uniform_ ( p ) if hasattr ( model . encoder , 'embeddings' ) : model . encoder . embeddings . load_pretrained_vectors ( model_opt . pre_word_vecs_enc , model_opt . fix_word_vecs_enc ) if hasattr ( model . decoder , 'embeddings' ) : model . decoder . embeddings . load_pretrained_vectors ( model_opt . pre_word_vecs_dec , model_opt . fix_word_vecs_dec ) # Add generator to model (this registers it as parameter of model). model . generator = generator model . to ( device ) return model