61. 构建 LeNet-5 Estimator#

61.1. 介绍#

前面的实验中,我们使用 TensorFlow 低阶 tf.nn 模块和高阶 tf.keras 模块,以及 PyTorch 模块 nn.Modulenn.Sequential 等 4 种不同的方法来构建了 LeNet-5 经典卷积神经网络。本次挑战将使用学习过的 TensorFlow Estimator 高阶 API 来重构 LeNet-5 并完成训练。

61.2. 知识点#

  • TensorFlow Estimator 使用

  • LeNet-5 卷积神经网络

61.3. LeNet-5 结构#

前面,我们已经使用 2 个框架共 4 种方法实现了 LeNet-5 卷积神经网络,相信你对其结构已经十分熟悉了。

image

实际上,在学习 TensorFlow 高阶 API 的实验中,我们还介绍了 TensorFlow Estimator。Estimator 具备很多优点,尤其是在生产环境部署,很多优质论文提供的示例代码也是通过 Estimator 分发。

所以,本次挑战你将独立尝试使用 TensorFlow 自定义一个 LeNet-5 网络的 Estimator,并完成训练。由于前面的实验只学习了使用 TensorFlow 提供的预定义 Estimator,所以挑战会提供一些自定义 Estimator 资料供你学习。

Exercise 61.1

开放型挑战

挑战:使用 TensorFlow 构建一个包含 LeNet-5 结构的 Estimator,并完成模型训练和评估。

规定:神经网络结构必须与 LeNet-5 完全一致。可以自由定义损失函数,优化方法及其他超级参数。挑战将提供 MNIST 数据及预处理过程。

首先,我们加载所需 MNIST 数据。

import numpy as np
import tensorflow as tf

# 从课程镜像服务器下载 MNIST NumPy 数据
DATA_URL = "https://cdn.aibydoing.com/hands-on-ai/files/mnist.npz"

path = tf.keras.utils.get_file("mnist.npz", DATA_URL)
with np.load(path) as data:
    # 将 28x28 图像 Padding 至 32x32
    x_train = np.pad(
        data["x_train"].reshape([-1, 28, 28, 1]),
        ((0, 0), (2, 2), (2, 2), (0, 0)),
        "constant",
    )
    y_train = data["y_train"]
    x_test = np.pad(
        data["x_test"].reshape([-1, 28, 28, 1]),
        ((0, 0), (2, 2), (2, 2), (0, 0)),
        "constant",
    )
    y_test = data["y_test"]

x_train.shape, y_train.shape, x_test.shape, y_test.shape
((60000, 32, 32, 1), (60000,), (10000, 32, 32, 1), (10000,))

上面,我们已经将原 \(28 \times 28 \times 1\) 的原始 NumPy 数组扩充为 \(32 \times 32 \times 1\) 尺寸。接下来,请大家参与 TensorFlow 官方指南,自学从 Keras 模型到 Estimator 模型的内容,基于课程前面构建的 Keras LeNet-5 模型,将其转换为 Estimator 模型并完成训练。

参考学习资料