【问题标题】:Tensorflow running two RNNs: Variable hidden/RNN/LSTMCell/W_0 already existsTensorflow 运行两个 RNN:变量 hidden/RNN/LSTMCell/W_0 已经存在
【发布时间】:2016-10-11 15:22:53
【问题描述】:

我试图同时运行两个 RNN 并将它们的输出连接在一起,方法是为每个 rnn_cell.LSTMCell 定义两个变量范围。为什么我会收到此变量已存在错误??

ValueError: 变量 hidden/RNN/LSTMCell/W_0 已经存在, 不允许。您的意思是在 VarScope 中设置 reuse=True 吗?

为什么是“hidden/RNN/LSTMCell/W_0”而不是“hidden/forward_lstm_cell/RNN/LSTMCell/W_0”???

    with tf.variable_scope('hidden', reuse=reuse): #reuse=None during training
        with tf.variable_scope('forward_lstm_cell'):
            lstm_fw_cell = tf.nn.rnn_cell.LSTMCell(num_units=self.num_hidden, use_peepholes=False, 
                                                initializer=tf.random_uniform_initializer(-0.003, 0.003),
                                                state_is_tuple=True)
            if not reuse:
                lstm_fw_cell = tf.nn.rnn_cell.DropoutWrapper(cell=lstm_fw_cell, input_keep_prob=0.7)

        with tf.variable_scope('backward_lstm_cell'):
            lstm_bw_cell = tf.nn.rnn_cell.LSTMCell(num_units=self.num_hidden, use_peepholes=False, 
                                                forget_bias=0.0, 
                                                initializer=tf.random_uniform_initializer(-0.003, 0.003),
                                                state_is_tuple=True)
            if not reuse:
                lstm_bw_cell = tf.nn.rnn_cell.DropoutWrapper(cell=lstm_bw_cell, input_keep_prob=0.7)

        with tf.name_scope("forward_lstm"):
            outputs_fw, output_states_fw  = tf.nn.dynamic_rnn(
                cell=lstm_fw_cell,
                inputs=embed_inputs_fw,
                dtype=tf.float32,
                sequence_length=self.seq_len_l
                )

        with tf.name_scope("backward_lstm"):
            outputs_bw, output_states_bw  = tf.nn.dynamic_rnn(
                cell=lstm_bw_cell,
                inputs=embed_inputs_bw,
                dtype=tf.float32,
                sequence_length=self.seq_len_r
                )

【问题讨论】:

    标签: neural-network tensorflow recurrent-neural-network


    【解决方案1】:

    只需使用tf.variable_scope 而不是tf.name_scopetf.name_scope 不会为 with tf.get_variable() 创建的变量添加前缀。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多