为什么未连接到Keras模型中的预期层

时间:2018-12-05 05:26:32

标签: tensorflow keras

我想在Keras(2.2.4,带有TensorFlow后端)中构造一个变体自动编码器,这是我的代码:

dims = [1000, 256, 64, 32]
x_inputs = Input(shape=(dims[0],), name='inputs')
h = x_inputs

# internal layers in encoder
for i in range(n_stacks-1):
    h = Dense(dims[i + 1], activation='relu', kernel_initializer='glorot_uniform', name='encoder_%d' % i)(h)

# hidden layer
z_mean = Dense(dims[-1], kernel_initializer='glorot_uniform', name='z_mean')(h)
z_log_var = Dense(dims[-1], kernel_initializer='glorot_uniform', name='z_log_var')(h)

z = Lambda(sampling, output_shape=(dims[-1],), name='z')([z_mean, z_log_var])

encoder = Model(inputs=x_inputs, outputs=z, name='encoder')
encoder_z_mean = Model(inputs=x_inputs, outputs=z_mean, name='encoder_z_mean')


# internal layers in decoder
latent_inputs = Input(shape=(dims[-1],), name='latent_inputs')
h = latent_inputs
for i in range(n_stacks-1, 0, -1):
    h = Dense(dims[i], activation='relu', kernel_initializer='glorot_uniform', name='decoder_%d' % i)(h)

# output
outputs = Dense(dims[0], activation='relu', kernel_initializer='glorot_uniform' name='mean')

decoder = Model(inputs=latent_inputs, outputs=outputs, name='decoder')

ae_output = decoder(encoder_z_mean(x_inputs))
ae = Model(inputs=x_inputs, outputs=ae_output, name='ae')
ae.summary()

vae_output = decoder(encoder(x_inputs))
vae = Model(inputs=x_inputs, outputs=vae_output, name='vae')
vae.summary()

问题是我可以打印“ ae”和“ vae”模型的摘要,但是当我训练ae模型时,它说

tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'latent_inputs' with dtype float and shape [?,32]

在模型中,“解码器”应该连接到AE模型中“ encoder_z_mean”层的输出。但是,当我打印“ ae”模型的摘要时,“解码器”实际上连接到“ encoder_z_mean [1] [0]”。应该是“ encoder_z_mean [0] [0]”吗?

1 个答案:

答案 0 :(得分:1)

一些更正:

  • x_inputs 已经是编码器的输入,请勿再通过encoder_z_mean(x_inputs)encoder(x_inputs)对其进行调用
    • 除了创建第二个节点(您担心的第一个节点,这不是问题)之外,它可能是错误的根源,因为它不是额外的输入,而是相同的输入
    • 要正常使用此方法,需要创建一个新的Input(...)张量
  • 未在张量上调用最后一个Dense层。您可能希望在那里(h)

这样做:

# output - called h in the last layer
outputs = Dense(dims[0], activation='relu', kernel_initializer='glorot_uniform' name='mean')(h)

#unchanged
decoder = Model(inputs=latent_inputs, outputs=outputs, name='decoder')   

#adjusted inputs
ae_output = decoder(encoder_z_mean.output)
ae = Model(encoder_z_mean.input, ae_output, name='ae')
ae.summary()

vae_output = decoder(encoder.output)
vae = Model(encoder.input, vae_output, name='vae')
vae.summary()

解码器可能仍会出现[1][0],但这根本不是问题。这意味着解码器本身具有自己的输入节点(数字0),并且当您使用另一个模型的输出调用它时,您创建了一个额外的输入节点(数字1)。这是无害的。将使用节点1,而将忽略节点0。