Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tensorflow 为什么';t连接到Keras模型中的预期层_Tensorflow_Keras - Fatal编程技术网

Tensorflow 为什么';t连接到Keras模型中的预期层

Tensorflow 为什么';t连接到Keras模型中的预期层,tensorflow,keras,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', kern

我想在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模型中“编码器z_均值”层的输出。但当我打印“ae”模型的摘要时,“decoder”实际上连接到“encoder_z_mean[1][0]”。应该是“编码器z_表示[0][0]”吗?

一些更正:

  • x_输入
    已经是编码器的输入了,不要用
    encoder\u z_mean(x_输入)
    encoder(x_输入)
    • 除了创建第二个节点(您担心的是1,这不是问题),它可能是错误的来源,因为它不是额外的输入,而是相同的输入
    • 健康地使用它需要创建一个新的
      输入(…)
      张量来调用
  • 最后一个
    密集的
    层没有在张量上调用。您可能需要
    (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将被忽略。

谢谢您的回答。我按照你的建议改正了。现在解码器确实连接到了[0][0],但仍然会出现相同的错误:tensorflow.python.framework.errors\u impl.invalidargumeinterror:必须为带有数据类型float和shape[?,32]的占位符张量“潜在输入”提供一个值。看起来很奇怪。@Tian,你打算怎么训练?你的keras和tensorflow版本是什么?