tensorflow2知识总结---7、dropout抑制过拟合实例
tensorflow2知识总结---7、dropout抑制过拟合实例
一、总结
一句话总结:
操作非常简单,直接增加dropout层即可:model.add(tf.keras.layers.Dropout(0.5))
# 增加dropout层来抑制过拟合 model = tf.keras.Sequential() model.add(tf.keras.layers.Flatten(input_shape=(28,28))) model.add(tf.keras.layers.Dense(128,activation='relu')) model.add(tf.keras.layers.Dropout(0.5)) model.add(tf.keras.layers.Dense(128,activation='relu')) model.add(tf.keras.layers.Dropout(0.5)) model.add(tf.keras.layers.Dense(128,activation='relu')) model.add(tf.keras.layers.Dropout(0.5)) model.add(tf.keras.layers.Dense(10,activation='softmax'))
二、dropout抑制过拟合实例
博客对应课程的视频位置:
In [16]:
# 增加dropout层来抑制过拟合
model = tf.keras.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=(28,28)))
model.add(tf.keras.layers.Dense(128,activation='relu'))
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(128,activation='relu'))
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(128,activation='relu'))
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(10,activation='softmax'))
In [17]:
model.summary()
In [18]:
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 [19]:
plt.rcParams["font.sans-serif"]=["SimHei"]
plt.rcParams["font.family"]="sans-serif"
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[19]:
In [20]:
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[20]: