教程中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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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