我正在学习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_img
和 z_decoded
上的损失还是 input_img
和 x
之间的损失?谢谢
最佳答案
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/