TensorFlow练习7: 基于RNN生成古诗词

RNN不像传统的神经网络-它们的输出输出是固定的,而RNN允许我们输入输出向量序列。RNN是为了对序列数据进行建模而产生的。

样本序列性:样本间存在顺序关系,每个样本和它之前的样本存在关联。比如说,在文本中,一个词和它前面的词是有关联的;在气象数据中,一天的气温和前几天的气温是有关联的。

例如本帖要使用RNN生成古诗,你给它输入一堆古诗词,它会学着生成和前面相关联的字词。如果你给它输入一堆姓名,它会学着生成姓名;给它输入一堆古典乐/歌词,它会学着生成古典乐/歌词,甚至可以给它输入源代码。

关于RNN:

本帖代码移植自char-rnn,它是基于Torch的洋文模型,稍加修改即可应用于中文。char-rnn使用文本文件做为输入、训练RNN模型,然后使用它生成和训练数据类似的文本。

使用的数据集:全唐诗(43030首):https://pan.baidu.com/s/1o7QlUhO

训练:

使用训练好的模型生成古诗:

生成的“诗词”(至少格式对上了):

生成藏头诗:

上面使用的TensroFlow版本为0.11,0.12版本貌似不能直接运行,简单修改如下代码:

读取模型的方法:

tf.initialize_all_variables()  deprecated,使用tf.global_variables_initializer()替代。

相关文章

《TensorFlow练习7: 基于RNN生成古诗词》有43个想法

  1. 博主,你好。我模型生成后,运行生成“诗歌”代码时,
    在第125行的gen_poetry()函数中的
    [probs_, state_] = sess.run([probs, last_state], feed_dict={input_data: x, initial_state: state_})

    出现Cannot feed value of shape (1, 1) for Tensor ‘Placeholder:0’, which has shape ‘(64, ?)’ 错误,运行生成“诗歌”代码时,需要对输入x进行怎样的处理啊?

  2. tensorflow.python.framework.errors_impl.NotFoundError: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for poetry.module-49
    这个是怎么回事?

  3. 我的是1.0.0版本,训练时报错了AttributeError: module ‘tensorflow.python.ops.nn’ has no attribute ‘rnn_cell’ 这个应该怎么改

  4. Traceback (most recent call last):
    File “D:/pycode/tensorflow/analy_test/TensorFlow_flight/word2vec_/quickly_test.py”, line 180, in
    print(gen_poetry())
    File “D:/pycode/tensorflow/analy_test/TensorFlow_flight/word2vec_/quickly_test.py”, line 166, in gen_poetry
    initial_state: state_})
    File “C:\Users\Lenovo\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py”, line 767, in run
    run_metadata_ptr)
    File “C:\Users\Lenovo\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py”, line 938, in _run
    np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)
    File “C:\Users\Lenovo\AppData\Local\Programs\Python\Python35\lib\site-packages\numpy\core\numeric.py”, line 531, in asarray
    return array(a, dtype, copy=False, order=order)
    TypeError: int() argument must be a string, a bytes-like object or a number, not ‘NoneType’

  5. 请问下 这个报错是什么原因啊
    W tensorflow/core/framework/op_kernel.cc:975] Not found: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for poetry.module-49
    W tensorflow/core/framework/op_kernel.cc:975] Not found: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for poetry.module-49
    W tensorflow/core/framework/op_kernel.cc:975] Not found: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for poetry.module-49
    W tensorflow/core/framework/op_kernel.cc:975] Not found: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for poetry.module-49
    W tensorflow/core/framework/op_kernel.cc:975] Not found: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for poetry.module-49
    W tensorflow/core/framework/op_kernel.cc:975] Not found: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for poetry.module-49
    W tensorflow/core/framework/op_kernel.cc:975] Not found: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for poetry.module-49

      1. Unable to open table file .\poetry.module-49: Data loss: file is too short to be an sstable: perhaps your file is in a different file format
        这个怎么改

  6. 大神,请问这是什么问题 用什么办法解决 ValueError: Variable rnnlm/softmax_w already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at:

  7. Traceback (most recent call last):
    File “/home/scj/doc/tf/poetry/poetry_rnn2.py”, line 155, in
    print(gen_poetry())
    File “/home/scj/doc/tf/poetry/poetry_rnn2.py”, line 141, in gen_poetry
    [probs_, state_] = sess.run([probs, last_state], feed_dict={input_data: x, initial_state: state_})
    File “/home/scj/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py”, line 766, in run
    run_metadata_ptr)
    File “/home/scj/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py”, line 943, in _run
    % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
    ValueError: Cannot feed value of shape (1, 1) for Tensor ‘Placeholder:0’, which has shape ‘(64, ?)’
    这个怎样修改。

  8. 丛焰长辙妒,此去遍三年。
    我筒终无力,阴中有旧游。
    西百竹中欲,草转五天松。

    归意园鱼去,钵旨作寺情。
    日正谷林外,风来达授还。
    从兹储舆绕,新装莫堪泛。

    就是没有意义的唐诗

  9. 你好,我想问一下,最后生成古诗的时候的“to_word(weights)函数”的意义。我的理解中,这个函数是在随机生成下一个字,貌似和网络输出的概率没有什么关系。那么这个函数又是如何体现前一个字和后一个的关系的呢?

  10. 感谢博主,成功生成古诗词:
    不?期花起怔,紲陼赏闲魗。之生头綽兰,门间花寒松
    盖贯下于掌,碫何香徆櫘。抮入齏几里,客仙青归路
    乆有谢州别,寒屝暮畦出。孯更春花雪,难随萑剆初。
    火摃将淯晗堀,孤何白山晛来。院少芳娘水,从作向移新?

  11. 站长,from tensorflow.python.ops import rnn_cell
    ImportError: cannot import name rnn_cell
    请问是TF版本太低了吗?有什么方法解决?谢谢

发表评论

电子邮件地址不会被公开。 必填项已用*标注