MNIST数据集
MNIST数据集是一个大型的手写体数字数据库,通常用于训练各种图像处理系统,也被广泛用于机器学习领域的训练和测试。MNIST数据库中的图像集是NIST(National Institute of Standards and Technology)的两个数据库的组合:专用数据库1和特殊数据库3。数据集是有250人手写数字组成,50%是高中学生,50%是美国人口普查局。
MNIST数据集分为60,000张的训练数据集合10,000张的测试数据集,每张图像的大小为28x28(像素);每张图像都为灰度图像,位深度为8(灰度图像是0-255)
程序代码部分
import tensorflow as tf
from tensorflow.keras.layers import Dense, Flatten, Conv2D
from tensorflow.keras import Model
# 加载并准备 MNIST 数据集。
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
# 增加维数
x_train = x_train[..., tf.newaxis]
x_test = x_test[..., tf.newaxis]
# 切分数据集,以混淆数据集
# from_tensor_slices:它的作用是把给定的元组、列表和张量等数据进行特征切片。切片的范围是从最外层维度开始的。
# shuffle(buffer_size, seed=None, reshuffle_each_iteration=None)
train_ds = tf.data.Dataset.from_tensor_slices((x_train, y_train)).shuffle(10000).batch(32)
test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32)
# 构建tf.keras模型
class MyModel(Model):
def __init__(self):
super(MyModel, self).__init__()
# ReLU(Rectified Linear Unit, 修正线性单元): max(0, x)
self.conv1 = Conv2D(32, 3, activation='softmax')
self.flatten = Flatten()
self.d1 = Dense(128, activation='softmax')
# softmax: si = e^i /(Σej)
self.d2 = Dense(10, activation='softmax')
def call(self, x):
x = self.conv1(x)
x = self.flatten(x)
x = self.d1(x)
return self.d2(x)
model = MyModel()
# 损失函数和优化器
# SparseCategoricalCrossentropy分类交叉熵函数
loss_obj = tf.keras.losses.SparseCategoricalCrossentropy()
# Adam随机梯度(SGD)下降与动量结合(Momentum)
optimizer = tf.keras.optimizers.Adam()
# 设定损失函数和准确率的评估标准
# SparseTopKCategoricalAccuracy (稀疏多分类TopK准确率,要求y_true(label)为序号编码形式)
train_loss = tf.keras.metrics.Mean(name='train_loss')
train_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='train_accuracy')
test_loss = tf.keras.metrics.Mean(name='test_loss')
test_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='test_accuracy')
# 使用tf.GradientTape来训练模型
# @ :装饰器本质上是一个Python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能.
@tf.function
def train_step(images, labels):
# 求解梯度
with tf.GradientTape() as tape:
# 得到预测结构,和进行损失值的计算
pre = model(images)
los = loss_obj(labels, pre)
# gadient是对los中的variabs自动求梯度,返回结果
gradients = tape.gradient(los, model.trainable_variables)
# 自动更新模型参数
optimizer.apply_gradients(zip(gradients, model.trainable_variables))
train_loss(los)
train_accuracy(labels, pre)
# 测试模型
@tf.function
def test_step(images, labels):
pred = model(images)
t_loss = loss_obj(labels, pred)
test_loss(t_loss)
test_accuracy(labels, pred)
if __name__ == "__main__":
EPOCH = 50
for epoch in range(EPOCH):
train_loss.reset_states()
train_accuracy.reset_states()
test_loss.reset_states()
test_accuracy.reset_states()
for images, labels in train_ds:
train_step(images, labels)
for test_images, test_labels in test_ds:
test_step(test_images, test_labels)
template = 'Epoch:{} , loss:{}, Accuracy:{}%, Test Loss:{}, Test Accuracy:{}%'
print(template.format(epoch+1, train_loss.result(), train_accuracy.result()*100, test_loss.result(), test_accuracy.result()*100))