title: Exception encountered when calling layer "sequential"
body: <p>I am attempting to build a text generator using tensorflow and keras. I am using code from the train.py file that can be found on the following website: <a href="https://www.thepythoncode.com/code/text-generation-keras-python" rel="nofollow noreferrer">https://www.thepythoncode.com/code/text-generation-keras-python</a>.  The following is a snippet from the code I'm running. Here, &quot;inputz&quot; is the variable name for the string of text that I am using to train my model:</p>
  <pre><code>import tensorflow as tf
  import numpy as np
  import os
  import pickle
  from tensorflow.keras.models import Sequential
  from tensorflow.keras.layers import Dense, LSTM, Dropout
  from string import punctuation
  
  sequence_length = 100
  BATCH_SIZE = 128
  EPOCHS = 30
  
  # dataset file path
  FILE_PATH = &quot;data/EXAMPLE.txt&quot;
  BASENAME = os.path.basename(FILE_PATH)
  
  # print some stats
  n_chars = len(inputz)
  vocab = ''.join(sorted(set(inputz)))
  print(&quot;unique_chars:&quot;, vocab)
  n_unique_chars = len(vocab)
  print(&quot;Number of characters:&quot;, n_chars)
  print(&quot;Number of unique characters:&quot;, n_unique_chars)
  
  # dictionary that converts characters to integers
  char2int = {c: i for i, c in enumerate(vocab)}
  # dictionary that converts integers to characters
  int2char = {i: c for i, c in enumerate(vocab)}
  
  # save these dictionaries for later generation
  pickle.dump(char2int, open(f&quot;{BASENAME}-char2int.pickle&quot;, &quot;wb&quot;))
  pickle.dump(int2char, open(f&quot;{BASENAME}-int2char.pickle&quot;, &quot;wb&quot;))
  
  # convert all text into integers
  encoded_text = np.array([char2int[c] for c in inputz])
  
  # construct tf.data.Dataset object
  char_dataset = tf.data.Dataset.from_tensor_slices(encoded_text)
  
  # print first 8 characters
  for char in char_dataset.take(8):
      print(char.numpy(), int2char[char.numpy()])
  
  # build sequences by batching
  sequences = char_dataset.batch(2*sequence_length + 1, drop_remainder=True)
  
  # print sequences
  for sequence in sequences.take(2):
      print(''.join([int2char[i] for i in sequence.numpy()]))
  
  def split_sample(sample):
      # example :
      # sequence_length is 10
      # sample is &quot;python is a great pro&quot; (21 length)
      # ds will equal to ('python is ', 'a') encoded as integers
      ds = tf.data.Dataset.from_tensors((sample[:sequence_length], sample[sequence_length]))
      for i in range(1, (len(sample)-1) // 2):
          # first (input_, target) will be ('ython is a', ' ')
          # second (input_, target) will be ('thon is a ', 'g')
          # third (input_, target) will be ('hon is a g', 'r')
          # and so on
          input_ = sample[i: i+sequence_length]
          target = sample[i+sequence_length]
          # extend the dataset with these samples by concatenate() method
          other_ds = tf.data.Dataset.from_tensors((input_, target))
          ds = ds.concatenate(other_ds)
      return ds
  
  # prepare inputs and targets
  dataset = sequences.flat_map(split_sample)
  
  
  def one_hot_samples(input_, target):
      # onehot encode the inputs and the targets
      # Example:
      # if character 'd' is encoded as 3 and n_unique_chars = 5
      # result should be the vector: [0, 0, 0, 1, 0], since 'd' is the 4th character
      return tf.one_hot(input_, n_unique_chars), tf.one_hot(target, n_unique_chars)
  
  # print first 2 samples
  for element in dataset.take(2):
      print(&quot;Input:&quot;, ''.join([int2char[np.argmax(char_vector)] for char_vector in element[0].numpy()]))
      print(&quot;Target:&quot;, int2char[np.argmax(element[1].numpy())])
      print(&quot;Input shape:&quot;, element[0].shape)
      print(&quot;Target shape:&quot;, element[1].shape)
      print(&quot;=&quot;*50, &quot;\n&quot;)
  
  # repeat, shuffle and batch the dataset
  ds = dataset.repeat().shuffle(1024).batch(BATCH_SIZE, drop_remainder=True)
  
  model = Sequential([
      LSTM(256, input_shape=(sequence_length, n_unique_chars), return_sequences=True),
      Dropout(0.3),
      LSTM(256),
      Dense(n_unique_chars, activation=&quot;softmax&quot;),
  ])
  
  # define the model path
  model_weights_path = f&quot;results/{BASENAME}-{sequence_length}.h5&quot;
  model.summary()
  model.compile(loss=&quot;categorical_crossentropy&quot;, optimizer=&quot;adam&quot;, metrics=[&quot;accuracy&quot;])
  
  # make results folder if does not exist yet
  if not os.path.isdir(&quot;results&quot;):
      os.mkdir(&quot;results&quot;)
  # train the model
  model.fit(ds, steps_per_epoch=(len(encoded_text) - sequence_length) // BATCH_SIZE, epochs=EPOCHS)
  # save the model
  model.save(model_weights_path)
  
  </code></pre>
  <p>When I run this code, I encounter the following error:</p>
  <pre><code>WARNING:tensorflow:Model was constructed with shape (None, 100, 42) for input KerasTensor(type_spec=TensorSpec(shape=(None, 100, 42), dtype=tf.float32, name='lstm_input'), name='lstm_input', description=&quot;created by layer 'lstm_input'&quot;), but it was called on an input with incompatible shape (128, 100).
  Traceback (most recent call last):
    File &quot;c:\Users\leehs\Desktop\Python\Garfield\new_text_gen.py&quot;, line 127, in &lt;module&gt;
      model.fit(ds, steps_per_epoch=(len(encoded_text) - sequence_length) // BATCH_SIZE, epochs=EPOCHS)
    File &quot;C:\Users\leehs\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\utils\traceback_utils.py&quot;, line 67, in error_handler    
      raise e.with_traceback(filtered_tb) from None
    File &quot;C:\Users\leehs\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tensorflow\python\framework\func_graph.py&quot;, line 1147, in autograph_handler
      raise e.ag_error_metadata.to_exception(e)
  ValueError: in user code:
  
      File &quot;C:\Users\leehs\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\engine\training.py&quot;, line 946, in train_function  *   
          return step_function(self, iterator)
      File &quot;C:\Users\leehs\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\engine\training.py&quot;, line 935, in step_function  **   
          outputs = model.distribute_strategy.run(run_step, args=(data,))
      File &quot;C:\Users\leehs\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\engine\training.py&quot;, line 928, in run_step  **        
          outputs = model.train_step(data)
      File &quot;C:\Users\leehs\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\engine\training.py&quot;, line 841, in train_step
          y_pred = self(x, training=True)
      File &quot;C:\Users\leehs\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\utils\traceback_utils.py&quot;, line 67, in error_handler  
          raise e.with_traceback(filtered_tb) from None
      File &quot;C:\Users\leehs\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\engine\input_spec.py&quot;, line 214, in assert_input_compatibility
          raise ValueError(f'Input {input_index} of layer &quot;{layer_name}&quot; '
  
      ValueError: Exception encountered when calling layer &quot;sequential&quot; (type Sequential).
  
      Input 0 of layer &quot;lstm&quot; is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (128, 100)
  
      Call arguments received:
        • inputs=tf.Tensor(shape=(128, 100), dtype=int32)
        • training=True
        • mask=None
  </code></pre>
  <p>What can I change to stop this error from reoccurring?</p>