2017-03-29 19 views
1

TensorFlowバックエンドを使用してKerasを使用していくつかのRNNを直列にスタックしようとしています。単一のSimpleRNNレイヤーでモデルを作成できますが、レイヤーを2番目に追加しようとすると、適切な入力サイズがわかりません。スタック型RNNの入力形状

from keras import models 
from keras.layers.recurrent import SimpleRNN 
from keras.layers import Activation 


model = models.Sequential() 

hidden_units = 256 
skeleton_dimensions = 3 * 16 # 3 dimensions for 16 joints 
input_temporal_length = 7 

in_shape = (input_temporal_length, skeleton_dimensions,) 

# three hidden layers of 256 each 
model.add(SimpleRNN(hidden_units, input_shape=in_shape, 
        activation='relu', use_bias=True,)) 
# what input shape is this supposed to have? 
model.add(SimpleRNN(hidden_units, input_shape=(1, skeleton_dimensions,), 
        activation='relu', use_bias=True,)) 

私の第2のSimpleRNNは入力形状としてどのようなものがありますか?

Recurrent Layersのドキュメントが暗示するようだ:return_sequences考える

Output shape

  • if return_sequences: 3D tensor with shape (batch_size, timesteps, units).
  • else, 2D tensor with shape (batch_size, units).

が自動的にFalseに設定されている私は、適切に次の次元のinput_shapeを設定しようとしましたが、私はエラーを取得:

Using TensorFlow backend. 
Traceback (most recent call last): 
    File "rnn_agony.py", line 19, in <module> 
    activation='relu', use_bias=True,)) 
    File "/usr/local/lib/python3.5/dist-packages/keras/models.py", line 455, in add 
    output_tensor = layer(self.outputs[0]) 
    File "/usr/local/lib/python3.5/dist-packages/keras/layers/recurrent.py", line 252, in __call__ 
    return super(Recurrent, self).__call__(inputs, **kwargs) 
    File "/usr/local/lib/python3.5/dist-packages/keras/engine/topology.py", line 511, in __call__ 
    self.assert_input_compatibility(inputs) 
    File "/usr/local/lib/python3.5/dist-packages/keras/engine/topology.py", line 413, in assert_input_compatibility 
    str(K.ndim(x))) 
ValueError: Input 0 is incompatible with layer simple_rnn_2: expected ndim=3, found ndim=2 

答えて

1

RNNをスタッキングする場合は、return_sequences=Trueと設定する必要があります。input_shapeの設定が解除されます。これは、RNNが入力シーケンスを期待するため、直観的に意味をなさない。

関連する問題