我怎么知道我的神经网络模型是否过拟合(Keras) [英] How do I know if my Neural Network model is overfitting or not (Keras)

查看:512
本文介绍了我怎么知道我的神经网络模型是否过拟合(Keras)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Keras预测我将得到1还是0的输出.数据看起来像这样:

I'm using Keras to predict if I'll get an output of 1 or 0. The data looks like this:

    funded_amnt  emp_length  avg_cur_bal  num_actv_rev_tl    loan_status
    10000       5.60088      19266                 2                  1
    13750       5.60088      2802                  6                  0
    26100       10.0000      19241                17                  1

目标是loan_status,其余功能.在开始构建神经网络模型之前,我已经对数据进行了标准化.

The target is loan_status and the features are the remaining. I've normalized the data before starting to build a Neural Network model.

这是我的训练和测试数据的形状:

Here's the shape of my training and testing data:

    print(X_train.shape,Y_train.shape) 
    # Output: (693, 4) (693,)

    print(X_test.shape,Y_test.shape) 
    # Output: (149, 4) (149,)

我建立神经网络所遵循的过程是:

The process I followed to build the Neural Network is:

     # define the keras model
     model = Sequential()
     model.add(Dense(4, input_dim=4,activation='relu'))
     model.add(Dense(4 ,activation='relu'))
     model.add(Dense(1,activation='sigmoid'))

     # compile the keras model
     model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

     # fit the keras model on the dataset
     hist = model.fit(X_train, Y_train, epochs=10, batch_size=2)

运行hist后的输出:

         Epoch 1/10
         693/693 [==============================] - 2s 2ms/step - loss: 0.6379 - acc: 0.7013
         Epoch 2/10
         693/693 [==============================] - 0s 611us/step - loss: 0.5207 - acc: 0.7951
         Epoch 3/10
         693/693 [==============================] - 0s 605us/step - loss: 0.5126 - acc: 0.7951
         Epoch 4/10
         693/693 [==============================] - 0s 621us/step - loss: 0.5109 - acc: 0.7951
         Epoch 5/10
         693/693 [==============================] - 0s 611us/step - loss: 0.5105 - acc: 0.7951
         Epoch 6/10
         693/693 [==============================] - 0s 636us/step - loss: 0.5091 - acc: 0.7951
         Epoch 7/10
         693/693 [==============================] - 0s 644us/step - loss: 0.5090 - acc: 0.7951
         Epoch 8/10
         693/693 [==============================] - 0s 659us/step - loss: 0.5086 - acc: 0.7951
         Epoch 9/10
         693/693 [==============================] - 0s 668us/step - loss: 0.5083 - acc: 0.7951
         Epoch 10/10
         693/693 [==============================] - 0s 656us/step - loss: 0.5076 - acc: 0.7951

几乎都是相同的,并且在第二个Epoch之后没有改变.我尝试更改时间数和批次大小,但保持相同的结果. 这正常吗?还是过度拟合的迹象,我需要更改一些参数

it's all almost the same and doesn't change after the second Epoch. I've tried changing number of Epochs and Batch size but keep getting the same results. Is this normal? or is it a sign of overfitting and I need to change some parameters

推荐答案

您的测试数据旨在监视模型的

Your test data meant to be for monitoring the model's overfitting on train data:

hist = model.fit(X_train, Y_train, validation_data=(X_test, Y_test), epochs=10, batch_size=2)

在训练过程中,您将达到训练损失继续减少但测试损失停止减少的程度.那就是您的数据开始过度拟合的地方.

During the training you will reach a point, where the train loss continues to decrease, but your test loss stops to decrease. That the point where your data starts to overfit.

在统计数据中,过度拟合是过于紧密或精确地对应于特定数据集的分析结果,因此可能无法拟合其他数据或可靠地预测未来的观察结果".

In statistics, overfitting is "the production of an analysis that corresponds too closely or exactly to a particular set of data, and may therefore fail to fit additional data or predict future observations reliably".

作为一个极端的例子,如果参数的数量等于或大于观察值的数量,则模型可以简单地通过完整地记住训练数据来完美地预测训练数据.但是,这样的模型通常在做出预测时会严重失败.

As an extreme example, if the number of parameters is the same as or greater than the number of observations, then a model can perfectly predict the training data simply by memorizing the data in its entirety. Such a model, though, will typically fail severely when making predictions.

通常使用一组训练数据"对学习算法进行训练:已知输出的示例情况.目标是该算法在输入在训练期间未遇到的"验证数据"时,在预测输出方面也将表现良好.在学习时间过长或训练实例很少的情况下,过度拟合尤其可能导致学习者适应训练数据的非常具体的随机特征,而这些随机特征与目标功能没有因果关系.在过度拟合的过程中,训练示例上的性能仍然会提高,而看不见数据上的性能会变差.

Usually a learning algorithm is trained using some set of "training data": exemplary situations for which the desired output is known. The goal is that the algorithm will also perform well on predicting the output when fed "validation data" that was not encountered during its training. Overfitting is especially likely in cases where learning was performed too long or where training examples are rare, causing the learner to adjust to very specific random features of the training data, that have no causal relation to the target function. In this process of overfitting, the performance on the training examples still increases while the performance on unseen data becomes worse.

绿线表示过度拟合的模型,黑线表示正则化的模型.绿线虽然最适合训练数据,但它过于依赖训练数据,与黑线相比,新出现的看不见的数据可能有更高的错误率.

The green line represents an overfitted model and the black line represents a regularized model. While the green line best follows the training data, it is too dependent on that data and it is likely to have a higher error rate on new unseen data, compared to the black line.

这篇关于我怎么知道我的神经网络模型是否过拟合(Keras)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆