环境: Ubuntu 18.04, tensorflow 2.4.1
mnist是Yann Lecun大神的手写数据,数据中的数字都是28X28的图像,每个像素点是[0-255]的值
其中训练数据为60000,测试数据为10000
打印出数字看一下
def show_mnist(images, labels):
n = 5
m = 5
for i in range(n):
for j in range(m):
plt.subplot(n, m, i*n+j+1)
index = i*n+j
array = images[index]
plt.title(labels[index])
plt.imshow(array, cmap='Greys')
plt.show()
准备数据
因为数据是三维的, (60000,28,28),但是我们构建的网络是
# 200 个 epoch
EPOCH = 200
BATCH_SIZE = 128
VERBOSE = 1
# 需要把图像二位数据压缩为一维数据
RESHAPED = 784 # 28*28
# 数字分类类别 0~9 一共10个
NUM_CLASSES = 10
# 载入数据,第一次使用时,会稍微花点时间下载
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
# 将图像二维数据压缩为一维,同时将数据转为浮点型
x_train = x_train.reshape(60000, RESHAPED).astype('float32')
x_test = x_test.reshape(10000, RESHAPED).astype('float32')
# normalization
# 如果数据不做归一化,其实训练也可以进行,但是会影响最终的loss
x_train /= 255
x_test /= 255
# 将label的数字 0~9 转为hothot
y_train = tf.keras.utils.to_categorical(y_train, NUM_CLASSES)
y_test = tf.keras.utils.to_categorical(y_test, NUM_CLASSES)
构建网络
使用最简单的一层网络
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(NUM_CLASSES, input_shape=(RESHAPED,), activation='softmax'))
model.summary()
训练
## compile network
model.compile(loss=tf.keras.losses.categorical_crossentropy, optimizer='adam', metrics=['accuracy'])
history = model.fit(x_train, y_train, batch_size=BATCH_SIZE, epochs=EPOCH, verbose=VERBOSE)
## validation 92.36
val_loss,val_acc = model.evaluate(x_test, y_test, verbose=VERBOSE)
print("Test loss: ", val_loss)
print("Test accuracy: ", val_acc)
完成代码
import tensorflow as tf
import matplotlib.pyplot as plt
# from PIL import Image
EPOCH = 200
BATCH_SIZE = 128
VERBOSE = 1
RESHAPED = 784 # 28*28
NUM_CLASSES = 10
def show_mnist(images, labels):
n = 5
m = 5
for i in range(n):
for j in range(m):
plt.subplot(n, m, i*n+j+1)
index = i*n+j
array = images[index]
plt.title(labels[index])
plt.imshow(array, cmap='Greys')
plt.show()
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
print('training images data shape:', x_train.shape)
print('training labels data shape:', y_train.shape)
print('testing images data shape:', x_test.shape)
print('testing labels data shape:', y_test.shape)
#show_mnist(x_train, y_train)
x_train = x_train.reshape(60000, RESHAPED).astype('float32')
x_test = x_test.reshape(10000, RESHAPED).astype('float32')
# normalization
x_train /= 255
x_test /= 255
#
y_train = tf.keras.utils.to_categorical(y_train, NUM_CLASSES)
y_test = tf.keras.utils.to_categorical(y_test, NUM_CLASSES)
# train_labels = train_labels.reshape(60000, RESHAPED)
## build network
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(NUM_CLASSES, input_shape=(RESHAPED,), activation='softmax'))
model.summary()
## compile network
model.compile(loss=tf.keras.losses.categorical_crossentropy, optimizer='adam', metrics=['accuracy'])
history = model.fit(x_train, y_train, batch_size=BATCH_SIZE, epochs=EPOCH, verbose=VERBOSE)
## validation 92.36
val_loss,val_acc = model.evaluate(x_test, y_test, verbose=VERBOSE)
print("Test loss: ", val_loss)
print("Test accuracy: ", val_acc)