繁体   English   中英

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

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

我正在研究预测心脏病的神经网络。 数据来自 kaggle 并经过预处理。 我使用了各种模型,例如逻辑回归、随机森林和 SVM,它们都产生了可靠的结果。 我正在尝试将相同的数据用于神经网络,以查看 NN 是否可以胜过其他 ML 模型(数据集相当小,这可能解释了结果不佳的原因)。 下面是我的网络代码。 下面的 model 产生 50% 的准确度,显然,这太低而无法使用。 据您所知,是否有任何会破坏 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))

使用 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

它建议使用如此简单的 MLP 获得合理的准确度和 f1 分数。

在此处输入图像描述

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

  1. 对所有 epoch 进行训练,初始精度可能较低,但 model 将在几个 epoch 后很快收敛。

  2. 随机使用seed ,tensorflow 和 numpy 每次都得到可重复的结果。

  3. 如果简单模型显示出良好的准确性,则 NN 有可能会表现出色,但您必须确保 NN 没有过度拟合。

  4. 检查您的数据是否不平衡,如果是,请尝试使用class_weights

  5. 您可以尝试使用交叉验证的tuner来获得性能最佳的 model。

定标器未到位; 您需要保存缩放的结果。

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

然后,您将获得更符合您预期的结果。

              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