TypeError:模型的输出张量必须是Keras张量 [英] TypeError: Output tensors to a Model must be Keras tensors
问题描述
我想拍摄一个输入图像img
(也具有负值)并将其输入两个激活层.但是,我想做一个简单的转换,例如将整个图像乘以-1.0
:
I want to take an input image img
(which also has negative values) and feed it into two activation layers. However, I want to make a simple transformation e.g. multiply the whole image with -1.0
:
left = Activation('relu')(img)
right = Activation('relu')(tf.mul(img, -1.0))
如果我这样做,我会得到:
If I do it this way I am getting:
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
?
and I am not sure how I can fix that. Is there a Keras
side mul()
method that I can use for such a thing? Or can I wrap the result of tf.mul(img, -1.0)
somehow such that I can pass it on to Activation
?
请注意:负值可能很重要.从而变换图像最小只是0.0
在这里不是解决方案.
Please note: The negative values may be important. Thus transforming the image s.t. the minimum is simply 0.0
is not a solution here.
我也遇到相同的错误
left = Activation('relu')(conv)
right = Activation('relu')(-conv)
相同的错误:
The same error for:
import tensorflow as tf
minus_one = tf.constant([-1.])
# ...
right = merge([conv, minus_one], mode='mul')
推荐答案
是否创建Lambda层来包装功能?
Does creating a Lambda Layer to wrap your function work?
请参见文档此处
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)
这篇关于TypeError:模型的输出张量必须是Keras张量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!