title: Keras: ValueError: Error when checking target: expected dense_2 to have shape (10,) but got array with shape (1,)
body: <p>I'm new to deep learning and Keras. When I used Keras to fit a LSTM model, I got the following error message: <code>ValueError: Error when checking target: expected dense_2 to have shape (10,) but got array with shape (1,)</code></p>
  
  <p>Here are my code for building LSTM:</p>
  
  <pre><code>def build(self, embedding_matrix, dim, num_class, vocab_size, maxlen):
      model = Sequential()
      model.add(Embedding(vocab_size, dim, weights = [embedding_matrix],
                          input_length = maxlen, trainable = False)) ## pre-trained model
      model.add(LSTM(dim))
      model.add(Dense(dim, activation = "relu"))
      model.add(Dense(num_class, activation = "softmax"))
      self.model = model
  </code></pre>
  
  <p>Before this post, I tried several solutions metioned in other SO posts. For example, use <code>to_categorical</code> to transform labels, use <code>Flatten</code> before the final layer. Sadly none of them worked.</p>
  
  <p>Here are my log file for running the script:</p>
  
  <pre><code>Start to fit GLOVE with reported data
  Applying GLOVE pre-trained model
  Number of unique tokens: 308758
  Pre-trained model finished
  
  Applying keras text pre-processing
  Finish to apply keras text pre-processing
  
  Start to fit the model...
  
  Build LSTM model...
  _________________________________________________________________
  Layer (type)                 Output Shape              Param #
  =================================================================
  embedding_1 (Embedding)      (None, 36303, 300)        92627400
  _________________________________________________________________
  lstm_1 (LSTM)                (None, 300)               721200
  _________________________________________________________________
  dense_1 (Dense)              (None, 300)               90300
  _________________________________________________________________
  dense_2 (Dense)              (None, 10)                3010
  =================================================================
  Total params: 93,441,910
  Trainable params: 814,510
  Non-trainable params: 92,627,400
  _________________________________________________________________
  None
  Finish model building
  </code></pre>
  
  <p>It went well untill <code>history = self.model.fit(train_padded, y_train, epochs = 10, batch_size = 128, validation_split = 0.2)</code>, then I got the above error.</p>
  
  <p>I run out of solutions. Any help will be appriciate!</p>
  
  <p>Edit:</p>
  
  <p>About the <code>y_train</code>, here is the code I use for building <code>y_train</code>:</p>
  
  <pre><code>labels = dt["category"].values
  num_class = len(np.unique(labels))
  classes = np.unique(labels)
  le = LabelEncoder()
  y = le.fit_transform(labels)
  y = to_categorical(y, num_class)
  ## split to training and test set
  x_train, y_train, x_test, y_test = train_test_split(text, y, test_size = 0.33, 
                       random_state = 42, 
                       stratify = dt["category"].astype("str"))
  </code></pre>
  
  <p>Another update: here are the shapes.</p>
  
  <pre><code>The shape of y_train:  (48334,)
  The shape of x_train:  (98132,)
  The shape of y_test:  (48334, 10)
  The shape of x_test:  (98132, 10)
  </code></pre>