简体   繁体   English

如何通过 dropout 层改进神经网络?

[英]How to improve neural network through dropout layers?

I am working on a neural network that predicts heart disease.我正在研究预测心脏病的神经网络。 The data comes from kaggle and has been pre-processed.数据来自 kaggle 并经过预处理。 I have used various models, such as logistic regression, random forests, and SVM, which all produce solid results.我使用了各种模型,例如逻辑回归、随机森林和 SVM,它们都产生了可靠的结果。 I'm trying to use the same data for a neural network, to see whether a NN can outperform the other ML models (the data set is rather small, which may explain the poor results).我正在尝试将相同的数据用于神经网络,以查看 NN 是否可以胜过其他 ML 模型(数据集相当小,这可能解释了结果不佳的原因)。 Below is my code for the network.下面是我的网络代码。 The model below produces 50% accuracy, which, obviously, is too low to be useful.下面的 model 产生 50% 的准确度,显然,这太低而无法使用。 From what you can tell, does anything look off that would undermine the accuracy of the model?据您所知,是否有任何会破坏 model 精度的东西?

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from tensorflow.keras.layers import Dense, Dropout
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.callbacks import EarlyStopping

df = pd.read_csv(r"C:\Users\***\Desktop\heart.csv")

X = df[['age','sex','cp','trestbps','chol','fbs','restecg','thalach']].values
y = df['target'].values

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30)

from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()

scaler.fit_transform(X_train)
scaler.transform(X_test)


nn = tf.keras.Sequential()

nn.add(Dense(30, activation='relu'))

nn.add(Dropout(0.2))

nn.add(Dense(15, activation='relu'))

nn.add(Dropout(0.2))


nn.add(Dense(1, activation='sigmoid'))


nn.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics= 
 ['accuracy'])


early_stop = EarlyStopping(monitor='val_loss',mode='min', verbose=1, 
patience=25)

nn.fit(X_train, y_train, epochs = 1000, validation_data=(X_test, y_test),
     callbacks=[early_stop])

model_loss = pd.DataFrame(nn.history.history)
model_loss.plot()

predictions = nn.predict_classes(X_test)

from sklearn.metrics import classification_report,confusion_matrix

print(classification_report(y_test,predictions))
print(confusion_matrix(y_test,predictions))

After running your model using EarlyStopping,使用 EarlyStopping 运行 model 后,

Epoch 324/1000
23/23 [==============================] - 0s 3ms/step - loss: 0.5051 - accuracy: 0.7364 - val_loss: 0.4402 - val_accuracy: 0.8182
Epoch 325/1000
23/23 [==============================] - 0s 3ms/step - loss: 0.4716 - accuracy: 0.7643 - val_loss: 0.4366 - val_accuracy: 0.7922
Epoch 00325: early stopping
WARNING:tensorflow:From <ipython-input-54-2ee8517852a8>:54: Sequential.predict_classes (from tensorflow.python.keras.engine.sequential) is deprecated and will be removed after 2021-01-01.
Instructions for updating:
Please use instead:* `np.argmax(model.predict(x), axis=-1)`,   if your model does multi-class classification   (e.g. if it uses a `softmax` last-layer activation).* `(model.predict(x) > 0.5).astype("int32")`,   if your model does binary classification   (e.g. if it uses a `sigmoid` last-layer activation).
              precision    recall  f1-score   support

           0       0.90      0.66      0.76       154
           1       0.73      0.93      0.82       154

    accuracy                           0.79       308
   macro avg       0.82      0.79      0.79       308
weighted avg       0.82      0.79      0.79       308

It suggests a reasonable accuracy and f1-score with such a simple MLP.它建议使用如此简单的 MLP 获得合理的准确度和 f1 分数。

在此处输入图像描述

I used this dataset: https://www.kaggle.com/abdulhakimrony/heartcsv/data我使用了这个数据集: https://www.kaggle.com/abdulhakimrony/heartcsv/data

  1. Train for all the epochs, the initial accuracy may be low but the model will soon converge after few epochs.对所有 epoch 进行训练,初始精度可能较低,但 model 将在几个 epoch 后很快收敛。

  2. Use seed in random, tensorflow and numpy to get reproducible result each time.随机使用seed ,tensorflow 和 numpy 每次都得到可重复的结果。

  3. If simple models show good accuracy, the chances are NN will outperform, but you have to make sure the NN is not overfitted.如果简单模型显示出良好的准确性,则 NN 有可能会表现出色,但您必须确保 NN 没有过度拟合。

  4. Check if your data is imbalanced or not, if yes, try using class_weights .检查您的数据是否不平衡,如果是,请尝试使用class_weights

  5. You can try tuner with cross-validation to get the best performing model.您可以尝试使用交叉验证的tuner来获得性能最佳的 model。

The scaler is not in-place;定标器未到位; you need to save the scaled results.您需要保存缩放的结果。

X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

You'll then get results more in line with what you were expecting.然后,您将获得更符合您预期的结果。

              precision    recall  f1-score   support

           0       0.93      0.98      0.95       144
           1       0.98      0.93      0.96       164

    accuracy                           0.95       308
   macro avg       0.95      0.96      0.95       308
weighted avg       0.96      0.95      0.95       308

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM