【发布时间】:2021-01-22 18:48:30
【问题描述】:
当我创建具有多个输入和输出的自动编码器架构时,plot_model 图表未按预期显示(问题以红色突出显示)。
我假设出现第一个问题是因为我使用 encoder.inputs 作为自动编码器。但是,为自动编码器创建新的输入层会导致我出错(图已断开)。
这个问题很可能是在我这边,而不是 Keras 中的错误,所以希望有更多经验的人可以指导我朝着正确的方向前进。
ps。我不能只使用单个自动编码器模型,这样可以避免这个问题,因为在 GAN 设置中额外使用了相同的编码器和解码器模型。 (实现目前正在使用单输入和切片,但我真的很想切换到多输入,因为它感觉更干净)
from tensorflow.keras.layers import *
from tensorflow.keras.models import Model
from tensorflow.keras.utils import plot_model
# Config encoder
state_inputs = Input(shape=7, name="encoder_state_inputs")
action_inputs = Input(shape=4, name="encoder_action_inputs")
encoder_layer = Dense(11, activation=LeakyReLU(alpha=0.2), name=f"encoder_layer")
classification_layer = Dense(4, activation="softmax", name=f"classification")
latent_layer = Dense(5, activation=LeakyReLU(alpha=0.2), name="latent_space")
# Build encoder
x = concatenate([state_inputs, action_inputs])
x = encoder_layer(x)
classifier = classification_layer(x)
latent = latent_layer(x)
encoder = Model([state_inputs, action_inputs], [classifier, latent], name="encoder")
plot_model(encoder, "encoder.png")
# Config decoder
classifier_inputs = Input(shape=4, name="classifier_inputs")
latent_inputs = Input(shape=5, name="latent_inputs")
decoder_layer = Dense(11, activation=LeakyReLU(alpha=0.2), name=f"decoder_layer")
state_reconstruction_layer = Dense(7, activation="softmax", name=f"state_reconstruction")
action_reconstruction_layer = Dense(4, activation="softmax", name=f"action_reconstruction")
# Build decoder
x = concatenate([classifier_inputs, latent_inputs])
x = decoder_layer(x)
state_reconstruction = state_reconstruction_layer(x)
action_reconstruction = action_reconstruction_layer(x)
decoder = Model([classifier_inputs, latent_inputs], [state_reconstruction, action_reconstruction], name="decoder")
plot_model(decoder, "decoder.png")
# Build autoencoder
encoded = encoder(encoder.inputs)
decoded = decoder(encoded)
autoencoder = Model(encoder.inputs, outputs=[classifier, decoded], name="autoencoder")
plot_model(autoencoder, "autoencoder.png", show_shapes=True, expand_nested=True)
【问题讨论】:
标签: tensorflow keras tensorflow2.0 keras-layer autoencoder