tensorflow:LSTM 单元中变量的初始化器

tensorflow: Initializer for variable in LSTM cell

我正在尝试构建一个 RNN 来预测输入数据的情绪是正面还是负面。

tf.reset_default_graph()

input_data = tf.placeholder(tf.int32, [batch_size, 40])
labels = tf.placeholder(tf.int32, [batch_size, 40])

data = tf.Variable(tf.zeros([batch_size, 40, 50]), dtype=tf.float32)
data = tf.nn.embedding_lookup(glove_embeddings_arr, input_data)

lstm_cell = tf.contrib.rnn.BasicLSTMCell(lstm_units)
lstm_cell = tf.contrib.rnn.DropoutWrapper(cell = lstm_cell, output_keep_prob = 0.75)
value,state = tf.nn.dynamic_rnn(lstm_cell, data, dtype=tf.float32)

weight = tf.Variable(tf.truncated_normal([lstm_units, classes]))
bias = tf.Variable(tf.constant(0.1, shape = [classes]))
value = tf.transpose(value, [1,0,2])
last = tf.gather(value, int(value.get_shape()[0]) - 1)
prediction = (tf.matmul(last, weight) + bias)



true_pred = tf.equal(tf.argmax(prediction, 1), tf.argmax(labels,1))
accuracy = tf.reduce_mean(tf.cast(true_pred,tf.float32))

loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=labels))
optimizer = tf.train.AdamOptimizer().minimize(loss)

解释器returns

ValueError: An initializer for variable rnn/basic_lstm_cell/kernel of <dtype: 'string'> is required

谁能给我解释一下这个错误?

问题是您(很可能)将原始输入文本提供给网络。这不在您的代码段中,但错误指示 <dtype: 'string'>:

ValueError: An initializer for variable rnn/basic_lstm_cell/kernel of <dtype: 'string'> is required

类型是从 LSTM 单元获得的输入中推导出来的。内部 LSTM 变量(kernelbias)使用默认初始化程序进行初始化,该初始化程序(至少现在)只能处理 ,但无法处理其他类型。在您的情况下,类型是 tf.string,这就是您看到此错误的原因。

现在,您应该做的是将输入的句子转换为实向量。最好的方法是通过 word embedding, e.g. word2vec, but a simple word indexing is also possible. Take a look at this post,尤其是它们如何处理文本数据。还有一个完整的工作代码示例。