keras 层“解码器”期望1个输入,但它接收了3个输入Tensor

neskvpey  于 2023-04-21  发布在  其他
关注(0)|答案(1)|浏览(128)

我正在尝试训练一个自动编码器(实际上拟合似乎是正确的)。然后我想测试我的模型:

encoded_imgs = encoder.predict(images[:10])
decoded_imgs = decoder.predict(encoded_imgs)

其中images是一个图像数组(224,224),潜在向量等于1024。我希望encoded_imgs是10x1024,但实际上是3x10x24,这导致了我执行decoder.predict时标题的错误。为什么编码器的结果具有这种形状?
我将添加编码器和解码器的结构,而预测使用标准的training.py库

latent_dim = 1024
encoder_inputs = Input(shape=(224, 224))
x = layers.Reshape((224, 224, 1))(encoder_inputs) # add a batch dimension
x = layers.Conv2D(32, 3, activation="relu", strides=2, padding="same")(x)
x = layers.MaxPool2D()(x)
x = layers.Conv2D(64, 3, activation="relu", strides=2, padding="same")(x)
x = layers.MaxPool2D()(x)
x = layers.Conv2D(128, 3, activation="relu", strides=2, padding="same")(x)
x = layers.Flatten()(x)
x = layers.Dense(4096, activation="relu")(x)
z_mean = layers.Dense(latent_dim, name="z_mean")(x)
z_log_var = layers.Dense(latent_dim, name="z_log_var")(x)
z = Sampling()([z_mean, z_log_var])
encoder = Model(encoder_inputs, [z_mean, z_log_var, z], name="encoder")

latent_inputs = Input(shape=(latent_dim,))
x = layers.Dense(7 * 7 * 64, activation="relu")(latent_inputs)
x = layers.Reshape((7, 7, 64))(x)
x = layers.Conv2DTranspose(128, 3, activation="relu", strides=2, padding="same")(x)
x = layers.UpSampling2D(size=(2, 2))(x)
x = layers.Conv2DTranspose(64, 3, activation="relu", strides=2, padding="same")(x)
x = layers.UpSampling2D(size=(2, 2))(x)
x = layers.Conv2DTranspose(32, 3, activation="relu", strides=2, padding="same")(x)
x = layers.Conv2DTranspose(1, 3, activation="sigmoid", padding="same")(x)
decoder_outputs = layers.Reshape((224, 224))(x)
decoder = Model(latent_inputs, decoder_outputs, name="decoder")

如果你认为需要一些额外的信息来回答,告诉我,我会添加它。

tvokkenx

tvokkenx1#

Meh,你可以看到编码器的输出是一个元组:(z_mean, z_log_var, z)。所以你只需要最后一个作为你的解码器输入。

encoded_imgs = encoder.predict(images[:10])[-1]
decoded_imgs = decoder.predict(encoded_imgs)

相关问题