无法在张量流中创建 MultiRNN 单元
Not able to create MultiRNN cell in tensorflow
我正在尝试创建一个 RNN 来生成下一个单词下面是代码
当我只提供 BasicLSTMCell 时,我没有收到错误并获得预期的输出
tf.reset_default_graph()
X = tf.placeholder(name="X", shape=[None, 3, 1], dtype=tf.float32)
Y = tf.placeholder(name="Y", shape=[None, uniqueWordsLength], dtype=tf.float32)
Xs = [tf.squeeze(seq, [1]) for seq in tf.split(X, 3, 1)]
print(len(Xs))
n_cells=200
n_layers=2
cells = tf.contrib.rnn.BasicLSTMCell(num_units=n_cells, state_is_tuple=True, forget_bias=1.0)
initial_state = cells.zero_state(tf.shape(X)[0], tf.float32)
outputs, states = tf.nn.dynamic_rnn(cells, X, dtype=tf.float32)
然而,当我如下添加具有两层的 Multi RNN Cell 时,出现错误
tf.reset_default_graph()
X = tf.placeholder(name="X", shape=[None, 3, 1], dtype=tf.float32)
Y = tf.placeholder(name="Y", shape=[None, uniqueWordsLength], dtype=tf.float32)
Xs = [tf.squeeze(seq, [1]) for seq in tf.split(X, 3, 1)]
print(len(Xs))
n_cells=200
n_layers=2
cells = tf.contrib.rnn.BasicLSTMCell(num_units=n_cells, state_is_tuple=True, forget_bias=1.0)
initial_state = cells.zero_state(tf.shape(X)[0], tf.float32)
cells = tf.contrib.rnn.MultiRNNCell([cells] * n_layers, state_is_tuple=True)
outputs, states = tf.nn.dynamic_rnn(cells, X, dtype=tf.float32)
错误是
ValueError: Dimensions must be equal, but are 400 and 201 for 'rnn/while/rnn/multi_rnn_cell/cell_0/cell_0/basic_lstm_cell/MatMul_1' (op: 'MatMul') with input shapes: [?,400], [201,800].
请帮忙
对于MultiRNNCell
,您需要为每个图层创建不同 个单元格。在您的代码中,您实际上是在尝试重复使用完全相同的单元格 n_layers
次。这可能会导致问题,例如当输入的维数与单元输出的维数不同时。除此之外,无论如何都不推荐,因为每一层都会使用相同的参数。
相反,像这样创建不同的单元格:
cell_list = [tf.contrib.rnn.BasicLSTMCell(num_units=n_cells, state_is_tuple=True, forget_bias=1.0)
for _ in range(n_layers)]
cells = tf.contrib.rnn.MultiRNNCell(cell_list, state_is_tuple=True)
我正在尝试创建一个 RNN 来生成下一个单词下面是代码
当我只提供 BasicLSTMCell 时,我没有收到错误并获得预期的输出
tf.reset_default_graph()
X = tf.placeholder(name="X", shape=[None, 3, 1], dtype=tf.float32)
Y = tf.placeholder(name="Y", shape=[None, uniqueWordsLength], dtype=tf.float32)
Xs = [tf.squeeze(seq, [1]) for seq in tf.split(X, 3, 1)]
print(len(Xs))
n_cells=200
n_layers=2
cells = tf.contrib.rnn.BasicLSTMCell(num_units=n_cells, state_is_tuple=True, forget_bias=1.0)
initial_state = cells.zero_state(tf.shape(X)[0], tf.float32)
outputs, states = tf.nn.dynamic_rnn(cells, X, dtype=tf.float32)
然而,当我如下添加具有两层的 Multi RNN Cell 时,出现错误
tf.reset_default_graph()
X = tf.placeholder(name="X", shape=[None, 3, 1], dtype=tf.float32)
Y = tf.placeholder(name="Y", shape=[None, uniqueWordsLength], dtype=tf.float32)
Xs = [tf.squeeze(seq, [1]) for seq in tf.split(X, 3, 1)]
print(len(Xs))
n_cells=200
n_layers=2
cells = tf.contrib.rnn.BasicLSTMCell(num_units=n_cells, state_is_tuple=True, forget_bias=1.0)
initial_state = cells.zero_state(tf.shape(X)[0], tf.float32)
cells = tf.contrib.rnn.MultiRNNCell([cells] * n_layers, state_is_tuple=True)
outputs, states = tf.nn.dynamic_rnn(cells, X, dtype=tf.float32)
错误是
ValueError: Dimensions must be equal, but are 400 and 201 for 'rnn/while/rnn/multi_rnn_cell/cell_0/cell_0/basic_lstm_cell/MatMul_1' (op: 'MatMul') with input shapes: [?,400], [201,800].
请帮忙
对于MultiRNNCell
,您需要为每个图层创建不同 个单元格。在您的代码中,您实际上是在尝试重复使用完全相同的单元格 n_layers
次。这可能会导致问题,例如当输入的维数与单元输出的维数不同时。除此之外,无论如何都不推荐,因为每一层都会使用相同的参数。
相反,像这样创建不同的单元格:
cell_list = [tf.contrib.rnn.BasicLSTMCell(num_units=n_cells, state_is_tuple=True, forget_bias=1.0)
for _ in range(n_layers)]
cells = tf.contrib.rnn.MultiRNNCell(cell_list, state_is_tuple=True)