为什么网络没有学习效果(Tensorflow - 用于文本生成的LSTM)?

时间:2016-09-17 20:45:51

标签: python tensorflow deep-learning lstm

当我最初在莎士比亚语料库上运行2个LSTM网络(每个512个单位)时,我在2个训练时期(每个时期=通过数据集的一个周期)后得到了相当不错的输出。 这是:

我的最佳病房 他,你是一个从他那个时代开始为贵族做出贡献的人吗?

DUKE VINGEN: 所以那个人就是这个,更多地说明舌头是一个人的那个部分,你就是每一面墙。

HALLES: 为什么,你的大厅,你对我唯一的男人和孪生兄弟的好话,对你的永远和我来说,应该成为一个强大的折痕。

HARLET: 我不喜欢那个清真而不是善良的灵魂,因为那些已经死去的人在我身上做了很多污点,而且我确实做了这些事情,并且做了很好的事情并且让他们知道了

它还在学习,损失1.73,损失图没有趋于平稳。然而,在所有后续的运行中(10),它已经无法在接近好的结果的任何地方实现 - 在6个时期之后,例如,在我最好的运行中随后它损失了2.43并且趋于平稳 - 这是输出:

修士? 我的l to mo to to to to to to to to to  在我看来,这是一个不错的选择  坐在地上,向耶和华d hum hum hum hum an an M M m the the the the the the exe exe exe exe exe and and and and and and and and and and and and and and and and and and and and and and and and and and and and and 特丹,特伦特 在这里,我们将为您提供服务  so所以,我们,但是我们应该回到这里,但是在胡锦涛 因为ehmug te to fare t ce 对于他来说,他确实喜欢这种方法,因为他们需要这样做才能实现这一目标。

这类似于4,5,6个时期之后的输出。最常见的是,在初始运行之后的运行中,网络将在损失2.70后平衡学习。

This is the graph of the loss  我也发布了代码:

from __future__ import absolute_import, division, print_function
import os
import numpy as np
import tflearn
from tflearn.data_utils import *
from tflearn.layers.estimator import regression


inputs, targets, char_dict = \
    textfile_to_semi_redundant_sequences("shakespeare_input.txt", seq_maxlen=20) #helper - vectorises text



LSTM = tflearn.input_data([None, 20, len(char_dict)])
LSTM = tflearn.lstm(LSTM, 512, return_seq=True, restore=True, dropout = 0.5)
LSTM = tflearn.lstm(LSTM, 512, restore=True, dropout = 0.5)
LSTM = tflearn.fully_connected(LSTM, len(char_dict), activation='softmax')

LSTM = tflearn.regression(LSTM, optimizer= 'adam', loss='categorical_crossentropy',
                       learning_rate=0.001) 

LSTMmodel = tflearn.SequenceGenerator(LSTM, dictionary=char_dict,
                              seq_maxlen=20,
                              clip_gradients=5.0, tensorboard_verbose=0,tensorboard_dir='pathfile/logs')

#LSTMmodel.load('/pathfile/LSTMmodel.tfl')
for i in range(10):
    print("-- TESTING...")
    starting = random_sequence_from_textfile("shakespeare_input.txt", 20)
    output_path = 'pathfile/epoch_' + str(i) + '_output.txt' 
    generated_output = LSTMmodel.generate(500, temperature=1.0, seq_seed=starting)
    text_file = open(output_path, "w") #save the outputs to a text file - allows us to view progress of model
    text_file.write("With temperature 1.0: \n \n \n") #two different temperatures - higher temp = more novel
    text_file.write(generated_output)
    generated_output = LSTMmodel.generate(500, temperature=0.5, seq_seed=starting) #lower temp = more accurate to original text
    text_file.write("\n \n \n With temperature 0.5: \n \n \n")
    text_file.write(generated_output)
    text_file.close()
    print("-- TRAINING...")
    LSTMmodel.fit(inputs, targets, batch_size=200, n_epoch=1, run_id='Shakespeare_Generator',shuffle = True)
    print("-- SAVING MODEL...")
    if (i%2==0):
        LSTMmodel.save("pathfile/LSTMmodel.tfl")
    else:
        LSTMmodel.save("pathfile//LSTMmodel2.tfl")
    print("-- EPOCH " + str(i+1) +" COMPLETE...")

由于我在带有4GB内存的旧双核i3上运行,因此网络需要6小时45分钟才能完成一个时期。当然,至少需要两个时代才能看到结果。所以不幸的是,我无法继续调整代码并运行和重新运行。我也受到计算机内存的限制,因为4中的1gb分配给Ubuntu - 其他3个分配给Windows。因此,我只能训练一个小网络。

如果有人能提供预先训练好的网络链接,我真的很感激

1 个答案:

答案 0 :(得分:1)

我对这个型号没有答案,但您是否尝试过现有的莎士比亚LSTM示例,就像这个一样?

https://github.com/sherjilozair/char-rnn-tensorflow

训练应该更快一点,如果你从一个有效的例子开始,可能更容易调试它出错的地方。