📜 原文链接:https://tensorflow.google.cn/tutorials/quickstart/advanced
将 TensorFlow 载入到程序中:
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]
使用 tf.data
将数据集切分为批(batch)数据集,并对数据进行打乱:
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)
通过 Keras 模块子类 API 构建 tf.keras
模型:
class MyModel(Model):
def __init__(self):
super(MyModel, self).__init__()
self.conv1 = Conv2D(32, 3, activation='relu')
self.flatten = Flatten()
self.d1 = Dense(128, activation='relu')
self.d2 = Dense(10)
def call(self, x):
x = self.conv1(x)
x = self.flatten(x)
x = self.d1(x)
return self.d2(x)
# Create an instance of the model
model = MyModel()
选择模型训练的优化器和损失函数:
loss_obj = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
optimizer = tf.keras.optimizers.Adam()
选择衡量指标来度量模型的损失值(loss)和准确率(accuracy),这些指标的值在每次迭代(epoch)上累加,然后打印出整体的结果:
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
来训练模型:
@tf.function
def train_step(images, labels):
with tf.GradientTape() as tape:
# train=True is only needed if there are layers with different
# behavior during training versus inference(e.g. Dropout)
predictions = model(images, training=True)
loss = loss_obj(labels, predictions)
gradients = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(zip(gradients, model.trainable_variables))
train_loss(loss)
train_accuracy(labels, predictions)
测试模型:
@tf.function
def test_step(images, labels):
# train=False is only needed if there are layers with different
# behavior during training versus inference(e.g. Dropout)
predictions = model(images, training=False)
t_loss = loss_obj(labels, predictions)
test_loss(t_loss)
test_accuracy(labels, predictions)
EPOCHS = 5
for epoch in range(EPOCHS):
# Reset the metrics at the start of the next 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))
📌 终端输出结果:
Epoch 1, Loss: 0.041934385895729065, Accuracy: 98.72000122070312, Test Loss: 0.05677669122815132, Test Accuracy: 98.13999938964844
Epoch 2, Loss: 0.022656703367829323, Accuracy: 99.2699966430664, Test Loss: 0.056276965886354446, Test Accuracy: 98.27999877929688
Epoch 3, Loss: 0.014473672024905682, Accuracy: 99.51333618164062, Test Loss: 0.06184951588511467, Test Accuracy: 98.19999694824219
Epoch 4, Loss: 0.007586538791656494, Accuracy: 99.7683334350586, Test Loss: 0.06390485912561417, Test Accuracy: 98.38999938964844
Epoch 5, Loss: 0.0070701888762414455, Accuracy: 99.76499938964844, Test Loss: 0.07669035345315933, Test Accuracy: 98.30999755859375