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 行会产生错误:
- “值错误:变量
rnn/multi_rnn_cell/cell_0/basic_lstm_cell/weights 已经存在,
不允许。您的意思是在 VarScope 中设置 reuse=True 吗?起初
定义于:..."
如果我将重用设置为 True
,错误是:
- "ValueError: 尝试重用 RNNCell
与第一个变量范围不同
采用。第一次使用 cell 是在范围内
'rnn/multi_rnn_cell/cell_0/basic_lstm_cell',本次尝试与
范围 'rnn/multi_rnn_cell/cell_1/basic_lstm_cell'。请创建一个
单元格的新实例,如果您希望它使用不同的集合
重量。如果在您使用之前:
MultiRNNCell([BasicLSTMCell(...)] * num_layers),改为:
MultiRNNCell([BasicLSTMCell(...) for _ in range(num_layers)])。如果
在您使用相同的单元格实例作为前向和
双向 RNN 的反向单元,只需创建两个实例(一个
一个用于正向,一个用于反向)。 2017年5月,我们将开始
将此单元格的行为转换为使用现有的存储权重,如果
任何,当使用 scope=None 调用时(这会导致静默
模型退化,因此此错误将一直存在。)"
最后,将 '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)
扩展: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 行会产生错误:
- “值错误:变量 rnn/multi_rnn_cell/cell_0/basic_lstm_cell/weights 已经存在, 不允许。您的意思是在 VarScope 中设置 reuse=True 吗?起初 定义于:..."
如果我将重用设置为 True
,错误是:
- "ValueError: 尝试重用 RNNCell 与第一个变量范围不同 采用。第一次使用 cell 是在范围内 'rnn/multi_rnn_cell/cell_0/basic_lstm_cell',本次尝试与 范围 'rnn/multi_rnn_cell/cell_1/basic_lstm_cell'。请创建一个 单元格的新实例,如果您希望它使用不同的集合 重量。如果在您使用之前: MultiRNNCell([BasicLSTMCell(...)] * num_layers),改为: MultiRNNCell([BasicLSTMCell(...) for _ in range(num_layers)])。如果 在您使用相同的单元格实例作为前向和 双向 RNN 的反向单元,只需创建两个实例(一个 一个用于正向,一个用于反向)。 2017年5月,我们将开始 将此单元格的行为转换为使用现有的存储权重,如果 任何,当使用 scope=None 调用时(这会导致静默 模型退化,因此此错误将一直存在。)"
最后,将 '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)