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

标签 python keras

我想获取输入图像 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/

相关文章:

python - 检查目标 : expected dense to have 3 dimensions, 时出错,但得到形状为 (32, 200) 的数组

python - 如何使用 keras-self-attention 包可视化注意力 LSTM?

python - 部署模型时在azure ml入口脚本中导入数据和python脚本

python - 求封闭二维均匀三次 B 样条的面积

python - 比较 n 维 numpy 数组中的值

python - ValueError:层激活_1调用的输入不是符号张量

opencv - Keras 模型到 Coreml 并使用 OpenCV

python - 在 Python 中将 0 和 1 的字符串转换为字节

Python 在特定目录模式中搜索文件名模式

python - 用于温度时间序列预测的 LSTM 神经网络