Keras(四)实现批标准化、激活函数、dropout 本⽂将详细介绍:
实现批标准化
实现lu激活函数
实现dropout谷歌中英文在线翻译
⼀,实现批标准化
在模型中加⼊批标准化层。
激活函数relu在keras中可以独⽴以层级的⽅式加⼊。
关于BatchNormalization的介绍
1,keras实现批标准化代码如下
model = dels.Sequential()
model.add(keras.layers.Flatten(input_shape=[28,28]))
for _ in range(20):
model.add(keras.layers.Den(100, activation="relu"))
model.add(keras.layers.BatchNormalization())# 批标准化
jingle bells 下载"""
model.add(keras.layers.Den(100))
model.add(keras.layers.BatchNormalization())
model.add(keras.layers.Activation('relu'))
"""
model.add(keras.layers.Den(10, activation="softmax"))
optimizer = keras.optimizers.SGD(0.001),
metrics =["accuracy"])
2,完整代码如下
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import sklearn
import pandas as pd
import os
import sys
import time
import tensorflow as tf
from tensorflow import keras
# 1,打印模型安装包版本号
print(tf.__version__)
print(sys.version_info)
for module in mpl, np, pd, sklearn, tf, keras:
print(module.__name__, module.__version__)
# 2,加载模型数据-测试数据,测试数据,验证数据
fashion_mnist = keras.datats.fashion_mnist
(x_train_all, y_train_all),(x_test, y_test)= fashion_mnist.load_data()
x_valid, x_train = x_train_all[:5000], x_train_all[5000:]
y_valid, y_train = y_train_all[:5000], y_train_all[5000:]
print(x_valid.shape, y_valid.shape)
print(x_train.shape, y_train.shape)
print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)
# 3,将测试数据,测试数据,验证数据做标准化处理
# x = (x - u) / std
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
# x_train: [None, 28, 28] -> [None, 784]
x_train_scaled = scaler.fit_transform(x_train.astype(np.float32).reshape(-1,1)).reshape(-1,28,28) x_valid_scaled = ansform(x_valid.astype(np.float32).reshape(-1,1)).reshape(-1,28,28) x_test_scaled = ansform(x_test.astype(np.float32).reshape(-1,1)).reshape(-1,28,28)
# 4,加载模型
# dels.Sequential()
model = dels.Sequential()
model.add(keras.layers.Flatten(input_shape=[28,28]))
for _ in range(1):
model.add(keras.layers.Den(100, activation="relu"))
model.add(keras.layers.BatchNormalization())# 批标准化
# model.add(keras.layers.Den(100))
# model.add(keras.layers.BatchNormalization())
# model.add(keras.layers.Activation('relu'))
model.add(keras.layers.Den(10, activation="softmax"))
optimizer = keras.optimizers.SGD(0.001),
metrics =["accuracy"])
# 5,查看模型层级
model.summary()
# 6,添加callback,并训练模型
# Tensorboard, earlystopping, ModelCheckpoint
logdir ='./dnn-bn-callbacks'
if not ists(logdir):
os.mkdir(logdir)
output_model_file = os.path.join(logdir,
"fashion_mnist_model.h5")
callbacks =[
keras.callbacks.TensorBoard(logdir),
keras.callbacks.ModelCheckpoint(output_model_file,save_best_only =True),
keras.callbacks.EarlyStopping(patience=5, min_delta=1e-3),
]
history = model.fit(x_train_scaled, y_train, epochs=10,
validation_data=(x_valid_scaled, y_valid),
callbacks = callbacks)
# 7,使⽤matplotlib画损失,正确率变化图
def plot_learning_curves(history):
pd.DataFrame(history.history).plot(figsize=(8,5))
pastaplt.show()
plot_learning_curves(history)
# 1. 参数众多,训练不充分
# 2. 梯度消失 -> 链式法则 -> 复合函数f(g(x))
# 批标准化缓解梯度消失
aged观月记翻译# 8,使⽤估计器计算测试数据的准确率
model.evaluate(x_test_scaled, y_test, verbo=0)
⼆,实现lu激活函数
1,keras实现lu激活函数代码如下
model = dels.Sequential()
model.add(keras.layers.Flatten(input_shape=[28,28]))
for _ in range(1):
model.add(keras.layers.Den(100, activation="lu"))
# model.add(keras.layers.BatchNormalization())# 批标准化
# model.add(keras.layers.Den(100))
# model.add(keras.layers.BatchNormalization())
# model.add(keras.layers.Activation('relu'))
model.add(keras.layers.Den(10, activation="softmax"))
optimizer = keras.optimizers.SGD(0.001),dramatize
metrics =["accuracy"])
三,实现dropout
1,keras实现dropout代码如下
model = dels.Sequential()
model.add(keras.layers.Flatten(input_shape=[28,28]))
for _ in range(20):
model.add(keras.layers.Den(100, activation="lu")) model.add(keras.layers.AlphaDropout(rate=0.5))domestically
# AlphaDropout: 1. 均值和⽅差不变 2.标准化性质也不变
# model.add(keras.layers.Dropout(rate=0.5))
model.add(keras.layers.Den(10, activation="softmax"))
optimizer = keras.optimizers.SGD(0.001),女生网名2013最新版
metrics =["accuracy"])
2,完整代码如下
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import sklearn
import pandas as pd
import os
import sys
import time
import tensorflow as tf
from tensorflow import keras
# 1,打印模型安装包版本号
print(tf.__version__)
print(sys.version_info)
for module in mpl, np, pd, sklearn, tf, keras:
print(module.__name__, module.__version__)
# 2,加载模型数据-测试数据,测试数据,验证数据
fashion_mnist = keras.datats.fashion_mnist
(x_train_all, y_train_all),(x_test, y_test)= fashion_mnist.load_data() x_valid, x_train = x_train_all[:5000], x_train_all[5000:]
y_valid, y_train = y_train_all[:5000], y_train_all[5000:]
y_valid, y_train = y_train_all[:5000], y_train_all[5000:]
print(x_valid.shape, y_valid.shape)
print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)
# 3,将测试数据,测试数据,验证数据做标准化处理
# x = (x - u) / std
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
# x_train: [None, 28, 28] -> [None, 784]
x_train_scaled = scaler.fit_transform(x_train.astype(np.float32).reshape(-1,1)).reshape(-1,28,28) x_valid_scaled = ansform(x_valid.astype(np.float32).reshape(-1,1)).reshape(-1,28,28) x_test_scaled = ansform(x_test.astype(np.float32).reshape(-1,1)).reshape(-1,28,28)
# 4,加载模型
# dels.Sequential()
model = dels.Sequential()
model.add(keras.layers.Flatten(input_shape=[28,28]))
for _ in range(20):
model.add(keras.layers.Den(100, activation="lu"))
strainmodel.add(keras.layers.AlphaDropout(rate=0.5))
# AlphaDropout: 1. 均值和⽅差不变 2. 标准化性质也不变
# model.add(keras.layers.Dropout(rate=0.5))
model.add(keras.layers.Den(10, activation="softmax"))
optimizer = keras.optimizers.SGD(0.001),
metrics =["accuracy"])
# 5,查看模型层级
model.summary()
# 6,添加callback,并训练模型
# Tensorboard, earlystopping, ModelCheckpoint
logdir ='./dnn-bn-callbacks'
if not ists(logdir):
os.mkdir(logdir)
output_model_file = os.path.join(logdir,
"fashion_mnist_model.h5")
callbacks =[
keras.callbacks.TensorBoard(logdir),
keras.callbacks.ModelCheckpoint(output_model_file,save_best_only =True),
keras.callbacks.EarlyStopping(patience=5, min_delta=1e-3),
]
history = model.fit(x_train_scaled, y_train, epochs=10,
validation_data=(x_valid_scaled, y_valid),
callbacks = callbacks)
# 7,使⽤matplotlib画损失,正确率变化图
def plot_learning_curves(history):
pd.DataFrame(history.history).plot(figsize=(8,5))
家庭的英文a().t_ylim(0,3)
plt.show()
plot_learning_curves(history)
# 1. 参数众多,训练不充分
# 2. 梯度消失 -> 链式法则 -> 复合函数f(g(x))
# 批标准化缓解梯度消失
# 8,使⽤估计器计算测试数据的准确率
model.evaluate(x_test_scaled, y_test, verbo=0)
model.evaluate(x_test_scaled, y_test, verbo=0)