本笔记演示了如何通过训练变分自编码器(1, 2)来生成手写数字图片。
# 用于生成 gifpip install -q imageioWARNING: You are using pip version 20.2.2; however, version 20.2.3 is available.You should consider upgrading via the '/tmpfs/src/tf_docs_env/bin/python -m pip install --upgrade pip' command.# 导入 Tensorflow 与其他库import tensorflow as tfimport osimport timeimport numpy as npimport globimport matplotlib.pyplot as pltimport PILimport imageiofrom IPython import display
加载 MNIST 数据集
每个 MNIST 图片最初都是包含 784 个整数的向量,每个整数取值都在 0-255 之间,表示像素的强度。我们在模型中使用伯努利分布对每个像素进行建模,并对数据集进行静态二值化。
(train_images, _), (test_images, _) = tf.keras.datasets.mnist.load_data()train_images = train_images.reshape(train_images.shape[0], 28, 28, 1).astype('float32')test_images = test_images.reshape(test_images.shape[0], 28, 28, 1).astype('float32')# 标准化图片到区间 [0., 1.] 内train_images /= 255.test_images /= 255.# 二值化train_images[train_images >= .5] = 1.train_images[train_images < .5] = 0.test_images[test_images >= .5] = 1.test_images[test_images < .5] = 0.TRAIN_BUF = 60000BATCH_SIZE = 100TEST_BUF = 10000
使用 tf.data 来将数据分批和打乱
train_dataset = tf.data.Dataset.from_tensor_slices(train_images).shuffle(TRAIN_BUF).batch(BATCH_SIZE)test_dataset = tf.data.Dataset.from_tensor_slices(test_images).shuffle(TEST_BUF).batch(BATCH_SIZE)
通过 tf.keras.Sequential 连接生成网络与推理网络
在我们的 VAE 示例中,我们将两个小型的 ConvNet 用于生成和推断网络。由于这些神经网络较小,我们使用 tf.keras.Sequential
来简化代码。在下面的描述中,令 x" role="presentation" style=" box-sizing: inherit; display: inline-block; line-height: normal; word-spacing: normal; overflow-wrap: normal; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; border-width: 0px; border-style: initial; border-co