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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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