获得Keras Mod的一部分

2024-06-26 13:24:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个AutoEncoder的模型,如下所示:

height, width = 28, 28

input_img = Input(shape=(height * width,))
encoding_dim = height * width//256

x = Dense(height * width, activation='relu')(input_img)

encoded1 = Dense(height * width//2, activation='relu')(x)
encoded2 = Dense(height * width//8, activation='relu')(encoded1)

y = Dense(encoding_dim, activation='relu')(encoded2)

decoded2 = Dense(height * width//8, activation='relu')(y)
decoded1 = Dense(height * width//2, activation='relu')(decoded2)

z = Dense(height * width, activation='sigmoid')(decoded1)
autoencoder = Model(input_img, z)

#encoder is the model of the autoencoder slice in the middle 
encoder = Model(input_img, y)
decoder = Model(y, z)
print(encoder)
print(decoder)

编码器部分使用上述代码进行检索,但是我无法使用上面添加的代码获取解码器部分:

我收到以下错误:

^{pr2}$

你能告诉我怎么得到那部分吗?在


Tags: theencoderimginputmodelwidthactivationencoding
1条回答
网友
1楼 · 发布于 2024-06-26 13:24:51

decoder模型需要有一个输入层。例如,这里的decoder_input

height, width = 28, 28

# Encoder layers
input_img = Input(shape=(height * width,))
encoding_dim = height * width//256

x = Dense(height * width, activation='relu')(input_img)
encoded1 = Dense(height * width//2, activation='relu')(x)
encoded2 = Dense(height * width//8, activation='relu')(encoded1)
y = Dense(encoding_dim, activation='relu')(encoded2)

# Decoder layers
decoder_input = Input(shape=(encoding_dim,))
decoded2 = Dense(height * width//8, activation='relu')(decoder_input)
decoded1 = Dense(height * width//2, activation='relu')(decoded2)
z = Dense(height * width, activation='sigmoid')(decoded1)

# Build the encoder and decoder models
encoder = Model(input_img, y)
decoder = Model(decoder_input, z)

# Finally, glue encoder and decoder together by feeding the encoder 
# output to the decoder
autoencoder = Model(input_img, decoder(y))

相关问题 更多 >