我正在使用支持GPU的Tensorflow 1.1.0,我有这个功能:
def get_init_cell(batch_size, rnn_size, keep_prob=0.75, layers=2):
"""
Create an RNN Cell and initialize it.
:param batch_size: Size of batches
:param rnn_size: Size of RNNs
:return: Tuple (cell, initialize state)
"""
# Basic LSTM cell
# lstm = tf.contrib.rnn.BasicLSTMCell(rnn_size)
lstm = tf.contrib.rnn.BasicLSTMCell(rnn_size, forget_bias=0.0, state_is_tuple=True, reuse=tf.get_variable_scope().reuse)
# Add drop to the cell
drop = tf.contrib.rnn.DropoutWrapper(lstm, output_keep_prob=keep_prob)
# Stack multiple LSTM layers
rnn_cell = tf.contrib.rnn.MultiRNNCell([drop for _ in range(layers)], state_is_tuple=True)
# Getting an initial state of zeros
initial_state = rnn_cell.zero_state(batch_size, tf.float32)
# Set the name
initial_state = tf.identity(initial_state, 'initial_state')
return rnn_cell, initial_state
如果我使用lstm = tf.contrib.rnn.BasicLSTMCell(rnn_size)
,之前的代码与Tensorflow 1.0.0完美配合,但现在我改变了我获取lstm单元格的方式。我收到了这个错误:
ValueError:尝试重用RNNCell 与其不同的变量范围 第一次使用。细胞的首次使用是在范围内 '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月,我们将开始 转换此单元格的行为以使用现有存储的权重,如果 any,当使用scope = None调用它时(可以导致静默模型) 降级,所以这个错误将一直存在。)
我用这个更改了get_init_cell
:
def lstm_cell(rnn_size, keep_prob=0.75):
# Basic LSTM cell
lstm = tf.contrib.rnn.BasicLSTMCell(rnn_size, reuse=tf.get_variable_scope().reuse)
# Add drop to the cell
return tf.contrib.rnn.DropoutWrapper(lstm, output_keep_prob=keep_prob)
def get_init_cell(batch_size, rnn_size, keep_prob=0.75, layers=2):
"""
Create an RNN Cell and initialize it.
:param batch_size: Size of batches
:param rnn_size: Size of RNNs
:return: Tuple (cell, initialize state)
"""
# Stack multiple LSTM layers
rnn_cell = tf.contrib.rnn.MultiRNNCell([lstm_cell(rnn_size, keep_prob) for _ in range(layers)])
# Getting an initial state of zeros
initial_state = rnn_cell.zero_state(batch_size, tf.float32)
# Set the name
initial_state = tf.identity(initial_state, 'initial_state')
return rnn_cell, initial_state
我仍然遇到同样的错误。
我也尝试过同样的错误:
def get_init_cell(batch_size, rnn_size, keep_prob=0.75, layers=2):
"""
Create an RNN Cell and initialize it.
:param batch_size: Size of batches
:param rnn_size: Size of RNNs
:return: Tuple (cell, initialize state)
"""
def lstm_cell():
# Basic LSTM cell
return tf.contrib.rnn.BasicLSTMCell(rnn_size, reuse=tf.get_variable_scope().reuse)
def attn_cell():
return tf.contrib.rnn.DropoutWrapper(lstm_cell(), output_keep_prob=keep_prob)
# Stack multiple LSTM layers
rnn_cell = tf.contrib.rnn.MultiRNNCell([attn_cell() for _ in range(layers)], state_is_tuple=True)
# Getting an initial state of zeros
initial_state = rnn_cell.zero_state(batch_size, tf.float32)
# Set the name
initial_state = tf.identity(initial_state, 'initial_state')
return rnn_cell, initial_state
有什么想法吗?