TensorFlow (v1.1.0) Multi-RNN BasicLSTMCell 错误('reuse' 参数)Python 3.5

TensorFlow (v1.1.0) Multi-RNN BasicLSTMCell Error ('reuse' parameter) Python 3.5

扩展:What is the use of a "reuse" parameter of tf.contrib.layers functions?

问题:虽然这个问题已经在 github 上提出,并且可能会在 TensorFlow 的另一个版本中解决,但我暂时没有找到现有的解决方案;在此期间是否有权宜之计?

代码:

state_size = 4
def lstm_cell():
    if 'reuse' in inspect.getargspec(tf.contrib.rnn.BasicLSTMCell.__init__).args:
        return tf.contrib.rnn.BasicLSTMCell(state_size, forget_bias=0.0, state_is_tuple=True, reuse=tf.get_variable_scope().reuse)
    else:
        return tf.contrib.rnn.BasicLSTMCell(state_size, forget_bias=0.0, state_is_tuple=True)

cell = lstm_cell()
cell = rnn.DropoutWrapper(cell, output_keep_prob=0.5)
cell = rnn.MultiRNNCell([cell] * num_layers, state_is_tuple=True)
states_series, current_state = tf.nn.dynamic_rnn(cell, tf.expand_dims(batchX_placeholder, -1), initial_state=rnn_tuple_state)
states_series = tf.reshape(states_series, [-1, state_size])

函数 lstm_cell() 是 https://github.com/tensorflow/models/blob/master/tutorials/rnn/ptb/ptb_word_lm.py 的建议。它解释了最新版本的 tensorflow 包含 BasicLSTMCell() 的 'reuse' 参数。

在此代码中,如果我将重用设置为 False,tf.nn.dynamic_rnn 行会产生错误:

如果我将重用设置为 True,错误是:

最后,将 'scope=None' 添加到 dynamic_rnn 也没有任何区别。

您是否考虑过尝试 'reuse to True'-错误提示的内容?

If before you were using: MultiRNNCell([BasicLSTMCell(...)] * num_layers), change to: MultiRNNCell([BasicLSTMCell(...) for _ in range(num_layers)]).

以下代码片段适合我(已回答

def lstm_cell():
    cell = tf.contrib.rnn.NASCell(state_size, reuse=tf.get_variable_scope().reuse)
    return tf.contrib.rnn.DropoutWrapper(cell, output_keep_prob=0.8)

rnn_cells = tf.contrib.rnn.MultiRNNCell([lstm_cell() for _ in range(num_layers)], state_is_tuple = True)
outputs, current_state = tf.nn.dynamic_rnn(rnn_cells, x, initial_state=rnn_tuple_state)