python - 教程中的 RNN 示例代码中的 "Variable weights already exists"

标签 python machine-learning tensorflow lstm recurrent-neural-network

我想从https://www.tensorflow.org/api_docs/python/tf/contrib/rnn/static_rnn重新实现RNN步进循环 但这对我不起作用。 当重用设置为 True 时,我得到“变量 test/basic_lstm_cell/weights 已存在”而没有重用,并且“变量 test/basic_lstm_cell/weights 不存在”。

import tensorflow as tf
batch_size = 32
n_steps = 10
lstm_size = 10
n_input = 17

words = tf.placeholder(tf.float32, [batch_size, n_steps, n_input])
words = tf.transpose(words, [1, 0, 2])
words = tf.reshape(words, [-1, n_input])
words = tf.split(words, n_steps, 0)

with tf.variable_scope('test', reuse=True):
    cell = tf.contrib.rnn.BasicLSTMCell(lstm_size)
    state = cell.zero_state(batch_size, dtype=tf.float32)
    outputs = []
    for input_ in words:
        output, state = cell(input_, state)
        outputs.append(output)

最佳答案

看看the source of the function you are trying to re-implement 。重要的是,重用标志不会在循环的第一次迭代中设置,但会在所有其他迭代中设置。因此,在您的情况下,包含带有该范围标志常量的循环的一个作用域将不起作用,您必须执行类似的操作

with tf.variable_scope('test') as scope:
    cell = tf.contrib.rnn.BasicLSTMCell(lstm_size)
    state = cell.zero_state(batch_size, dtype=tf.float32)
    outputs = []
    for step, input_ in enumerate(words):
        if step > 0:
            scope.reuse_variables()
        output, state = cell(input_, state)
        outputs.append(output)

关于python - 教程中的 RNN 示例代码中的 "Variable weights already exists",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42997659/

相关文章:

python - 使用 matplotlib 的动态图在一段时间后变慢

python - 您是否必须为部署选择 pickle scaler 和 ML 模型?

machine-learning - 是否可以对图像序列使用图像预处理?

python - 使用 JS 后端和 Python 进行机器学习

python - Keras - 如何获得训练中每一层所花费的时间?

Python函数替换输入变量

python - 在 Python 中,当遍历列表中的项目时,如果到达末尾,如何从 0 开始?

python - Spark 最快的创建 numpy 数组 RDD 的方法

python - 分析数据框中分类变量的变化

python-3.x - 使用 SSE4.2 和 AVX 重新编译 Tensorflow