简体   繁体   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

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.

相关问题 Tensorflow - ValueError:模型的输出张量必须是 TensorFlow `Layer` 的输出 - Tensorflow - ValueError: Output tensors to a Model must be the output of a TensorFlow `Layer` ValueError:模型的输出张量必须是TensorFlow`Layer`的输出 - ValueError: Output tensors to a Model must be the output of a TensorFlow `Layer` Keras输出张量到模型必须是TensorFlow`Telay`的输出 - Keras Output tensors to a Model must be the output of a TensorFlow `Layer` ValueError: Output tensors of a Functional model must be the output of a TensorFlow `Layer` (thus holding past layer metadata) - ValueError: Output tensors of a Functional model must be the output of a TensorFlow `Layer` (thus holding past layer metadata) TensorFlow 自动编码器尝试:ValueError:功能模型的输出张量必须是 TensorFlow `Layer` 的输出 - Tensorflow autoencoder attempt: ValueError: Output tensors of a Functional model must be the output of a TensorFlow `Layer` 如何解决 ValueError:模型的输出张量必须是 Keras `Layer` 的输出(因此保存过去的层元数据)。? - How can I resolve ValueError: Output tensors to a Model must be the output of a Keras `Layer` (thus holding past layer metadata).? 模型的Keras输出张量必须是Keras层的输出(因此保留了过去的层元数据) - Keras Output tensors to a Model must be the output of a Keras `Layer` (thus holding past layer metadata) 模型的输出张量必须是模型Api Tensorfow中的TensorFlow`Layer`(因此保留了过去的层元数据)的输出 - Output tensors to a Model must be the output of a TensorFlow `Layer` (thus holding past layer metadata) in Model Api Tensorfow 如何为 tf.keras 模型的隐藏层选择输出神经元的值? - How can I choose the value of output neurons for the hidden layer of tf.keras model? 模型的输出张量必须是 Keras `Layer` 的输出(因此保存过去的层元数据)。 CNN LSTM 使用函数式 api 时 - Output tensors to a Model must be the output of a Keras `Layer` (thus holding past layer metadata). When using functional api for CNN LSTM
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM