我正在嘗試構建一個CNN + RNN模型,並且出現以下錯誤。 任何幫助將不勝感激。tf.nn.dynamic_rnn中的等級錯誤
fc2 has shape (?,4096)
cell = tf.contrib.rnn.BasicLSTMCell(self.rnn_hidden_units)
stack = tf.contrib.rnn.MultiRNNCell([cell]*self.rnn_layers)
initial_state = cell.zero_state(self.batch_size, tf.float32)
initial_state = tf.identity(initial_state, name='initial_state')
outputs, _ = tf.nn.dynamic_rnn(stack, fc2,dtype=tf.float32)
File "rcnn.py", line 182, in model outputs, _ = tf.nn.dynamic_rnn(stack, [fc2],dtype=tf.float32)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/rnn.py", line 574, in dynamic_rnn dtype=dtype)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/rnn.py", line 637, in _dynamic_rnn_loop for input_ in flat_input)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/rnn.py", line 637, in for input_ in flat_input)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 649, in with_rank_at_least raise ValueError("Shape %s must have rank at least %d" % (self, rank)) ValueError: Shape (4096, ?) must have rank at least 3
這個錯誤似乎很清楚,['tf.nn.dynamic_rnn'](https://www.tensorflow.org/api_docs/python/tf/nn/dynamic_rnn)需要一個三維張量作爲輸入(即秩3),但'fc2'只有兩個維度。如果你傳遞'time_major = True','fc2'的形狀應該是'(,,)'(或'(,, )')。 –
jdehesa
@jdehesa我正在拍攝一張圖片(用於ocr),並將它傳遞給CNN,然後將其連接到完全連接的圖層,因此形狀'fc2'是'(?,4096)',還有其他方法可以做到這一點然後 ? – lordzuko
沒關係,但那「時間」維度會是什麼?如果您只有一個圖像(即,一批示例,其中每個圖像包含一個圖像,我假設),您希望RNN迭代的維度是什麼?像素,有點像PixelRNN? – jdehesa