title: Keras: Multiclass CNN Text Classifier predicts the same class for all input data
body: <p>I am trying to predict one out of 8 classes for some short text data with word embeddings and CNN. The occuring problem is that the CNN Classifier predicts everything in one class (the biggest one of the training data, distribution ~40%). The text data should be preprocesed quite well, because the classification works fine with other classifiers like SVM or NB. </p>
  
  <p>'''
      #first, build index mapping words in the embeddings set to their embedding vector
      import os
      embeddings_index = {}
      f = open(os.path.join('', 'embedding_word2vec.txt'), encoding='utf-8')
      for line in f:
          values = line.split()
          words = values[0]
          coefs = np.asarray(values[1:])
          embeddings_index[word] = coefs
      f.close()</p>
  
  <pre><code>#vectorize text samples in 2D Integer Tensor 
  from tensorflow.python.keras.preprocessing.text import Tokenizer
  tokenizer_obj = Tokenizer()
  tokenizer_obj.fit_on_texts(stemmed_text)
  sequences = tokenizer_obj.texts_to_sequences(stemmed_text)
  
  #pad sequences
  from tensorflow.python.keras.preprocessing.sequence import pad_sequences
  from keras.utils import to_categorical
  max_length = max([len(s.split(',')) for s in total_texts])
  print('MaxLength :', max_length)
  word_index = tokenizer_obj.word_index
  print('Found %s unique Tokens.' % len(word_index))
  
  text_pad = pad_sequences(sequences)
  labels = to_categorical(np.asarray(labels))
  print('Shape of label tensor:', labels.shape)
  print('Shape of Text Tensor: ', text_pad.shape)
  
  embedding_dim = 100
  num_words = len(word_index) + 1
  
  #prepare embedding matrix
  embedding_matrix = np.zeros((vocab_size, embedding_dim))
  for index, word in enumerate(vocab):
      embedding_vector = w2v.wv[word]
      if embedding_vector is not None:
          embedding_matrix[index] = embedding_vector
  
  #train test split 
  validation_split = 0.2
  indices = np.arange(text_pad.shape[0])
  np.random.shuffle(indices)
  text_pad = text_pad[indices]
  labels = labels[indices]
  num_validation_samples = int(validation_split * text_pad.shape[0])
  
  x_train = text_pad[:-num_validation_samples]
  y_train = labels[:-num_validation_samples]
  x_test = text_pad[-num_validation_samples:]
  y_test = labels[-num_validation_samples:]
  
  from keras.models import Sequential
  from keras.layers.core import Dense, Flatten, Dropout
  from keras.layers.convolutional import Conv1D, MaxPooling1D
  from keras.layers.embeddings import Embedding
  
  model = Sequential()
  model.add(Embedding(vocab_size, embedding_dim, input_length=max_length, 
                      #embeddings_initializer = Constant(embedding_matrix),
                      weights=[embedding_matrix], 
                      trainable=False))
  
  model.add(Dropout(0.2))
  model.add(Conv1D(filters=64, kernel_size=5, activation='relu'))
  model.add(MaxPooling1D(pool_size=4))
  #model.add(Conv1D(filters=64, kernel_size=5, activation='relu'))
  #model.add(MaxPooling1D(pool_size=4))
  model.add(Flatten())
  model.add(Dense(8, activation='sigmoid'))
  model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
  history = model.fit(x_train, y_train,
            epochs=25,
            validation_data=(x_test, y_test))
  print(model.summary())
  </code></pre>
  
  <p>'''</p>
  
  <p>I am grateful for every hint.
  Thank you.</p>