ValueError:模型的输出张量必须是具有tf.keras-Lambda层的TensorFlow层的输出



我试图使用tf.keras.layers.Lambda函数的输出作为tf.keras模型中的最后一层,但tf将Lambda层的输出解释为张量(而不是层(对象。

错误为:"ValueError:模型的输出张量必须是TensorFlowLayer的输出(因此保存过去的层元数据(。找到:张量("鉴别器/mulayer/mul:0",shape=(2,2(,dtype=float32(">

代码附在下面

from tensorflow.contrib.keras import layers, models
#lots of stuff up here, all working fine...
logits = layers.Dense(1, name=name+'fc')(x)# x works fine
mullayer = layers.Lambda(lambda x: x * self.alphaVal,name = "mullayer")
test = tf.constant([1.0],shape = (2,2))
testOut = mullayer(test)
outputs = [logits, testOut]
self.disc = models.Model(inputs=inp, outputs=outputs)

self.alphaVal不是keras变量,只是一个float,我怀疑这可能是问题的一部分。如果是,那么在tf.keras中,keras后端K的等价物是什么?

感谢

test不是来自任何被认为是Keras层的地方。

如果test要作为模型的输入,它必须是:

test = Input(tensor=tf.constant([1.0], shape=(2,2))
#there may be some implications with shape, batch size and other stuff....

作为一个有两个输入的模型,您应该记得在定义Model时添加它。

如果希望在不作为输入的情况下使用常数值,则必须而不是将其作为层的"输入"传递。您只需从层内部引用它,或者在层内部创建它。


如果你只想测试你的Lambda层:

inp = Input((2,2))
out = mullayer(inp)
testModel = Model(inp,out)
testModel.predict(np.ones((1,2,2)))

最新更新