vendredi 26 mars 2021

MLP keras, my training result is 97% while testing is 86% can I increase testing accuracy and how?

I'm doing a text classification using deep learning model , but my testing result is just 86% I need to increase it, so I appreciate any one who helps. my code is below

dataframe= pandas.read_excel('my_dataset.xlsx')
dataframe.drop_duplicates(inplace = True)
data= dataframe
texts= data['text']
tags= data['target']
train_posts, test_posts,train_tags,test_tags = train_test_split(texts, tags, test_size=0.2)
tokenizer = Tokenizer(num_words=300,lower=False)
tokenizer.fit_on_texts(texts)

x_train = tokenizer.texts_to_matrix(train_posts,mode='tfidf')
x_test = tokenizer.texts_to_matrix(test_posts,mode='tfidf'
encoder = LabelEncoder()
encoder.fit(tags)
tagst=encoder.fit_transform(tags)

num_classes = int((len(set(tagst))))
y_train = encoder.fit_transform(train_tags)
y_test = encoder.fit_transform(test_tags)
num_labels = int(len(y_train.shape))
vocab_size = len(tokenizer.word_index) + 1
max_words=vocab_size
model = Sequential()
model.add(Dense(1032, input_shape=(300,),kernel_regularizer=regularizers.l2(0.01)))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1032))
model.add(Activation('relu'))
model.add(Dense(7))
model.add(Activation('softmax'))
model.compile(loss='sparse_categorical_crossentropy',
              optimizer='adam',metrics=['accuracy'])
batch_size = 64
epochs = 25
history = model.fit(x_train, y_train,
                    batch_size=batch_size,
                    epochs=epochs,
                    verbose=1,
                    validation_data=(x_test,y_test))

Aucun commentaire:

Enregistrer un commentaire