卷积自编码器_卷积变分自编码器

本笔记演示了如何通过训练变分自编码器(1, 2)来生成手写数字图片。

cbce3162f972b3b79ad284869c916669.gif

# 用于生成 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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值