def initialize_models(voc): print('Building encoder and decoder ...') # initialize word embeddings embedding = nn.Embedding(voc.num_words, c.HIDDEN_SIZE) if c.LOAD_FILENAME: embedding.load_state_dict(embedding_sd) # initialize encoder & decoder models encoder = EncoderRNN(c.HIDDEN_SIZE, embedding, c.ENCODER_N_LAYERS, c.DROPOUT) decoder = LuongAttnDecoderRNN(c.ATTN_MODEL, embedding, c.HIDDEN_SIZE, voc.num_words, c.DECODER_N_LAYERS, c.DROPOUT) if c.LOAD_FILENAME: encoder.load_state_dict(encoder_sd) decoder.load_state_dict(decoder_sd) # Use appropriate device encoder = encoder.to(c.DEVICE) decoder = decoder.to(c.DEVICE) print('Models built and ready to go!') return embedding, encoder, decoder
def main(): """primary entry """ voc, pairs = loadPreparedData() print('Building encoder and decoder ...') # Initialize word embeddings #embedding = nn.Embedding(voc.num_words, params.hidden_size) embedding = nn.Embedding(voc.num_words, params.embedding_size) # Initialize encoder & decoder models encoder = EncoderRNN(embedding, params.hidden_size, params.encoder_n_layers, params.dropout) decoder = LuongAttnDecoderRNN(params.attn_model, embedding, params.hidden_size, voc.num_words, params.decoder_n_layers, params.dropout) # Use appropriate device encoder = encoder.to(params.device) decoder = decoder.to(params.device) print('Models built and ready to go!') # Ensure dropout layers are in train mode encoder.train() decoder.train() # Initialize optimizers print('Building optimizers ...') encoder_optimizer = optim.Adam(encoder.parameters(), lr=params.learning_rate) decoder_optimizer = optim.Adam(decoder.parameters(), lr=params.learning_rate * params.decoder_learning_ratio) # Run training iterations print("Starting Training!") trainIters(voc, pairs, encoder, decoder, encoder_optimizer, decoder_optimizer, embedding, params.encoder_n_layers, params.decoder_n_layers, params.save_dir, params.n_iteration, params.batch_size, params.print_every, params.save_every, params.clip, params.corpus_name, load_filename=None)
checkpoint = torch.load(ckpt) encoder_sd = checkpoint['en'] decoder_sd = checkpoint['de'] encoder_optimizer_sd = checkpoint['en_opt'] decoder_optimizer_sd = checkpoint['de_opt'] embedding_sd = checkpoint['embedding'] voc.__dict__ = checkpoint['voc_dict'] # Initialize word embeddings embedding = nn.Embedding(voc.num_words, hidden_size) embedding.load_state_dict(embedding_sd) # Initialize encoder & decoder models encoder = EncoderRNN(hidden_size, embedding, encoder_n_layers, dropout) decoder = LuongAttnDecoderRNN(attn_model, embedding, hidden_size, voc.num_words, decoder_n_layers, dropout) encoder.load_state_dict(encoder_sd) decoder.load_state_dict(decoder_sd) encoder = encoder.to(device) decoder = decoder.to(device) # Set dropout layers to eval mode encoder.eval() decoder.eval() # Initialize search module searcher = GreedySearchDecoder(encoder, decoder) # Begin chatting (uncomment and run the following line to begin) evaluateInput(encoder, decoder, searcher, voc)
num_workers=4, collate_fn=collate_fn) ### Initialize model encoder = EncoderRNN(embedding=src_embedding, rnn_type=opts.rnn_type, hidden_size=opts.hidden_size, num_layers=opts.num_layers, dropout=opts.dropout, bidirectional=opts.bidirectional) decoder = LuongAttnDecoderRNN(encoder, embedding=tgt_embedding, attention=opts.attention, tie_embeddings=opts.tie_embeddings, dropout=opts.dropout, tie_ext_feature=opts.tie_ext_feature, ext_rate_embedding=ext_rate_emebdding, ext_appcate_embedding=ext_appcate_embedding, ext_seqlen_embedding=ext_seqlen_embedding, ext_senti_embedding=ext_senti_embedding ) print("emb start") if opts.pretrained_embeddings: if opts.use_keyword: train_embedding = Embedding(filename=args.glove_filename, embedding_size=embedding_size).load_embedding_plus_keyword(train_dataset.src_vocab) target_embedding = Embedding(filename=args.glove_filename, embedding_size=embedding_size).load_embedding_plus_keyword( train_dataset.tgt_vocab) else: train_embedding = Embedding(filename=args.glove_filename, embedding_size=embedding_size).load_embedding(train_dataset.src_vocab) target_embedding = Embedding(filename=args.glove_filename, embedding_size=embedding_size).load_embedding(train_dataset.tgt_vocab) encoder.embedding.weight.data.copy_(train_embedding) decoder.embedding.weight.data.copy_(target_embedding)
#checkpoint = torch.load(loadFilename, map_location=torch.device('cpu')) encoder_sd = checkpoint['en'] decoder_sd = checkpoint['de'] encoder_optimizer_sd = checkpoint['en_opt'] decoder_optimizer_sd = checkpoint['de_opt'] embedding_sd = checkpoint['embedding'] voc.__dict__ = checkpoint['voc_dict'] print('Building encoder and decoder ...') # Initialize word embeddings embedding = nn.Embedding(voc.num_words, hidden_size) if loadFilename: embedding.load_state_dict(embedding_sd) # Initialize encoder & decoder models encoder = EncoderRNN(hidden_size, embedding, encoder_n_layers, dropout) decoder = LuongAttnDecoderRNN(attn_model, embedding, hidden_size, voc.num_words, decoder_n_layers, dropout) if loadFilename: encoder.load_state_dict(encoder_sd) decoder.load_state_dict(decoder_sd) # Use appropriate device encoder = encoder.to(device) decoder = decoder.to(device) print('Models built and ready to go!') # Ensure dropout layers are in train mode encoder.train() decoder.train() # Initialize optimizers print('Building optimizers ...') encoder_optimizer = optim.Adam(encoder.parameters(), lr=learning_rate)