为什么网络学习效果不好(Tensorflow LSTM用于文本生成)?

2024-05-14 17:04:51 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我最好的病房 从他那一天起,你就成了高贵的人吗?

温根公爵: 这样,人,更何况舌头是一个,你就可以在每堵墙上。

哈利斯:为什么,你的大厅,你对我唯一的男人先生说一句好话,那两个人对你和我来说,这是一个使皱纹变结实的好办法。

哈莱特区:我不愿意清教徒和善良的灵魂,因为他是死了的人,我所做的一切都会给我留下污点

它还在学习,损失了1.73,损失曲线也没有稳定下来。然而,在随后的所有运行(10)中,它并没有达到任何接近良好的结果-例如,在6个时期之后,在我的最佳运行,它损失了2.43,并趋于平稳- 这是输出:

修士? 我的头发怎么样 在我看来这是一个很好的选择 坐在那里,他会给你加上脚趾练习的机会 我不知道你是谁吗 他已经开始在这里换座位了 你这么说我们现在还活着吗 因为我要去旅行 我要告诉你,你要去的地方是什么地方 像一个快乐的人

这与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...")

因为我在一个旧的双核i3上运行它,4gb的内存,网络需要6小时45分钟来经历一个时代。当然,至少需要两个时期才能看到结果。所以不幸的是,我不能继续修改代码并运行和重新运行。我也受到计算机内存的限制,因为4个内存中的1gb分配给了Ubuntu,另外3个分配给了Windows。所以,我只能训练一个小的网络。在

如果有人能提供一个链接到一个预先训练过的网络,我将非常感激


Tags: textfromimport网络outputseqfileprint

热门问题