예제 #1
0
# input words num
input_size = lang_dataset.input_lang_words
hidden_size = 256
# output words num
output_size = lang_dataset.output_lang_words
total_epoch = 20

encoder = EncoderRNN(input_size, hidden_size)
decoder = DecoderRNN(hidden_size, output_size, n_layers=2)
attn_decoder = AttnDecoderRNN(hidden_size, output_size, n_layers=2)
use_attn = True

if torch.cuda.is_available():
    encoder = encoder.cuda()
    decoder = decoder.cuda()
    attn_decoder = attn_decoder.cuda()


def show_plot(points):
    plt.figure()
    x = np.arange(len(points))
    plt.plot(x, points)
    plt.show()


def train(encoder, decoder, total_epoch, use_attn):
    param = list(encoder.parameters()) + list(decoder.parameters())
    optimizer = optim.Adam(param, lr=1e-3)
    criterion = nn.NLLLoss()
    plot_losses = []
    for epoch in range(total_epoch):
        print('')


input_size = lang_dataset.input_lang_words
hidden_size = 256
output_size = lang_dataset.output_lang_words

encoder = EncoderRNN(input_size, hidden_size)
encoder.load_state_dict(torch.load('./encoder.pth'))
if use_attn:
    decoder = AttnDecoderRNN(hidden_size, output_size, n_layers=2)
    decoder.load_state_dict(torch.load('./attn_decoder.pth'))
else:
    decoder = DecoderRNN(hidden_size, output_size, n_layers=2)
    decoder.load_state_dict(torch.load('./decoder.pth'))

if use_cuda:
    encoder = encoder.cuda()
    decoder = decoder.cuda()

evaluateRandomly(encoder, decoder)

if use_attn:
    pair_idx = random.choice(list(range(len(lang_dataset))))
    pairs = lang_dataset.pairs[pair_idx]
    print('>')
    print(pairs[0])
    in_lang, out_lang = lang_dataset[pair_idx]
    output_words, attentions = evaluate(encoder, decoder, in_lang)
    plt.matshow(attentions.cpu().numpy())
    plt.show()
예제 #3
0
        print('')


input_size = lang_dataset.input_lang_words
hidden_size = 256
output_size = lang_dataset.output_lang_words

encoder = EncoderRNN(input_size, hidden_size)
encoder.load_state_dict(torch.load('./encoder.pth'))
if use_attn:
    decoder = AttnDecoderRNN(hidden_size, output_size, n_layers=2)
    decoder.load_state_dict(torch.load('./attn_decoder.pth'))
else:
    decoder = DecoderRNN(hidden_size, output_size, n_layers=2)
    decoder.load_state_dict(torch.load('./decoder.pth'))

if use_cuda:
    encoder = encoder.cuda()
    decoder = decoder.cuda()

evaluateRandomly(encoder, decoder)

if use_attn:
    pair_idx = random.choice(list(range(len(lang_dataset))))
    pairs = lang_dataset.pairs[pair_idx]
    print('>')
    print(pairs[0])
    in_lang, out_lang = lang_dataset[pair_idx]
    output_words, attentions = evaluate(encoder, decoder, in_lang)
    plt.matshow(attentions.cpu().numpy())
    plt.show()
print()

input_size = lang_dataset.input_lang_words
hidden_size = 256
output_size = lang_dataset.output_lang_words
total_epoch = 20

encoder = EncoderRNN(input_size, hidden_size)
decoder = DecoderRNN(hidden_size, output_size, n_layers=2)
attn_decoder = AttnDecoderRNN(hidden_size, output_size, n_layers=2)
use_attn = True

if torch.cuda.is_available():
    encoder = encoder.cuda()
    decoder = decoder.cuda()
    attn_decoder = attn_decoder.cuda()


def showPlot(points):
    plt.figure()
    x = np.arange(len(points))
    plt.plot(x, points)
    plt.show()


def train(encoder, decoder, total_epoch, use_attn):

    param = list(encoder.parameters()) + list(decoder.parameters())
    optimizer = optim.Adam(param, lr=1e-3)
    criterion = nn.NLLLoss()
    plot_losses = []