教程中RNN示例代码中的”变量权重已存在”

我想重新实现来自https://www.tensorflow.org/api_docs/python/tf/contrib/rnn/static_rnn的RNN步骤循环,但对我来说不起作用。我在不重用时得到“变量 test/basic_lstm_cell/weights 已存在”的错误,而在将重用设置为True时得到“变量 test/basic_lstm_cell/weights 不存在”的错误。

import tensorflow as tfbatch_size = 32n_steps = 10lstm_size = 10n_input = 17words = 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)

回答:

请查看你试图重新实现的函数的源代码。重要的是,在循环的第一次迭代中不设置重用标志,但在所有其他迭代中设置。所以在你的情况下,一个包含循环的作用域并在整个作用域内保持标志恒定是行不通的,你需要做类似下面的操作:

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)

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注