Exemple #1
0
dropout_p = 0.05
teacher_forcing_ratio = .5
clip = 5.
criterion = nn.NLLLoss()

# Initialize models
encoder = EncoderRNN(input_lang.n_words, hidden_size, n_layers)
decoder = AttentionDecoderRNN(attn_model,
                              hidden_size,
                              output_lang.n_words,
                              n_layers,
                              dropout_p=dropout_p)

learning_rate = 1
encoder_optimizer = optim.Adam(encoder.parameters(), lr=learning_rate)
decoder_optimizer = optim.Adam(decoder.parameters(), lr=learning_rate)

# Load model parameters
encoder.load_state_dict(torch.load(
    './data/encoder_params_{}'.format(language)))
decoder.load_state_dict(torch.load(
    './data/decoder_params_{}'.format(language)))
decoder.attention.load_state_dict(
    torch.load('./data/attention_params_{}'.format(language)))

# Move models to GPU
# encoder.cuda()
# decoder.cuda()


def GetData(input_data):
Exemple #2
0
input_lang, output_lang, pairs = etl.prepare_data(args.language)

# Initialize models
encoder = EncoderRNN(input_lang.n_words, args.embedding_size, args.hidden_size,
                     args.n_layers, args.dropout)

decoder = AttentionDecoderRNN(output_lang.n_words, args.embedding_size,
                              args.hidden_size, args.attn_model, args.n_layers,
                              args.dropout)
# Move models to device
encoder = encoder.to(device)
decoder = decoder.to(device)

# Initialize optimizers and criterion
encoder_optimizer = optim.Adam(encoder.parameters(), lr=args.lr)
decoder_optimizer = optim.Adam(decoder.parameters(), lr=args.lr)
criterion = nn.NLLLoss()

# Keep track of time elapsed and running averages
start = time.time()
plot_losses = []
print_loss_total = 0  # Reset every print_every
plot_loss_total = 0  # Reset every plot_every

# Begin training
for epoch in range(1, args.n_epochs + 1):
    # Get training data for this cycle
    training_pair = etl.tensor_from_pair(random.choice(pairs), input_lang,
                                         output_lang)
    input = training_pair[0]
    target = training_pair[1]