作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试使用 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/
我是一名优秀的程序员,十分优秀!