# tf.keras--callbacks

```python
# 回调函数
# 是在模型的训练过程中进行调用实现的，作为一个参数传入model.fit()中

import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import sklearn
import pandas as pd
import os
import sys
import time
import tensorflow as tf
from tensorflow import keras
```

```python
# 获取图片数据集
fashion_mnist = keras.datasets.fashion_mnist
(x_train_all,y_train_all),(x_test,y_test) = fashion_mnist.load_data()
# 将训练集拆分为训练集和验证集 共60000张图片，前5000张为验证集，后55000张为训练集
x_valid,x_train = x_train_all[:5000],x_train_all[5000:]
y_valid,y_train = y_train_all[:5000],y_train_all[5000:]
```

```python
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)
```

```python
model = keras.models.Sequential()

model = keras.models.Sequential([
    keras.layers.Flatten(input_shape=[28, 28]),
    keras.layers.Dense(300,activation='relu'),
    keras.layers.Dense(100,activation='relu'),
    keras.layers.Dense(10,activation='softmax')
])

model.compile(loss='sparse_categorical_crossentropy', 
             optimizer = 'adam',
             metrics = ["accuracy"])
```

```python
# 这里使用三个callback函数，TensorBoard, EarlyStopping, ModelCheckpoint
# Tensorboard需要一个文件夹存数据，ModelCheckpoint需要一个文件存数据
# logdir = './callbacks'  此处报错，改用下行
logdir = os.path.join("callbacks")
if not os.path.exists(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), # save_best_only保存最好的模型，否则默认保存最近训练的模型
#     如果连续5次迭代的变化值都小于阈值min_delta，则停止训练
    keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=5, min_delta=1e-2)
]
# 开始训练  callbacks作为参数填入
history = model.fit(x_train_scaled, y_train, epochs=30, validation_data=(x_valid_scaled, y_valid), callbacks = callbacks)
```

```
# 可以看出5次val_accuracy的值都没有提高1e-2，故回调函数EarlyStopping停止了训练
Train on 55000 samples, validate on 5000 samples
Epoch 1/30
55000/55000 [==============================] - 7s 119us/sample - loss: 0.0884 - accuracy: 0.9666 - val_loss: 0.6621 - val_accuracy: 0.8948
Epoch 2/30
55000/55000 [==============================] - 6s 117us/sample - loss: 0.0893 - accuracy: 0.9677 - val_loss: 0.6010 - val_accuracy: 0.9000
Epoch 3/30
55000/55000 [==============================] - 6s 115us/sample - loss: 0.0848 - accuracy: 0.9681 - val_loss: 0.6775 - val_accuracy: 0.8916
Epoch 4/30
55000/55000 [==============================] - 6s 116us/sample - loss: 0.0810 - accuracy: 0.9701 - val_loss: 0.6324 - val_accuracy: 0.8916
Epoch 5/30
55000/55000 [==============================] - 6s 116us/sample - loss: 0.0822 - accuracy: 0.9687 - val_loss: 0.6530 - val_accuracy: 0.8936
Epoch 6/30
55000/55000 [==============================] - 6s 118us/sample - loss: 0.0824 - accuracy: 0.9695 - val_loss: 0.6708 - val_accuracy: 0.8952
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bing-dong.gitbook.io/tensorflow/tf.keras--callbacks.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
