【发布时间】:2023-04-11 02:11:01
【问题描述】:
我有一个用于在 Keras 中制作 NN 模型的数据集,我从该数据集中取了 2000 行作为验证数据,这 2000 行应该添加到 .predict 函数中。
我为 Keras NN 编写了一个代码,现在它运行良好,但我注意到一些对我来说非常奇怪的东西。它给了我超过 83% 的非常好的准确率,损失在 0.12 左右,但是当我想用看不见的数据(那 2000 行)进行预测时,它只能预测平均 65% 的正确率。 当我添加 Dropout 层时,它只会降低准确性。
然后我添加了EarlyStopping,它给了我大约 86% 的准确率,损失在 0.10 左右,但是当我用看不见的数据进行预测时,我得到了 67% 的最终预测准确率。
这是否意味着模型在 87% 的情况下做出了正确的预测?我的逻辑是,如果我在 .predict 函数中添加 100 个样本,那么该程序应该对 87/100 个样本或该范围内的某个位置(假设超过 80 个)做出良好的预测?我尝试在我的.predict 函数中添加 100、500、1000、1500 和 2000 个样本,它总是在 65-68% 的样本中做出正确的预测。
为什么,我做错了什么? 我尝试过使用层数、节点数、不同的激活函数和不同的优化器,但它只会将结果改变 1-2%。 我的数据集如下所示:
DataFrame shape (59249, 33)
x_train shape (47399, 32)
y_train shape (47399,)
x_test shape (11850, 32)
y_test shape (11850,)
testing_features shape (1000, 32)
这是我的神经网络模型:
model = Sequential()
model.add(Dense(64, input_dim = x_train.shape[1], activation = 'relu')) # input layer requires input_dim param
model.add(Dropout(0.2))
model.add(Dense(32, activation = 'relu'))
model.add(Dropout(0.2))
model.add(Dense(16, activation = 'relu'))
model.add(Dense(1, activation='sigmoid')) # sigmoid instead of relu for final probability between 0 and 1
# compile the model, adam gradient descent (optimized)
model.compile(loss="binary_crossentropy", optimizer= "adam", metrics=['accuracy'])
# call the function to fit to the data training the network)
es = EarlyStopping(monitor='val_loss', min_delta=0.0, patience=1, verbose=0, mode='auto')
model.fit(x_train, y_train, epochs = 15, shuffle = True, batch_size=32, validation_data=(x_test, y_test), verbose=2, callbacks=[es])
scores = model.evaluate(x_test, y_test)
print(model.metrics_names[0], round(scores[0]*100,2), model.metrics_names[1], round(scores[1]*100,2))
这些是结果:
Train on 47399 samples, validate on 11850 samples
Epoch 1/15
- 25s - loss: 0.3648 - acc: 0.8451 - val_loss: 0.2825 - val_acc: 0.8756
Epoch 2/15
- 9s - loss: 0.2949 - acc: 0.8689 - val_loss: 0.2566 - val_acc: 0.8797
Epoch 3/15
- 9s - loss: 0.2741 - acc: 0.8773 - val_loss: 0.2468 - val_acc: 0.8849
Epoch 4/15
- 9s - loss: 0.2626 - acc: 0.8816 - val_loss: 0.2416 - val_acc: 0.8845
Epoch 5/15
- 10s - loss: 0.2566 - acc: 0.8827 - val_loss: 0.2401 - val_acc: 0.8867
Epoch 6/15
- 8s - loss: 0.2503 - acc: 0.8858 - val_loss: 0.2364 - val_acc: 0.8893
Epoch 7/15
- 9s - loss: 0.2480 - acc: 0.8873 - val_loss: 0.2321 - val_acc: 0.8895
Epoch 8/15
- 9s - loss: 0.2450 - acc: 0.8886 - val_loss: 0.2357 - val_acc: 0.8888
11850/11850 [==============================] - 2s 173us/step
loss 23.57 acc 88.88
这是为了预测:
#testing_features are 2000 rows that i extracted from dataset (these samples are not used in training, this is separate dataset thats imported)
prediction = model.predict(testing_features , batch_size=32)
res = []
for p in prediction:
res.append(p[0].round(0))
# Accuracy with sklearn - also much lower
acc_score = accuracy_score(testing_results, res)
print("Sklearn acc", acc_score)
result_df = pd.DataFrame({"label":testing_results,
"prediction":res})
result_df["prediction"] = result_df["prediction"].astype(int)
s = 0
for x,y in zip(result_df["label"], result_df["prediction"]):
if x == y:
s+=1
print(s,"/",len(result_df))
acc = s*100/len(result_df)
print('TOTAL ACC:', round(acc,2))
问题是......现在我得到了 sklearn 52% 和 my_acc 52% 的准确度。
为什么我在验证时得到如此低的准确度,而它说它要大得多?
【问题讨论】:
-
您的模型可能过拟合。了解避免过度拟合的方法
-
请在这个新的测试集中包含预测的代码,并且损失不是百分比。
-
@MatiasValdenegro 完成了,我也改变了它,所以损失不是 %。请给我建议我做错了什么。我的训练集和测试集是 0.75/0.25
-
您没有正确计算准确率,只需使用model.evaluate,返回元组中的第二个元素是准确率
-
对不起,我没听懂你的意思。我已经做到了,我得到了大约 87% 的准确率,然后我用 2000 个样本进行了预测,我想看看我的模型的表现如何,它说它在 67% 的时间内做出了正确的预测
标签: python machine-learning keras neural-network