tensorflow2知识总结---6、过拟合实例
tensorflow2知识总结---6、过拟合实例
一、总结
一句话总结:
A、训练集上的loss一直降低 ,测试集上的loss却有较大波折,证明过拟合
B、训练的时候验证测试数据:history = model.fit(train_image,train_label,epochs=10,validation_data=(test_image,test_label))
二、过拟合实例
博客对应课程的视频位置:
In [15]:
model.compile(optimizer=tf.keras.optimizers.Adam(lr=0.01),
loss='sparse_categorical_crossentropy',
metrics=['acc'])
history = model.fit(train_image,train_label,epochs=10,validation_data=(test_image,test_label))
In [16]:
history.history.keys()
Out[16]:
In [21]:
plt.plot(history.epoch, history.history.get('loss'),"r-",linewidth=2,label="训练集:loss")
plt.plot(history.epoch, history.history.get('val_loss'),"g-",linewidth=2,label="测试集:val_loss")
plt.legend(loc ="upper right")
Out[21]:
图形分析:
训练集上的loss一直降低 ,测试集上的loss却有较大波折,证明过拟合
In [24]:
plt.rcParams["font.sans-serif"]=["SimHei"]
plt.rcParams["font.family"]="sans-serif"
plt.plot(history.epoch, history.history.get('acc'),"r-",linewidth=2,label="训练集:acc")
plt.plot(history.epoch, history.history.get('val_acc'),"g-",linewidth=2,label="测试集:val_acc")
plt.legend(loc ="upper right")
Out[24]: