ValueError:尝试使用第二个RNNCell使用已经具有权重的变量范围的权重

时间:2017-03-28 04:50:20

标签: python tensorflow

我运行Tensorflow 1.0提供的ptb_word_lm.py,但它显示了以下消息:

  

ValueError:尝试让第二个RNNCell使用已经具有权重的变量范围的权重:   '型号/ RNN / multi_rnn_cell / cell_0 / basic_lstm_cell';而细胞是   不构造为BasicLSTMCell(...,reuse = True)。分享   RNNCell的权重,只需在第二次计算中重复使用,或者   使用参数reuse = True创建一个新的。

然后我修改代码,将'reuse=True'添加到BasicLSTMCell,但它会显示以下消息:

  

ValueError:变量模型/ RNN / multi_rnn_cell / cell_0 / basic_lstm_cell / weights不   存在,或者不是用tf.get_variable()创建的。你的意思是设置   在VarScope中重用=无?

我怎么能解决这个问题?

5 个答案:

答案 0 :(得分:1)

以下对我有用:

with tf.variable_scope('rnn'):
    outputs, final_state = tf.nn.dynamic_rnn(lstm_cell, X_in, initial_state=init_state, time_major=False, scope='rnn')

答案 1 :(得分:0)

向BasicLSTMCell添加reuse = tf.get_variable_scope().reuse对我来说没问题。

答案 2 :(得分:0)

它修改lstm_cell()如下:

def lstm_cell():
    if 'reuse' in inspect.signature(tf.contrib.rnn.BasicLSTMCell.__init__).parameters:
        return tf.contrib.rnn.BasicLSTMCell(size, forget_bias=0.0,
                                     state_is_tuple=True, 
                                     reuse=tf.get_variable_scope().reuse)
    else:
        return tf.contrib.rnn.BasicLSTMCell(
                                     size, forget_bias=0.0, state_is_tuple=True)

我的环境:

Windows 10_x64

tensorflow-gpu:1.1.0

测试没问题。

答案 3 :(得分:0)

只需在代码顶部添加以下行:

tf.reset_default_graph()

答案 4 :(得分:0)

您可以尝试在scope='lstmrnn'功能中添加tf.nn.dynamic_rnn()