tensorflow2.0学习笔记:回调函数(callbacks)

callbacks:回调函数,显示或者控制模型训练过程中的一些信息或者参数。
EarlyStopping:当梯度下降速度慢的时候,提前终止训练
ModelCheckpoint:保存模型
Tensorboard:训练过程可视化工具

import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import sklearn
import tensorflow as tf

from tensorflow import keras

print(tf.__version__)
2.0.0
fashion_mnist = keras.datasets.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_test.shape,y_test.shape)
(5000, 28, 28) (5000,)
(55000, 28, 28) (55000,)
(10000, 28, 28) (10000,)
#标准化:x = (x-mu)/std 均值为0,方差为1
from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()
x_train_scaled = scaler.fit_transform(
    x_train.astype(np.float32).reshape(-1,1)).reshape(-1,28,28)
x_valid_scaled = scaler.transform(
    x_valid.astype(np.float32).reshape(-1,1)).reshape(-1,28,28)
x_test_scaled = scaler.transform(
    x_test.astype(np.float32).reshape(-1,1)).reshape(-1,28,28)

print(np.max(x_train_scaled),np.min(x_train_scaled))
2.0231433 -0.8105136
#tf.keras.Sequential()

model = keras.models.Sequential()
model.add(keras.layers.Flatten(input_shape=[28,28])) #输入28*28的图像,展平成28*28的1维向量
model.add(keras.layers.Dense(300,activation="relu")) #全连接层,300个神经元,激活函数rule
model.add(keras.layers.Dense(100,activation="relu"))
model.add(keras.layers.Dense(10,activation="softmax"))#输出层为全连接层,10类,激活函数softmax,获得每一类的概率

model.compile(loss="sparse_categorical_crossentropy",
             optimizer = "adam",
             metrics = ["accuracy"]) #编译,将精度accuracy保存到metrics

# #callbacks: Tensorboard , earlystopping , ModelCheckpoint
#设置一个文件夹
if not os.path.exists(logdir):
     os.mkdir(logdir)
output_model_file = os.path.join(logdir,"fashion_mnist_model.h5")
#logdir = os.path.join('./callbacks')
# print(logdir)
#output_model_file = ("./callbacks/fashion_mnist_model.h5")

callbacks:数组类型,
TensorBoard:需要输入一个目录
ModelCheckpoint:需要输入的是模型名称
EarlyStopping: 需要输入的是的损失函数变化大小和迭代步数

callbacks = [
#     keras.callbacks.TensorBoard(logdir),
    keras.callbacks.ModelCheckpoint(output_model_file,save_best_only = True),
    keras.callbacks.EarlyStopping(patience=5,min_delta=1e-3)
]
print(callbacks)

history = model.fit(x_train_scaled,y_train,epochs=10,
                    validation_data=(x_valid_scaled,y_valid),
                    callbacks = callbacks)

你可能感兴趣的:(tensorflow2,可视化,深度学习,tensorflow,神经网络,机器学习)