gpt4 book ai didi

python - 类型错误 : Output tensors to a Model must be Keras tensors

转载 作者:太空狗 更新时间:2023-10-30 01:12:32 24 4
gpt4 key购买 nike

我想获取输入图像 img(它也有负值)并将其馈送到两个激活层。但是,我想做一个简单的转换,例如将整个图像乘以 -1.0:

left = Activation('relu')(img)
right = Activation('relu')(tf.mul(img, -1.0))

如果我这样做,我会得到:

TypeError: Output tensors to a Model must be Keras tensors. Found: Tensor("add_1:0", shape=(?, 5, 1, 3), dtype=float32)

我不确定该如何解决。是否有 Keras 方面的 mul() 方法可以用于此类事情?或者我能否以某种方式包装 tf.mul(img, -1.0) 的结果,以便我可以将其传递给 Activation

请注意:负值可能很重要。从而转换图像 st。最小值只是 0.0 不是这里的解决方案。


同样的错误

left = Activation('relu')(conv)
right = Activation('relu')(-conv)

同样的错误:

import tensorflow as tf

minus_one = tf.constant([-1.])

# ...

right = merge([conv, minus_one], mode='mul')

最佳答案

创建 Lambda 层来包装您的函数是否有效?

参见文档 here

from keras.layers import Lambda
import tensorflow as tf

def mul_minus_one(x):
return tf.mul(x,-1.0)
def mul_minus_one_output_shape(input_shape):
return input_shape

myCustomLayer = Lambda(mul_minus_one, output_shape=mul_minus_one_output_shape)
right = myCustomLayer(img)
right = Activation('relu')(right)

关于python - 类型错误 : Output tensors to a Model must be Keras tensors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42297359/

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