我在这里学习教程。我的模型部分是:
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_img
和z_decoded
上的损耗还是input_img
和x
之间的损耗?感谢
x在整个模型中都在变化,其中x = layers.Conv2D(1, 3,padding='same', activation='sigmoid')(x)
将x
设置为解码器模型的最后一层
在执行z_decoded = decoder(z)
时,将解码器直接链接到编码器之后,z_decoded
实际上是解码器的输出层,因此与前面的x
相同。此外,您还可以在实际输入和输出之间创建链接
计算损失将在两者上产生相同的结果(因为它们都代表同一层(
简而言之,vae = Model(input_img, z_decoded)
和vae = Model(input_img, x)
都是端到端模型,为了可读性,我建议使用z_decoded版本。