TensorFlow LSTM生成模型 [英] TensorFlow LSTM Generative Model

查看:100
本文介绍了TensorFlow LSTM生成模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究LSTM语言模型教程在此处讨论了.

I'm working off the LSTM language model tutorial discussed here.

对于语言模型,通常在训练后使用该模型从头开始生成新句子(即,来自模型的样本).

With language models, it's common to use the model to generate a new sentence from scratch after training (i.e. sample from the model).

我是TensorFlow的新手,但是我试图使用经过训练的模型来生成新词,直到句子结束标记为止.

I'm new to TensorFlow but I'm trying to use my trained model to generate new words until the end-of-sentence marker.

我最初的尝试:

x = tf.zeros_like(m.input_data)
state = m.initial_state.eval()
for step in xrange(m.num_steps):
    state = session.run(m.final_state,
                               {m.input_data: x,
                                m.initial_state: state})
    x = state

它失败并显示错误:

ValueError:设置具有序列的数组元素.

ValueError: setting an array element with a sequence.

推荐答案

此处的问题似乎是通过session.run()传递的feed_dict中的m.input_data: x映射.在这种情况下,TensorFlow期望x是一个numpy数组(或某些可以隐式转换为numpy数组的对象),但是该值是TensorFlow Tensor(tf.zeros_like()的结果).

The issue here seems to be the m.input_data: x mapping in the feed_dict passed session.run(). In this case, TensorFlow expects that x is a numpy array (or some object that can be implicitly converted to a numpy array), but the value is a TensorFlow Tensor (the result of tf.zeros_like()).

幸运的是,解决方案很简单.将x = tf.zeros_like(m.input_data)替换为以下内容:

Fortunately, the solution is simple. Replace x = tf.zeros_like(m.input_data) with the following:

x = tf.zeros_like(m.input_data).eval()

...这可确保将x转换为numpy数组.

...which ensures that x is converted to a numpy array.

(请注意,更直接的方法是将初始x构造为适当大小的numpy数组.)

(Note that a more direct way to achieve this would be to construct the initial x as a numpy array of the appropriate size.)

这篇关于TensorFlow LSTM生成模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆