【问题标题】:ValueError: Output tensors to a Model must be the output of a TensorFlow Layer with tf.keras Lambda layerValueError:模型的输出张量必须是具有 tf.keras Lambda 层的 TensorFlow 层的输出
【发布时间】:2018-07-24 20:03:22
【问题描述】:

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

错误是:“ValueError: Output tensors to a Model must be the output of a TensorFlow Layer (因此保存过去的层元数据)。找到:Tensor("Discriminator/mullayer/mul:0", shape=( 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 变量,只是一个浮点数,我怀疑这可能是问题的一部分。如果是,那么 keras 的后端 K 在 tf.keras 中的等价物是什么?

谢谢

【问题讨论】:

    标签: python tensorflow keras


    【解决方案1】:

    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)))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-15
      • 1970-01-01
      • 2017-09-23
      • 1970-01-01
      • 2021-12-14
      • 2019-10-13
      • 1970-01-01
      • 2019-12-24
      相关资源
      最近更新 更多