Variational Autoencoder 变分自动编码器

本文逐步介绍了如何构建一个变分自编码器(VAE),从最简单的全连接层开始,逐渐深入到使用多层全链接层和深度卷积网络。通过CIFAR10数据集展示了VAE的训练过程,并详细解析了VAE的损失函数,特别是变分推断中的KL散度和重构造误差。最后,解释了重参数化技巧在解决采样不可导问题中的关键作用。

一步一步实现一个VAE

大部分来自Keras VAE的教程,不过没有使用mnist,而是用了cifar10的数据集

最简单的两个全链接层的Autoencoder

先贴个代码:

# this is the size of our encoded representations
encoding_dim = 32  # 32 floats -> compression of factor 24.5, assuming the input is 784 floats
# this is our input placeholder
input_img = Input(shape=(784,))
# "encoded" is the encoded representation of the input
encoded = Dense(encoding_dim, activation='relu')(input_img)
# "decoded" is the lossy reconstruction of the input
decoded = Dense(784, activation='sigmoid')(encoded)
# this model maps an input to its reconstruction
autoencoder = Model(input_img, decoded)
# this model maps an input to its encoded representation
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

这里就是把输入层放入一个全链接层,压缩成为一个32维的数组,然后再使用这个32维的数组用一个全链接层去还原原来的图片。
下面这段代码是用来显示效果,这里还是用的mnist的数据集。

encoder = Model(input_img, encoded)
# create a placeholder for an encoded (32-dimensional) input
encoded_input = Input(shape=(encoding_dim,))
# retrieve the last layer of the autoencoder model
decoder_layer = autoencoder.layers[-1]
# create the decoder model
decoder = Model(encoded_input, decoder_layer(encoded_input))

结果如下:
两个全链接层autoencoder的效果

使用多层全链接层的Autoencoder

input_img = Input(shape=(784,))
encoded = Dense(128, activation='relu')(input_img)
encoded = Dense(64, activation='relu')(encoded)
encoded = Dense(32, activation='relu')(encoded)

decoded = Dense(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值