- Neural Networks with Keras Cookbook
- V Kishore Ayyadevara
- 225字
- 2021-07-02 12:46:29
Overcoming overfitting using dropout
In the previous section of overcoming overfitting using regularization, we used L1/ L2 regularization as a means to avoid overfitting. In this section, we will use another tool that is helpful to achieve the same—dropout.
Dropout can be considered a way in which only a certain percentage of weights get updated, while the others do not get updated in a given iteration of weight updates. This way, we are in a position where not all weights get updated in a weight update process, thus avoiding certain weights to achieve a very high magnitude when compared to others:
model = Sequential()
model.add(Dense(1000, input_dim=784, activation='relu'))
model.add(Dropout(0.75))
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy'])
history = model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=100, batch_size=1024, verbose=1)
In the preceding code, we have given a dropout of 0.75; that is, randomly, 75% of weights do not get updated in a certain weight update iteration.
The preceding would result in the gap between the training and test accuracy being not as high as it is when the model was built without dropout in the previous scenario, where the spread of weights was higher.
Note the histogram of weights of the first layer now:
plt.hist(model.get_weights()[-2].flatten())

Note that in the preceding scenario, the frequency count of weights that are beyond 0.2 or -0.2 is less when compared to the 100-epochs scenario.
- 密碼學(xué)原理與Java實(shí)現(xiàn)
- Java面向?qū)ο笏枷肱c程序設(shè)計(jì)
- Interactive Data Visualization with Python
- Java面向?qū)ο蟪绦蜷_(kāi)發(fā)及實(shí)戰(zhàn)
- Building Cross-Platform Desktop Applications with Electron
- Python貝葉斯分析(第2版)
- Mastering Google App Engine
- 小學(xué)生C++創(chuàng)意編程(視頻教學(xué)版)
- Corona SDK Mobile Game Development:Beginner's Guide(Second Edition)
- Extending Puppet(Second Edition)
- Mastering ROS for Robotics Programming
- HTML5從入門(mén)到精通(第4版)
- Python 3.7從入門(mén)到精通(視頻教學(xué)版)
- Bootstrap for Rails
- Windows Phone 8 Game Development