python - Keras 中的 VAE : how to define the end-to-end model?

标签 python keras autoencoder

我正在学习tutorial这里。我的模型部分是:

input_img = keras.Input(shape=img_shape)

x = layers.Conv2D(32, (3, 3),
                  padding='same', activation='relu')(input_img)

...
x = layers.Conv2D(64, (3, 3),
                  padding='same', activation='relu')(x)
shape_before_flattening = K.int_shape(x)

x = layers.Flatten()(x)
x = layers.Dense(32, activation='relu')(x)

z_mean = layers.Dense(latent_dim)(x)
z_log_var = layers.Dense(latent_dim)(x)

def sampling(args):
    ...

z = layers.Lambda(sampling)([z_mean, z_log_var])

decoder_input = layers.Input(K.int_shape(z)[1:])

x = layers.Dense(np.prod(shape_before_flattening[1:]),
                 activation='relu')(decoder_input)

x = layers.Reshape(shape_before_flattening[1:])(x)

x = layers.Conv2DTranspose(32, 3,
                           padding='same', activation='relu',
                           strides=(2, 2))(x)
x = layers.Conv2D(1, 3,
                  padding='same', activation='sigmoid')(x)

# This is our decoder model from letent space to reconstructed images
decoder = Model(decoder_input, x)

# We then apply it to `z` to recover the decoded `z`.
z_decoded = decoder(z)

def vae_loss(self, x, z_decoded):
    ...


# Fit the end-to-end model
vae = Model(input_img, z_decoded) # vae = Model(input_img, x)
vae.compile(optimizer='rmsprop', loss=vae_loss)
vae.summary()

我的问题是:端到端是 vae = Model(input_img, z_decoded)vae = Model(input_img, x)。我们应该计算 input_imgz_decoded 上的损失还是 input_imgx 之间的损失?谢谢

最佳答案

x 在整个模型中不断变化,其中 x =layers.Conv2D(1, 3,padding='same',activation='sigmoid')(x) 您设置 x 作为解码器模型的最后一层。

当执行 z_decoded = detector(z) 时,您将解码器直接链接在编码器之后,z_decoded 实际上是解码器的输出层,因此,相同的 x 如前所述。此外,您还可以创建实际输入和输出之间的链接。

计算损失将在两者上产生相同的结果(因为它们都代表同一层)。
简而言之 - vae = Model(input_img, z_decoded)vae = Model(input_img, x) 都是端到端模型,我建议使用 z_decoded 版本,为了便于阅读。

关于python - Keras 中的 VAE : how to define the end-to-end model?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53245079/

相关文章:

python - 使用 tf.train.Checkpoint 在 keras 中保存 GAN

python - Redshift COPY 操作在 SQLAlchemy 中不起作用

python - Heroku、Flask、Flask-Mail、Gmail

python - 无法将多个回调传递给 keras 模型

python-3.x - 如何使用opencv python查找图像中的最小矩形?

python - 自动编码器内 conv2d 层的形状大小不匹配

python - 为什么这个 sqlite python 3x 代码与 python 27 不兼容

python - 使用python的elasticsearch批量索引

python - ValueError : Error when checking target: expected model_2 to have shape (None, 252, 252, 1) 但得到形状为 (300, 128, 128, 3) 的数组

computer-vision - 如何确定用于图像分类的卷积神经网络的参数?