Keras(四)实现批标准化、激活函数、dropout

更新时间:2023-06-09 11:43:59 阅读: 评论:0

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())# 批标准化
"""
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))
plt.show()
plot_learning_curves(history)
# 1. 参数众多,训练不充分
# 2. 梯度消失 -> 链式法则 -> 复合函数f(g(x))
#    批标准化缓解梯度消失
# 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),
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))
# 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"])
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"))
model.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))
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)

本文发布于:2023-06-09 11:43:59,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/910903.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:测试数据   标准化   模型   数据   验证   实现
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图