- Neural Networks with Keras Cookbook
- V Kishore Ayyadevara
- 214字
- 2021-07-02 12:46:30
How to do it...
In code, batch normalization is applied as follows:
Note that we will be using the same data-preprocessing steps as those we used in step 1 and step 2 in the Scaling the input dataset recipe.
- Import the BatchNormalization method as follows:
from keras.layers.normalization import BatchNormalization
- Instantiate a model and build the same architecture as we built when using the regularization technique. The only addition is that we perform batch normalization in a hidden layer:
model = Sequential()
model.add(Dense(1000, input_dim=784,activation='relu', kernel_regularizer = l2(0.01)))
model.add(BatchNormalization())
model.add(Dense(10, activation='softmax', kernel_regularizer = l2(0.01)))
- Build, compile, and fit the model as follows:
from keras.optimizers import Adam
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)
The preceding results in training that is much faster than when there is no batch normalization, as follows:

The previous graphs show the training and test loss and accuracy when there is no batch normalization, but only regularization. The following graphs show the training and test loss and accuracy with both regularization and batch normalization:

Note that, in the preceding two scenarios, we see much faster training when we perform batch normalization (test dataset accuracy of ~97%) than compared to when we don't (test dataset accuracy of ~91%).
Thus, batch normalization results in much quicker training.
- INSTANT Mock Testing with PowerMock
- 微服務(wù)設(shè)計(jì)(第2版)
- Intel Galileo Essentials
- Java 9 Concurrency Cookbook(Second Edition)
- Microsoft Dynamics 365 Extensions Cookbook
- UI智能化與前端智能化:工程技術(shù)、實(shí)現(xiàn)方法與編程思想
- 人人都懂設(shè)計(jì)模式:從生活中領(lǐng)悟設(shè)計(jì)模式(Python實(shí)現(xiàn))
- Learning Probabilistic Graphical Models in R
- 小程序,巧應(yīng)用:微信小程序開發(fā)實(shí)戰(zhàn)(第2版)
- Android嵌入式系統(tǒng)程序開發(fā):基于Cortex-A8(第2版)
- JQuery風(fēng)暴:完美用戶體驗(yàn)
- CodeIgniter Web Application Blueprints
- Getting Started with Web Components
- Mastering Machine Learning with R
- 歐姆龍PLC編程指令與梯形圖快速入門