Flatten and back keras

我正在尝试使用自编码器获取简单向量中的值

这是我的代码

input_img = Input(shape=(28, 28, 1))x = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)x = MaxPooling2D((2, 2), padding='same')(x)x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)encoded = MaxPooling2D((2, 2), padding='same')(x)

这里我需要一个平坦层

encoder = Model(input_img, encoded)

然后再将其转换回卷积层

encoderOutputShape = encoded._keras_shape[1:]# 在这里进行反平坦化decoder_input= Input(encoderOutputShape)decoder = Conv2D(32, (3, 3), activation='relu', padding='same')(decoder_input)x = UpSampling2D((2, 2))(decoder)x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)x = UpSampling2D((2, 2))(x)decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)decoder = Model(decoder_input, decoded)auto_input = Input(shape=(28,28,1))encoded = encoder(auto_input)decoded = decoder(encoded)auto_encoder = Model(auto_input, decoded)

如何正确地实现这一点?

换句话说,我希望获取编码器的输出(或使用随机数据),对其进行更改,然后输入到解码器中并获取解码结果。


回答:

这里有一个问题,如果你不使用任何Dense层,为什么要平坦化张量?

但你可以这样做:

encoder_output = Flatten()(encoded)decoder_input = Reshape((7, 7, 32))(encoder_output)decoder = Conv2D(32, (3, 3), activation='relu', padding='same')(decoder_input)

这是因为你需要在之前重塑你的张量。

Related Posts

如何按索引访问PyTorch模型参数

如果我的网络有10层,包括偏置项,如何仅通过索引访问第…

Python中多元逻辑回归显示错误

我试图使用逻辑回归进行预测,并使用Python和skl…

在MACOS上安装NLTK

我在我的2015款Mac Pro上尝试安装NLTK,操…

如何在R中将通过RFE选择的变量插入到机器学习模型中?

我想使用递归特征消除方法来选择最重要的特征,然后将这些…

CountVectorizer 错误:ValueError: setting an array element with a sequence

我有一个包含144条学生反馈的数据集,其中有72条正面…

### 在特定轮次后开始回调值准确性[start callback val acc after specific epoch]

我在使用验证准确性实现提前停止时有一个疑问。 假设我想…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注