import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
# 下载数据集并划分为目标集和测试集
(train_image,train_lable),(test_image,test_label) = tf.keras.datasets.fashion_mnist.load_data()
train_image.shape
# 在最后一个维度扩张,扩张成一个4维数据
train_image = np.expand_dims(train_image,-1)
train_image.shape # (None,hight,witch,chanal)1表示黑白3彩色
test_image = np.expand_dims(test_image,-1)
# 建立模型
model = tf.keras.Sequential()# 顺序模型
model.add(tf.keras.layers.Conv2D(32,(3,3),
input_shape=train_image.shape[1:],
activation="relu"))# 建立卷积层
#每层建立32个卷积核,卷积核大小(3*3)
#输入图片的形状如(60000,28,28,1除去第0位的图片个数)就是28*28*1
# 激活函数relu
model.add(tf.keras.layers.MaxPool2D())# 最大池化默认2*2形状
model.add(tf.keras.layers.Conv2D(64,(3,3),activation="relu"))# 再次添加卷积层2的n次方形式添加卷积核
model.add(tf.keras.layers.GlobalAveragePooling2D())# 全局平均值池化
model.add(tf.keras.layers.Dense(10,activation="softmax"))# 输出
model.summary()
# 训练模型
model.compile(optimizer="adam",
loss="sparse_categorical_crossentropy",
metrics=["acc"])
history = model.fit(train_image,train_lable,epochs=30,
validation_data=(test_image,test_label))
history.history.keys()
# 正确率(通过绘图得train数据得分不高未达到拟合 test数据过拟合)
plt.plot(history.epoch,history.history.get("acc"),label="acc")
plt.plot(history.epoch,history.history.get("val_acc"),label="val_acc")
# 误差
plt.plot(history.epoch,history.history.get("loss"),label="loss")
plt.plot(history.epoch,history.history.get("val_loss"),label="val_loss")
优化模型
# CNN优化增加卷积层及抑制拟合:增大测试训练集隐藏单元数增大拟合,降低抑制数据拟合
# 建立模型
model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(64,(3,3),
input_shape=train_image.shape[1:],
activation="relu",
padding="same"))
model.add(tf.keras.layers.Conv2D(64,(3,3),activation="relu",padding="same"))
model.add(tf.keras.layers.Conv2D(64,(3,3),activation="relu",padding="same"))
model.add(tf.keras.layers.MaxPool2D())
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Conv2D(128,(3,3),activation="relu",padding="same"))
model.add(tf.keras.layers.Conv2D(128,(3,3),activation="relu",padding="same"))
model.add(tf.keras.layers.MaxPool2D())
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Conv2D(256,(3,3),activation="relu",padding="same"))
model.add(tf.keras.layers.Conv2D(256,(3,3),activation="relu",padding="same"))
model.add(tf.keras.layers.MaxPool2D())
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Conv2D(512,(3,3),activation="relu",padding="same"))
model.add(tf.keras.layers.Conv2D(512,(3,3),activation="relu",padding="same"))
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.GlobalAveragePooling2D())
model.add(tf.keras.layers.Dense(256,activation="relu"))
model.add(tf.keras.layers.Dense(10,activation="softmax"))