【问题标题】:Keras 'plot_model' shows wrong graph for nested models (Autoencoder)Keras 'plot_model' 显示嵌套模型的错误图(自动编码器)
【发布时间】: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


    【解决方案1】:

    我能够通过向自动编码器添加两个输入层来防止第一个问题。

    第二个问题(多个输出只连接到一个输入)似乎是一个已知错误:https://github.com/tensorflow/tensorflow/issues/42101

    【讨论】:

      猜你喜欢
      • 2019-01-06
      • 2012-10-09
      • 2020-06-11
      • 2017-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-19
      相关资源
      最近更新 更多