导入库文件
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import sklearn
import pandas as pd
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import sys
import time
import tensorflow as tf
from tensorflow import keras
def pause():
print("SYSTEM PAUSED")
wait = input("PRESS ENTER TO CONTINUE.")
print(tf.__version__)
print(sys.version_info)
for module in mpl, np, pd, tf, keras, sklearn:
print(module.__name__, module.__version__)
导入数据,keras里自带mnist数据库,初次运行会自行下载
print("loading data from mnist...")
fashion_mnist = keras.datasets.mnist
(x_train_all, y_train_all), (x_test, y_test) = fashion_mnist.load_data()
x_val, x_train = x_train_all[:10000], x_train_all[10000:]
y_val, y_train = y_train_all[:10000], y_train_all[10000:]
print(x_val.shape, y_val.shape)
print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)
print("loading complete!")
利用sklearn工具对x进行正规化
#normalization:
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
x_train = scaler.fit_transform(x_train.astype(np.float32).reshape(-1, 1)).reshape(-1, 28, 28)
x_val = scaler.transform(x_val.astype(np.float32).reshape(-1, 1)).reshape(-1, 28, 28)
x_test = scaler.transform(x_test.astype(np.float32).reshape(-1, 1)).reshape(-1, 28, 28)
显示数据集中图片
def show_single_image(img_arr):
plt.imshow(img_arr, cmap="binary")
plt.show()
def show_imgs(n_rows, n_cols, x_data, y_data, class_names):
assert len(x_data) == len(y_data)
assert n_rows * n_cols < len(x_data)
plt.figure(figsize = (n_cols, n_rows))
for row in range(n_rows):
for col in range(n_cols):
index = n_cols * row +col
plt.subplot(n_rows, n_rows, index+1)
plt.imshow(x_data[index], cmap='binary',
interpolation = 'nearest')
plt.axis('off')
plt.title(class_names[y_data[index]])
plt.show()
class_names = ['0', '1', '2', '3', '4',
'5', '6', '7', '8', '9']
print("showing examples...")
show_imgs(4, 4, x_train, y_train, class_names)
pause()
构建神经网络层次
print("building model...")
model = keras.models.Sequential()
model.add(keras.layers.Flatten(input_shape=[28,28]))
model.add(keras.layers.Dense(300, activation = 'relu'))
model.add(keras.layers.Dense(100, activation = 'relu'))
model.add(keras.layers.Dense(10, activation = 'softmax'))
定义损失函数,优化器和量值
model.compile(loss = 'sparse_categorical_crossentropy',
optimizer = 'adam',
metrics = ["accuracy"])
print("architecture info:")
num_layers = model.layers
model.summary()
训练并保存模型,在TensorBoard里查看模型
logdir = os.path.join("callbacks")
print(logdir)
if not os.path.exists(logdir):
os.mkdir(logdir)
output_model_flie = os.path.join(logdir,
"mnist_model.h5")
callbacks = [
keras.callbacks.TensorBoard(logdir),
keras.callbacks.ModelCheckpoint(output_model_flie,
save_best_only = True),
keras.callbacks.EarlyStopping(patience=5, min_delta = 1e-3)]
history = model.fit(x_train, y_train, epochs = 10,
validation_data = (x_val, y_val), callbacks = callbacks)
print("training complete!")
绘制学习曲线,查看收敛情况
print("training the classifier...")
history = model.fit(x_train, y_train, epochs = 5,
validation_data = (x_val, y_val))
def plot_learning_curves(history):
pd.DataFrame(history.history).plot(figsize = (8, 5))
plt.grid(True)
plt.gca().set_ylim(0, 1)
plt.show()
print("plotting learning curves:")
plot_learning_curves(history)
执行结果:
在TensorBoard中查看模型