导入包
import tensorflow as tf
import numpy as np
import pathlib
查看tensorflow的版本
print('Tensorflow version: {}'.format(tf.__version__))
Tensorflow version: 2.3
读取数据
(train_image, train_label),(test_image, test_label) = tf.keras.datasets.fashion_mnist.load_data()
查看数据图像的shape(维度)
train_image.shape,test_image.shape
((60000, 28, 28), (10000, 28, 28))
查看数据图像标签的shape(维度)
train_label.shape,test_label.shape
((60000,), (10000,))
测试标签的输出格式
train_label
array([9, 0, 0, ..., 3, 0, 5], dtype=uint8)
显示其中一张图像
plt.imshow(test_image[0])
train_image = train_image/255
test_image = test_image/255
归一化后的数据图像的shape(维度)
train_image.shape,test_image.shape
((60000, 28, 28), (10000, 28, 28))
建立模型
model = tf.keras.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=(28,28)))
model.add(tf.keras.layers.Dense(128, activation='relu'))
model.add(tf.keras.layers.Dense(10, activation='softmax'))
模型概述
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
flatten (Flatten) (None, 784) 0
_________________________________________________________________
dense (Dense) (None, 128) 100480
_________________________________________________________________
dense_1 (Dense) (None, 10) 1290
=================================================================
Total params: 101,770
Trainable params: 101,770
Non-trainable params: 0
模型编译:目标值是顺序编码,使用sparse_categorical_crossentropy损失函数
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['acc'])
模型训练
model.fit(train_image, train_label, epochs=5)
模型评估
model.evaluate(test_image,test_label)
10000/10000 [==============================] - 1s 78us/sample - loss: 0.3496 - acc: 0.8767
[0.3496406935095787, 0.8767]
标签顺序值转换为杜热编码型
train_label_onehot = tf.keras.utils.to_categorical(train_label)
train_label_onehot[0],train_label[0]
(array([0., 0., 0., 0., 0., 0., 0., 0., 0., 1.], dtype=float32), 9)
测试集标签转换为独热编码型
test_label_onehot = tf.keras.utils.to_categorical(test_label)
建立模型
model = tf.keras.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=(28,28)))
model.add(tf.keras.layers.Dense(128, activation='relu'))
model.add(tf.keras.layers.Dense(10, activation='softmax'))
模型概述
model.summary()
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
flatten_1 (Flatten) (None, 784) 0
_________________________________________________________________
dense_2 (Dense) (None, 128) 100480
_________________________________________________________________
dense_3 (Dense) (None, 10) 1290
=================================================================
Total params: 101,770
Trainable params: 101,770
Non-trainable params: 0
模型编译:目标值是独热编码,使用categorical_crossentropy损失函数
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), loss='categorical_crossentropy', metrics=['acc'])
模型训练
history = model.fit(train_image, train_label_onehot, epochs=5, validation_data=(test_image, test_label_onehot))
history.history.keys()
dict_keys(['loss', 'acc', 'val_loss', 'val_acc'])
训练过程结果显示
plt.plot(history.epoch, history.history.get('loss'))
plt.plot(history.epoch, history.history.get('val_loss'))
plt.plot(history.epoch, history.history.get('acc'))
plt.plot(history.epoch, history.history.get('val_acc'))