2017-06-21 44 views
2

我正在嘗試構建一個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

+1

這個錯誤似乎很清楚,['tf.nn.dynamic_rnn'](https://www.tensorflow.org/api_docs/python/tf/nn/dynamic_rnn)需要一個三維張量作爲輸入(即秩3),但'fc2'只有兩個維度。如果你傳遞'time_major = True','fc2'的形狀應該是'(,)'(或'(,, )')。 – jdehesa

+0

@jdehesa我正在拍攝一張圖片(用於ocr),並將它傳遞給CNN,然後將其連接到完全連接的圖層,因此形狀'fc2'是'(?,4096)',還有其他方法可以做到這一點然後 ? – lordzuko

+0

沒關係,但那「時間」維度會是什麼?如果您只有一個圖像(即,一批示例,其中每個圖像包含一個圖像,我假設),您希望RNN迭代的維度是什麼?像素,有點像PixelRNN? – jdehesa

回答

1

複製@jdehesa從他的評論答案爲更好的可視性:

錯誤似乎相當明確,tf.nn.dynamic_rnn預計3維張量輸入(即等級3) ,但fc2只有兩個維度。的fc2的形狀應該是這樣的(<batch_size>, <max_time>, <num_features>)(或(<max_time>, <batch_size>, <num_features>)如果你通過time_major=True

0

tf.nn.dynamic_rnn的默認輸入有3(BATCHSIZE,sequence_length,NUM_FEATURES)的尺寸。由於您的num_features是1,您可以擴展您的fc_seq

fc2 = tf.expand_dims(fc2, axis = 2) 

然後使用上面的代碼。