[英]ValueError: Output tensors to a Model must be the output of a TensorFlow Layer with tf.keras Lambda layer
I'm trying to use the output of a tf.keras.layers.Lambda function as the last layer in a tf.keras model, but tf is interpreting the lambda layer's output as a Tensor(as opposed to a Layer) object. 我正在尝试将tf.keras.layers.Lambda函数的输出用作tf.keras模型中的最后一层,但是tf会将lambda层的输出解释为Tensor(与Layer相对)对象。
The error is: "ValueError: Output tensors to a Model must be the output of a TensorFlow Layer
(thus holding past layer metadata). Found: Tensor("Discriminator/mullayer/mul:0", shape=(2, 2), dtype=float32)" 错误是:“ ValueError:模型的输出张量必须是TensorFlow
Layer
的输出(因此保留了过去的层元数据)。找到:Tensor(“ Discriminator / mullayer / mul:0”,shape =(2,2), D型= FLOAT32)”
and the code is attached below 并且代码附在下面
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' is not a keras variable, just a float, which I suspect may be part of the problem. “ self.alphaVal”不是一个keras变量,只是一个浮点数,我怀疑这可能是问题的一部分。 If so, what is the equivalent of keras' backend K in tf.keras?
如果是这样,tf.keras中的keras后端K等于多少?
Thanks 谢谢
test
is not coming from anywhere considered a Keras layer. test
不是来自任何被认为是Keras层的地方。
If test
intended to be a model's input, it must be: 如果
test
打算作为模型的输入,则必须为:
test = Input(tensor=tf.constant([1.0], shape=(2,2))
#there may be some implications with shape, batch size and other stuff....
As a model with two inputs, you should remember to add it when defining the Model
. 作为具有两个输入的模型,您应该记住在定义
Model
时将其添加。
If you want a constant value to be used without it being an input, you must not pass it as the "input" of a layer. 如果要在不将其用作输入的情况下使用常量值,则不得将其作为图层的“输入”传递。 You just refer to it from inside the layer, or you create it inside the layer.
您只需从图层内部引用它,或者在图层内部创建它。
If you just want to test your Lambda layer: 如果您只想测试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.