繁体   English   中英

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

[英]ValueError: Output tensors to a Model must be the output of a TensorFlow Layer with tf.keras Lambda layer

我正在尝试将tf.keras.layers.Lambda函数的输出用作tf.keras模型中的最后一层,但是tf会将lambda层的输出解释为Tensor(与Layer相对)对象。

错误是:“ ValueError:模型的输出张量必须是TensorFlow Layer的输出(因此保留了过去的层元数据)。找到:Tensor(“ Discriminator / mullayer / mul:0”,shape =(2,2), D型= 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变量,只是一个浮点数,我怀疑这可能是问题的一部分。 如果是这样,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)))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM