无法在张量流中创建MultiRNN单元

时间:2018-07-31 09:36:43

标签: tensorflow rnn

我试图创建一个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单元时,出现错误

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].

请帮助

1 个答案:

答案 0 :(得分:1)

对于MultiRNNCell,您需要为每一层创建 distinct 单元。在代码中,您实际上是在尝试重复使用相同的单元格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)