Keras中的VAE:如何定义端到端模型?

时间:2018-11-11 01:31:37

标签: 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之间的损失?谢谢

1 个答案:

答案 0 :(得分:0)

x在整个模型中都在变化,其中'a'设置为'a' in True作为解码器模型的最后一层。

在进行x = layers.Conv2D(1, 3,padding='same', activation='sigmoid')(x)时,您将解码器直接链接到编码器之后,x实际上是解码器的输出层,因此与之前的z_decoded = decoder(z)相同。同样,您可以在实际输入和输出之间创建链接。

计算损耗将在两者上产生相同的结果(因为它们都表示相同的层)。
简而言之-z_decodedx都是端到端模型,我建议使用z_decoded版本,以提高可读性。