python - 减少 CNN 模型中的验证损失
问题描述
import tensorflow as tf
import tensorflow.keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D
import pickle
import numpy as np
from keras.models import model_from_json
from keras.models import load_model
import matplotlib.pyplot as plt
# Opening the files about data
X = pickle.load(open("X.pickle", "rb"))
y = pickle.load(open("y.pickle", "rb"))
# normalizing data (a pixel goes from 0 to 255)
X = X/255.0
# Building the model
model = Sequential()
# 3 convolutional layers
model.add(Conv2D(32, (3, 3), input_shape = X.shape[1:]))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(64, (3, 3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(64, (3, 3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.9))
# 5 hidden layers
model.add(Flatten())
model.add(Dense(128))
model.add(Activation("relu"))
model.add(Dense(128))
model.add(Activation("relu"))
model.add(Dense(128))
model.add(Activation("relu"))
model.add(Dense(128))
model.add(Activation("relu"))
model.add(Dense(128))
model.add(Activation("relu"))
# The output layer with 7 neurons, for 7 classes
model.add(Dense(13))
model.add(Activation("softmax"))
# Compiling the model using some basic parameters
model.compile(loss="sparse_categorical_crossentropy",
optimizer="adam",
metrics=["accuracy"])
# Training the model, with 40 iterations
# validation_split corresponds to the percentage of images used for the validation phase compared to all the images
print("X = " + str(len(X)))
print("y = " + str(len(y)))
history = model.fit(X, y, batch_size=32, epochs=1000, validation_split=0.1)
# Saving the model
model_json = model.to_json()
with open("model.json", "w") as json_file :
json_file.write(model_json)
model.save_weights("model.h5")
print("Saved model to disk")
model.save('CNN.model')
# Printing a graph showing the accuracy changes during the training phase
print(history.history.keys())
plt.show()
plt.plot(history.history['accuracy'])
plt.plot(history.history['loss'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.show()
问题是,我的训练损失较低,但验证准确度很高。而且验证的准确性也极低。我该如何解决这个问题?我试图将下降值增加到 0.9,但损失仍然高得多。我也尝试使用线性函数进行激活,但没有用。
请帮忙。
解决方案
正如已经提到的,在没有看到数据的情况下很难给出好的建议。
我会尝试以下内容: - 在 maxpooling 层之后删除 Dropout - 删除一些密集层 - 在密集层之间添加 dropout
如果仍然过拟合,请在密集层之间添加 dropout
编辑:在我看到损失和准确度图后,我会建议以下内容:
- 最高优先级是,获取更多数据。
- 然后使用数据增强甚至增加您的数据集
- 如果额外的数据没有帮助,进一步降低你的神经网络的复杂性(但我认为训练会随着更多的数据而减慢,并且验证损失也会在更长的时期内减少)
推荐阅读
- bash - awk 捕获两个模式之间的重复线条组
- amazon-web-services - 尝试使用 RDS 运行 Ec2 时,连接超时。我的安全组规则看起来正确吗?
- javascript - 试图从表中删除 ROW 但删除了我的所有行
- docker - 如何使用带有hadoop流的docker工具箱来解决多节点集群的map reduce
- .net - 在流文档中设置静态样式,以便仅 Style=(static somestyle} 与文档一起保存
- python - Arcpy 脚本循环 - 如何循环浏览文件夹中的表并在每个表上执行 arcpy 连接功能?
- xml - 如何使用 Wordpress 修改 YoastSEO 站点地图?(XML 文件)
- macos-mojave - 更改时不显示 NSTabViewItem 图像
- django - 如何通过 UserChangeForm 保存我的条目?
- vb.net - 如何在 VB.NET 中格式化 curl API 调用如何处理主 url 之后的部分