title: NN in Keras - expected dense_2 to have 3 dimensions, but got array with shape (10980, 3)
body: <p>I want to train a <strong>Neutral Network</strong> for <strong>Multi-Classification Sentiment Analysis</strong> using <strong>word embedding</strong> for tweets.</p>
  <p>Here is my code:</p>
  <pre><code>import pandas as pd
  import numpy as np
  import re
  from nltk.corpus import stopwords
  from sklearn.model_selection import train_test_split
  from sklearn.preprocessing import LabelEncoder
  from tensorflow.python.keras.preprocessing.text import Tokenizer
  from tensorflow.python.keras.preprocessing.sequence import pad_sequences
  from keras.models import Sequential
  from keras.layers import Dense, Embedding, LSTM, GRU
  from keras.layers.embeddings import Embedding
  
  
  from keras.wrappers.scikit_learn import KerasClassifier
  from keras.utils import np_utils
  from sklearn.model_selection import cross_val_score
  from sklearn.model_selection import KFold
  from sklearn.pipeline import Pipeline
  </code></pre>
  <h1>Import the data</h1>
  <pre><code>df = pd.DataFrame()
  df = pd.read_csv('Tweets.csv', encoding='utf-8')
  </code></pre>
  <h1>clean the tweets</h1>
  <pre><code>def remove_mentions(input_text):
      return re.sub(r'@\w+', '', input_text)
  
  def remove_stopwords(input_text):
      stopwords_list = stopwords.words('english')
      whitelist = [&quot;n't&quot;, &quot;not&quot;, &quot;no&quot;]
      words = input_text.split() 
      clean_words = [word for word in words if (word not in stopwords_list or word in whitelist) and len(word) &gt; 1] 
      return &quot; &quot;.join(clean_words) 
  
  df.text = df.text.apply(remove_stopwords).apply(remove_mentions)
  df.text = [tweet for tweet in df.text if type(tweet) is str]
  
  X = df['text']
  y = df['airline_sentiment']
  </code></pre>
  <h1>Split my data into train and test</h1>
  <pre><code>X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.25, random_state=37)
  </code></pre>
  <h1>One-Hot Encode the field &quot;Sentiment&quot;</h1>
  <p>Originally the labels are of type string: 'neutral', 'positive', 'negative'. So I first transform them to integer and then apply one-hot encoding:</p>
  <pre><code>le = LabelEncoder()
  y_train_num = le.fit_transform(y_train.values)
  y_test_num = le.fit_transform(y_test.values)
  
  nb_classes = 3
  y_train = np_utils.to_categorical(y_train_num, nb_classes)
  y_test = np_utils.to_categorical(y_test_num, nb_classes)
  </code></pre>
  <h1>Prepare for Word Embedding</h1>
  <pre><code>tokenizer_obj = Tokenizer()
  tokenizer_obj.fit_on_texts(X)
  max_length = max([len(tweet.split()) for tweet in X])
  print(&quot;max_length=%s&quot; % (max_length))
  
  vocab_size = len(tokenizer_obj.word_index) + 1 
  print(&quot;vocab_size=%s&quot; % (vocab_size))
  
  X_train_tokenized = tokenizer_obj.texts_to_sequences(X_train)
  X_test_tokenized = tokenizer_obj.texts_to_sequences(X_test)
  
  X_train_pad = pad_sequences(X_train_tokenized, maxlen=max_length, padding='post')
  X_test_pad = pad_sequences(X_test_tokenized, maxlen=max_length, padding='post')
  </code></pre>
  <h1>Define and Apply my NN Model</h1>
  <pre><code>EMBEDDING_DIM = 100
      
  model = Sequential()
  model.add(Embedding(vocab_size, EMBEDDING_DIM, input_length=max_length))
  model.add(Dense(8, input_dim=4, activation='relu'))
  model.add(Dense(3, activation='softmax'))
  
  model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
      
  print(model.summary())
  
  model.fit(X_train_pad, y_train, batch_size=128, epochs=25, validation_data=(X_test_pad, y_test), verbose=2)
  </code></pre>
  <p><strong>The reason I chose my last layer to have 3 output units is because it's a multi-classification task and I have 3 classes.</strong></p>
  <p>Here is the model summary:</p>
  <pre><code>Layer (type)                 Output Shape              Param #   
  =================================================================
  embedding_1 (Embedding)      (None, 23, 100)           1488200   
  _________________________________________________________________
  dense_1 (Dense)              (None, 23, 8)             808       
  _________________________________________________________________
  dense_2 (Dense)              (None, 23, 3)             27        
  =================================================================
  Total params: 1,489,035
  Trainable params: 1,489,035
  Non-trainable params: 0
  _________________________________________________________________
  </code></pre>
  <p>When the code gets to <code>model.fit()</code>, I get the following error:</p>
  <pre><code>ValueError: Error when checking target: expected dense_2 to have 3 dimensions, but got array with shape (10980, 3)
  </code></pre>
  <p>What am I doing wrong?</p>