我正在使用convd2d和conv2dtranspose创建CVAE。当使用conv2dtranspose时,我无法复制编码器,因为形状尺寸是奇数而不是偶数。我已经搜索了如何做到这一点,但我找不到任何有用的东西。我添加和删除了不同的步长,并调整了填充,但它不起作用。我在下面附上了编码器和解码器,以及它们各自的模型摘要。主要问题是在conv2d_transpose1中,当输出形状应该是(None,64,4,32(时,输出形状是(None,64,3,32(
self.latent_dim = 6
self.encoder = tf.keras.Sequential(
[
tf.keras.layers.InputLayer(input_shape=input_shape),
tf.keras.layers.Conv2D(filters=32, kernel_size=3, strides=(1, 1), activation='relu', padding='same'),
tf.keras.layers.Conv2D(filters=32, kernel_size=3, strides=(2, 2), activation='relu', padding='same'),
tf.keras.layers.Conv2D(filters=64, kernel_size=3, strides=(2, 2), activation='relu', padding='same'),
tf.keras.layers.Conv2D(filters=64, kernel_size=3, strides=(2, 2), activation='relu', padding='same'),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(64, activation='relu'),
# No activation
tf.keras.layers.Dense(latent_dim + latent_dim),
]
)
self.decoder = tf.keras.Sequential(
[
tf.keras.layers.InputLayer(input_shape=(latent_dim,)),
tf.keras.layers.Dense(units=16 * 1 * 64, activation=tf.nn.relu),
tf.keras.layers.Reshape(target_shape=(16, 1, 64)),
tf.keras.layers.Conv2DTranspose(filters=64, kernel_size=3, strides=2, padding='same', activation='relu'),
tf.keras.layers.Conv2DTranspose(filters=32, kernel_size=3, strides=2, padding='same', activation='relu',),
# No activation
tf.keras.layers.Conv2DTranspose(filters=32, kernel_size=3, strides=2, padding='same', activation='relu'),
tf.keras.layers.Conv2D(filters=1, kernel_size=3, strides=(1, 1), padding='same'),
]
)
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 128, 6, 32) 320
_________________________________________________________________
conv2d_1 (Conv2D) (None, 64, 3, 32) 9248
_________________________________________________________________
conv2d_2 (Conv2D) (None, 32, 2, 64) 18496
_________________________________________________________________
conv2d_3 (Conv2D) (None, 16, 1, 64) 36928
_________________________________________________________________
flatten (Flatten) (None, 1024) 0
_________________________________________________________________
dense (Dense) (None, 64) 65600
_________________________________________________________________
dense_1 (Dense) (None, 12) 780
=================================================================
Total params: 131,372
Trainable params: 131,372
Non-trainable params: 0
_________________________________________________________________
None
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_2 (Dense) (None, 1024) 7168
_________________________________________________________________
reshape (Reshape) (None, 16, 1, 64) 0
_________________________________________________________________
conv2d_transpose (Conv2DTran (None, 32, 2, 64) 36928
_________________________________________________________________
conv2d_transpose_1 (Conv2DTr (None, 64, 4, 32) 18464
_________________________________________________________________
conv2d_transpose_2 (Conv2DTr (None, 128, 8, 32) 9248
_________________________________________________________________
conv2d_4 (Conv2D) (None, 128, 8, 1) 289
=================================================================
Total params: 72,097
Trainable params: 72,097
Non-trainable params: 0
_________________________________________________________________
步长为2的Conv2DTranspose使前两个维度加倍,因此(None,32,2,32