python - 如何从 tensorflow 中的 RNN 模型中提取细胞状态和隐藏状态?

标签 python tensorflow lstm

我是 TensorFlow 的新手,很难理解 RNN 模块。我正在尝试从 LSTM 中提取隐藏/单元格状态。 对于我的代码,我使用来自 https://github.com/aymericdamien/TensorFlow-Examples 的实现.

# tf Graph input
x = tf.placeholder("float", [None, n_steps, n_input])
y = tf.placeholder("float", [None, n_classes])

# Define weights
weights = {'out': tf.Variable(tf.random_normal([n_hidden, n_classes]))}
biases = {'out': tf.Variable(tf.random_normal([n_classes]))}

def RNN(x, weights, biases):
    # Prepare data shape to match `rnn` function requirements
    # Current data input shape: (batch_size, n_steps, n_input)
    # Required shape: 'n_steps' tensors list of shape (batch_size, n_input)

    # Permuting batch_size and n_steps
    x = tf.transpose(x, [1, 0, 2])
    # Reshaping to (n_steps*batch_size, n_input)
    x = tf.reshape(x, [-1, n_input])
    # Split to get a list of 'n_steps' tensors of shape (batch_size, n_input)
    x = tf.split(0, n_steps, x)

    # Define a lstm cell with tensorflow
    #with tf.variable_scope('RNN'):
    lstm_cell = rnn_cell.BasicLSTMCell(n_hidden, forget_bias=1.0, state_is_tuple=True)

    # Get lstm cell output
        outputs, states = rnn.rnn(lstm_cell, x, dtype=tf.float32)

    # Linear activation, using rnn inner loop last output
    return tf.matmul(outputs[-1], weights['out']) + biases['out'], states

pred, states = RNN(x, weights, biases)

# Define loss and optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

# Evaluate model
correct_pred = tf.equal(tf.argmax(pred,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
# Initializing the variables
init = tf.initialize_all_variables()

现在我想为预测中的每个时间步提取单元/隐藏状态。状态存储在形式为 (c,h) 的 LSTMStateTuple 中,我可以通过评估 print states 找到它。但是,尝试调用 print states.c.eval()(根据文档应该给我张量 states.c 中的值),会产生一个错误,指出即使我在预测某事后立即调用它,我的变量也没有初始化。代码在这里:

# Launch the graph
with tf.Session() as sess:
    sess.run(init)
    step = 1
    # Keep training until reach max iterations
    for v in tf.get_collection(tf.GraphKeys.VARIABLES, scope='RNN'):
        print v.name
    while step * batch_size < training_iters:
        batch_x, batch_y = mnist.train.next_batch(batch_size)
        # Reshape data to get 28 seq of 28 elements
        batch_x = batch_x.reshape((batch_size, n_steps, n_input))
        # Run optimization op (backprop)
        sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})

        print states.c.eval()
        # Calculate batch accuracy
        acc = sess.run(accuracy, feed_dict={x: batch_x, y: batch_y})

        step += 1
    print "Optimization Finished!"

错误信息是

InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float
     [[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

状态在 tf.all_variables() 中也不可见,只有经过训练的矩阵/偏差张量(如此处所述:Tensorflow: show or save forget gate values in LSTM)。我不想从头开始构建整个 LSTM,因为我在 states 变量中有状态,我只需要调用它。

最佳答案

您可以像收集准确度一样简单地收集状态的值。

我想,pred, states, acc = sess.run(pred, states, accuracy, feed_dict={x: batch_x, y: batch_y}) 应该工作得很好。

关于python - 如何从 tensorflow 中的 RNN 模型中提取细胞状态和隐藏状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40272567/

相关文章:

python 多处理共享变量安全

Python 日志记录 StreamHandler 不从模块记录日志

python - 使用 pandas 或 Numpy 根据列数据选择一系列行

python - tensorflow 中图像的有效加权和

python - 使用 tensorflow 进行增量模型训练

Python 正则表达式没有返回任何重复的内容?

tensorflow - 有什么方法可以使用tensorflow-federated API对真实的多台机器进行联邦学习吗?

python - 无法在 tensorflow 中训练玩具 LSTM

machine-learning - 如何在 Keras 中配置双向 LSTM 的输入形状

python - 如何在 LSTM Keras 中为多步多变量准备时间序列数据