gpt4 book ai didi

python - ValueError : Output tensors to a Model must be the output of a TensorFlow Layer with tf. keras Lambda层

转载 作者:行者123 更新时间:2023-12-01 01:44:42 25 4
gpt4 key购买 nike

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

错误是:“ValueError:模型的输出张量必须是 TensorFlow Layer 的输出(因此保存过去的层元数据)。发现:Tensor(“Discriminator/mullayer/mul:0 “,形状=(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....

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

如果您希望使用常量值而不将其作为输入,则必须将其作为图层的“输入”传递。您只需从图层内部引用它,或者在图层内部创建它。

<小时/>

如果您只想测试 Lambda 层:

inp = Input((2,2))
out = mullayer(inp)
testModel = Model(inp,out)

testModel.predict(np.ones((1,2,2)))

关于python - ValueError : Output tensors to a Model must be the output of a TensorFlow Layer with tf. keras Lambda层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51506691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com