numpy multiinput GAN返回错误ValueError:图形断开连接:

yhived7q  于 2023-05-29  发布在  其他
关注(0)|答案(1)|浏览(117)

我有一个模型,它将采用普通数组并在GAN中处理它,它工作了,但一旦我将其更改为多输入,我开始得到:

ValueError: Graph disconnected:

我的原始代码:

# Build stacked GAN model
gan_input = Input(shape=Xtrain.shape[1])
H = generator(gan_input)
gd_input=Concatenate()([gan_input,H])
gan_V = discriminator(gd_input)
GAN = Model(gan_input, [gan_V,H])
GAN.compile(loss=['categorical_crossentropy','mse'], optimizer=opt) #Complete GAN have both loss functions
GAN.summary()

然后我修改了它的多输入:

gan_dataframe_input = Input(shape=Xtrain[1][:-2].shape) #new testing
numpy_input = Input(shape=Xtrain[1][-1].shape)

gan_input = layers.concatenate([gan_dataframe_input, numpy_input])

print(gan_input)
print(mergedLayer)

H = generator([gan_dataframe_input,numpy_input]) <<--two shapes being imputed
gd_input=Concatenate()([gan_input,H])   <<--merged layer + above two shapes being imputed
gan_V = discriminator(gd_input) 
GAN = Model(gan_input, [gan_V,H])  <<--this line returns an error
GAN.compile(loss=['categorical_crossentropy','mse'], optimizer=opt) #Complete GAN have both loss functions
GAN.summary()

堆栈跟踪:

KerasTensor(type_spec=TensorSpec(shape=(None, 736), dtype=tf.float32, name=None), name='concatenate_28/concat:0', description="created by layer 'concatenate_28'")
KerasTensor(type_spec=TensorSpec(shape=(None, 736), dtype=tf.float32, name=None), name='concatenate_27/concat:0', description="created by layer 'concatenate_27'")
WARNING:tensorflow:Functional model inputs must come from `tf.keras.Input` (thus holding past layer metadata), they cannot be the output of a previous non-Input layer. Here, a tensor specified as input to "model_34" was not an Input tensor, it was generated by layer concatenate_28.
Note that input tensors are instantiated via `tensor = tf.keras.Input(shape)`.
The tensor that caused the issue was: concatenate_28/concat:0
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-94-ac83091846e6> in <module>()
     69 gd_input=Concatenate()([gan_input,H])
     70 gan_V = discriminator(gd_input)
---> 71 GAN = Model(gan_input, [gan_V,H])
     72 GAN.compile(loss=['categorical_crossentropy','mse'], optimizer=opt) #Complete GAN have both loss functions
     73 GAN.summary()

4 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/functional.py in _map_graph_network(inputs, outputs)
    988                              'The following previous layers '
    989                              'were accessed without issue: ' +
--> 990                              str(layers_with_complete_input))
    991         for x in nest.flatten(node.outputs):
    992           computable_tensors.add(id(x))

ValueError: Graph disconnected: cannot obtain value for tensor KerasTensor(type_spec=TensorSpec(shape=(None, 659), dtype=tf.float32, name='input_71'), name='input_71', description="created by layer 'input_71'") at layer "concatenate_28". The following previous layers were accessed without issue: []

奇怪的是,看完整的轨道跟踪,在我打印数据层后,似乎数组中的项目数量没有对齐?(659,)是其中一个输入的大小,而另一个是(77,)。我做错了什么?

laik7k3q

laik7k3q1#

当您构建多输入/多输出模型时,您必须将模型的输入和输出作为数组进行编译和馈送,而不是像您那样将它们连接起来。此外,模型的输入必须始终为tf.keras.layers.Input。所以正确的代码应该是

gan_dataframe_input = Input(shape=Xtrain[1][:-2].shape) #new testing
numpy_input = Input(shape=Xtrain[1][-1].shape)

gan_input = layers.concatenate([gan_dataframe_input, numpy_input])

print(gan_input)
print(mergedLayer)

H = generator([gan_dataframe_input,numpy_input]) <<--two shapes being imputed
gd_input=Concatenate()([gan_input,H])   <<--merged layer + above two shapes being imputed
gan_V = discriminator(gd_input) 
GAN = Model([gan_dataframe_input, numpy_input ], [gan_V,H])  <<--this line is modified
GAN.compile(loss=['categorical_crossentropy','mse'], optimizer=opt) #Complete GAN have both loss functions
GAN.summary()

相关问题