Ejemplo n.º 1
0
 def build_embedding(dictionary,
                     embed_dim,
                     path=None,
                     num_embed_chunks=1):
     assert embed_dim % num_embed_chunks == 0, (
         f"Number of embedding chunks = {num_embed_chunks} should be " +
         f"divisible by the embedding dimension = {embed_dim}")
     assert path is None or num_embed_chunks == 1, (
         "Loading embedding from a path with number of embedding chunks > 1"
         + " is not yet supported")
     num_embeddings = len(dictionary)
     padding_idx = dictionary.pad()
     # if provided, load from preloaded dictionaries
     if path:
         emb = Embedding(num_embeddings, embed_dim, padding_idx)
         embed_dict = utils.parse_embedding(path)
         utils.load_embedding(embed_dict, dictionary, emb)
     else:
         embed_chunk_dim = embed_dim // num_embed_chunks
         emb = nn.ModuleList()
         for i in range(num_embed_chunks):
             emb.append(
                 Embedding(num_embeddings, embed_chunk_dim,
                           padding_idx))
     return emb
Ejemplo n.º 2
0
 def load_pretrained_embedding_from_file(embed_path, dictionary,
                                         embed_dim):
     num_embeddings = len(dictionary)
     padding_idx = dictionary.pad()
     embed_tokens = Embedding(num_embeddings, embed_dim, padding_idx)
     embed_dict = utils.parse_embedding(embed_path)
     utils.print_embed_overlap(embed_dict, dictionary)
     return utils.load_embedding(embed_dict, dictionary, embed_tokens)
Ejemplo n.º 3
0
 def build_embedding(dictionary, embed_dim, path=None):
     num_embeddings = len(dictionary)
     padding_idx = dictionary.pad()
     emb = Embedding(num_embeddings, embed_dim, padding_idx)
     # if provided, load from preloaded dictionaries
     if path:
         embed_dict = utils.parse_embedding(path)
         utils.load_embedding(embed_dict, dictionary, emb)
     return emb
Ejemplo n.º 4
0
    def build_model(cls, args, task):
        """Build a new model instance."""
        # make sure that all args are properly defaulted (in case there are any new ones)
        base_architecture(args)

        encoder_embed_dict = None
        if args.encoder_embed_path:
            encoder_embed_dict = utils.parse_embedding(args.encoder_embed_path)
            utils.print_embed_overlap(encoder_embed_dict,
                                      task.source_dictionary)

        decoder_embed_dict = None
        if args.decoder_embed_path:
            decoder_embed_dict = utils.parse_embedding(args.decoder_embed_path)
            utils.print_embed_overlap(decoder_embed_dict,
                                      task.target_dictionary)

        encoder = FConvEncoder(
            dictionary=task.source_dictionary,
            embed_dim=args.encoder_embed_dim,
            embed_dict=encoder_embed_dict,
            convolutions=eval(args.encoder_layers),
            dropout=args.dropout,
            max_positions=args.max_source_positions,
        )
        decoder = FConvDecoder(
            dictionary=task.target_dictionary,
            embed_dim=args.decoder_embed_dim,
            embed_dict=decoder_embed_dict,
            convolutions=eval(args.decoder_layers),
            out_embed_dim=args.decoder_out_embed_dim,
            attention=eval(args.decoder_attention),
            dropout=args.dropout,
            max_positions=args.max_target_positions,
            share_embed=args.share_input_output_embed,
        )
        return FConvModel(encoder, decoder)