title: Keras: Input tensor must be of rank 3, 4 or 5 but was 2
body: <p>I'm trying to build a SIMON model based of this example(<a href="https://github.com/algorine/simon/blob/master/Simon/Simon.py#L116" rel="nofollow noreferrer">link</a>). Below is how I'm building the 2 models: (1) sentence encoder (sentEncoder) which feeds into (2) doc encoder (docModel).</p>
  <p>When I try to fit, I'm getting the following error.</p>
  <blockquote>
  <p>Input tensor must be of rank 3, 4 or 5 but was 2.</p>
  </blockquote>
  <p>My input is of shape (3003, 30, 28), i.e. (samples,sent max length, one-hot encoded character).</p>
  <pre><code>    maxLength = 30
      max_cells = 3003
      charMap = {'a': 1,'b': 2, 'c': 3,'d': 4,'e': 5,'f': 6,
             'g': 7,'h': 8, 'i': 9, 'j': 10, 'k': 11,
             'l': 12,'m': 13, 'n': 14, 'o': 15, 'p': 16,
             'q': 17,'r': 18, 's': 19, 't': 20, 'u': 21,
             'v': 22,'w': 23, 'x': 24, 'y': 25, 'z': 26,
             ' ': 27}
      maxChars = len(charMap)+1
      x_train = np.zeros((max_cells, maxLength, maxChars), dtype='int32')
      y_train = np.zeros((max_cells, 3), dtype='int32')
      
      def buildSentModel(sentModelInput):
          layer = Conv1D(10,
                           5,
                           padding='valid',
                           activation='relu',
                           strides=1)(sentModelInput)
          layer = Bidirectional(LSTM(units=12, return_sequences=False))(layer)
          return Model(input=sentModelInput, output=layer)
          
      
      
      def buildDocModel(sentModel, docInput):
          layer = TimeDistributed(sentModel)(docInput)
          
          #layer = Flatten()(layer)
          layer = Dense(3, activation='sigmoid')(layer)
          
          return Model(input=docInput, output=layer)
          
      
      sentModelInput = Input(shape=(30,28), dtype='float32')
      sentModel = buildSentModel(sentModelInput);
      
      docModel = buildDocModel(sentModel, sentModelInput);
      
      docModel.compile(optimizer=&quot;adam&quot;, loss='categorical_crossentropy', metrics=['categorical_accuracy'])
      docModel.fit(x_train, y_train, steps_per_epoch=20,epochs=100, shuffle=True)
  </code></pre>
  <p>Here is the whole error:</p>
  <blockquote>
  <p>File &quot;C:\temp\Simon\TempSimonNames.py&quot;, line 107, in 
  model = buildDocModel(sentModel, sentModelInput);</p>
  <p>File &quot;C:\temp\Simon\TempSimonNames.py&quot;, line 94, in buildDocModel
  layer = TimeDistributed(sentModel)(docInput)</p>
  <p>File
  &quot;C:\ProgramData\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py&quot;,
  line 75, in symbolic_fn_wrapper
  return func(*args, **kwargs)</p>
  <p>File
  &quot;C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\base_layer.py&quot;,
  line 489, in <strong>call</strong>
  output = self.call(inputs, **kwargs)</p>
  <p>File
  &quot;C:\ProgramData\Anaconda3\lib\site-packages\keras\layers\wrappers.py&quot;,
  line 250, in call
  y = self.layer.call(inputs, **kwargs)</p>
  <p>File
  &quot;C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\network.py&quot;,
  line 583, in call
  output_tensors, _, _ = self.run_internal_graph(inputs, masks)</p>
  <p>File
  &quot;C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\network.py&quot;,
  line 740, in run_internal_graph
  layer.call(computed_tensor, **kwargs))</p>
  <p>File
  &quot;C:\ProgramData\Anaconda3\lib\site-packages\keras\layers\convolutional.py&quot;,
  line 163, in call
  dilation_rate=self.dilation_rate[0])</p>
  <p>File
  &quot;C:\ProgramData\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py&quot;,
  line 3671, in conv1d
  **kwargs)</p>
  <p>File
  &quot;C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\ops\nn_ops.py&quot;,
  line 917, in convolution_v2
  name=name)</p>
  <p>File
  &quot;C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\ops\nn_ops.py&quot;,
  line 969, in convolution_internal
  &quot;Input tensor must be of rank 3, 4 or 5 but was {}.&quot;.format(n + 2))</p>
  <p>ValueError: Input tensor must be of rank 3, 4 or 5 but was 2.</p>
  </blockquote>
  <p>Would appreciate any help. Thank you!</p>