首页 > 解决方案 > 获取特定层的输出作为测试数据的结果,而不是 keras 中的最后一层(自动编码器潜在特征)

问题描述

我正在尝试获取 的输出latent layer/hidden layer以将其用作其他内容的输入。我以一种有效的方式训练我的模型以最大限度地减少损失,因此我的模型可以有效地学习潜在特征并尽可能接近图像。我的模型是

input_img = Input(shape=(28, 28, 1))  # adapt this if using `channels_first` image data format

#Encoder
x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)

encoded = MaxPooling2D((2, 2), padding='same')(x) 


# Decoder
x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded) 
x = UpSampling2D((2, 2))(x) # opposite of Pooling
x = Conv2D(16, (3, 3), activation='relu')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)

autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

我想要encoded层的输出作为模型的输出。可能吗?广告如果是,请告诉我如何。

标签: tensorflowmachine-learningkerasdeep-learningautoencoder

解决方案


你可以简单地这样做

autoencoder.fit(...)

latent_model = Model(input_img, encoded)
latent_representation = latent_model.predict(X)

推荐阅读