为什么" tf.nn.bidirectional_dynamic_rnn"有一个"不兼容的操作"当TensorBoard检查TPU时?

时间:2018-05-05 14:47:28

标签: tensorflow google-cloud-tpu

当我检查" TPU兼容性"我遇到了问题。双向的。 TensorBoard告诉我序列长度向量的逆转操作在TPU上是不相容的。我不知道为什么?

我的简单代码:

X_batch = np.array([
                    [[0., 1., 2.], [8., 2., 1.], [9., 8., 7.]],
                    [[3., 4., 5.], [9., 7., 4.], [0., 0., 0.]],
                    [[6., 7., 8.], [3., 6., 7.], [6., 5., 4.]],
                    [[9., 0., 1.], [0., 0., 0.], [0., 0., 0.]]
                   ])
seq_length_batch = np.array([3, 2, 3, 1])

batch = 4
n_steps = 3
input_size = 3
inputs = tf.placeholder(tf.float32, [batch, n_steps, input_size])
seq_len = tf.placeholder(tf.int32, [None])

def biLSTM(inputs, seq_len, n_hidden, batch_size):
    lstm_fw = tf.nn.rnn_cell.LSTMCell(n_hidden, state_is_tuple=True)
    lstm_bw = tf.nn.rnn_cell.LSTMCell(n_hidden, state_is_tuple=True)

    _initial_state_fw = lstm_fw.zero_state(batch_size, tf.float32)
    _initial_state_bw = lstm_bw.zero_state(batch_size, tf.float32)

    output, _states = tf.nn.bidirectional_dynamic_rnn(lstm_fw, lstm_bw, inputs,
                                   initial_state_fw=_initial_state_fw,
                                   initial_state_bw=_initial_state_bw,
                                   sequence_length=seq_len)

    final_outputs = tf.concat([output[0], output[1]], 2)
    return final_outputs

biLSTM_model = biLSTM(inputs, seq_len, 4, batch)

with tf.Session() as sess:
    check_write = tf.summary.FileWriter('../test_tensorboard', sess.graph)
    init = tf.global_variables_initializer()
    init.run()
    print(sess.run(biLSTM_model, feed_dict={inputs: X_batch,
                                           seq_len: seq_length_batch}))

TensorBoard截图:

  1. Incompatible Operation

  2. ReverseSequence1

  3. ReverseSequence2

1 个答案:

答案 0 :(得分:0)

我有同样的问题,我甚至在TPU上实现它并遇到了无法展开的障碍,因为它包含一个条件while循环(循环直到输入结束)

一种可能的解决方法是将输入数据填充为常量长度,并将条件while循环更改为具有静态长度的循环。