官术网_书友最值得收藏!

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.

  1. Import the BatchNormalization method as follows:
from keras.layers.normalization import BatchNormalization
  1. 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)))
  1. 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.

主站蜘蛛池模板: 台前县| 张家界市| 东乌珠穆沁旗| 衡南县| 遵化市| 德庆县| 惠来县| 西昌市| 循化| 辛集市| 丰台区| 南京市| 松桃| 兰坪| 子洲县| 洛扎县| 会理县| 进贤县| 新丰县| 惠东县| 怀宁县| 当涂县| 绥芬河市| 宁安市| 深泽县| 江口县| 苏尼特左旗| 堆龙德庆县| 岚皋县| 驻马店市| 商城县| 葫芦岛市| 湾仔区| 桐柏县| 中西区| 舟山市| 武胜县| 水城县| 玉田县| 巢湖市| 体育|