Keras给出的准确性低于任何分类器

时间:2018-12-06 16:14:23

标签: python keras deep-learning text-classification

我使用python进行多类文本分类,我的数据集包含25000个阿拉伯文推文,分为10个类[体育,政治,....] 当我使用

training = pd.read_csv('E:\cluster data\One_File_nonnormalizenew2norm.txt', sep="*")
training.dropna(inplace=True)
training.columns = ["text", "class1"]
training['class1'] = training.class1.astype('category').cat.codes
training.dropna(inplace=True)
# create our training data from the tweets
text = training['text']

y = (training['class1'])

from sklearn.model_selection import train_test_split
sentences_train, sentences_test, y_train, y_test = train_test_split(text, y, test_size=0.25, random_state=1000)
from sklearn.feature_extraction.text import CountVectorizer



vectorizer = CountVectorizer()
vectorizer.fit(sentences_train)

X_train = vectorizer.transform(sentences_train)
X_test  = vectorizer.transform(sentences_test)
X_train
from sklearn.linear_model import LogisticRegression

classifier = LogisticRegression()
classifier.fit(X_train, y_train)
score = classifier.score(X_test, y_test)

print("Accuracy:", score)

准确性:0.9525099601593625

当我使用keras时:

model = Sequential()
max_words=5000
model.add(Dense(512, input_shape=(input_dim,), activation='softmax'))
model.add(Dropout(0.5))
model.add(Dense(256, activation='softmax'))
model.add(Dropout(0.5))
model.add(Dense(1,activation='softmax'))
model.add(Dense(10))

model.summary()

model.compile(loss='sparse_categorical_crossentropy',
  optimizer='adam',
  metrics=['accuracy'])
model.fit(X_train, y_train,  batch_size=150,  epochs=5,  verbose=1, validation_split=0.3,shuffle=True)


predicted = model.predict(X_test)
predicted = np.argmax(predicted, axis=1)
accuracy_score(y_test, predicted)

0.28127490039840636

哪里出错了?

更新 我将代码更改为:

model = Sequential()
max_words=5000
model.add(Dense(512, input_shape=(input_dim,)))
model.add(Dropout(0.5))
model.add(Dense(256))
model.add(Dropout(0.5))
#model.add(Dense(1,activation='sigmoid'))####
model.add(Dense(10))

model.summary()
model.compile(loss='sparse_categorical_crossentropy',
  optimizer='adam',
  metrics=['accuracy'])


model.fit(X_train, y_train,batch_size=150,epochs=10,verbose=1,validation_split=0.3,shuffle=True)
predicted = model.predict(X_test)
predicted = np.argmax(predicted, axis=1)
accuracy_score(y_test, predicted)

0.7201593625498008 准确性仍然很差!!!

1 个答案:

答案 0 :(得分:1)

一些想法。

  1. 删除所有softmax激活(如@Matias所说)。
  2. 删除model.add(Dense(1,activation='softmax')),可能会破坏您的结果。
  3. 执行5个以上的纪元。
  4. 在两种方法中,您没有使用相同的推文进行验证。

您可能应该同时给出训练和测试数据集的准确性,以确保正在发生的情况。