Python ValueError:模型的输出张量必须是TensorFlow`层的输出`
在遇到这个问题时,我正在Keras中使用最后一层中的一些张量流函数(reduce_sum和l2_normalize)构建一个模型。我一直在寻找一个解决方案,但所有这些都与“Keras张量”有关 这是我的密码:Python ValueError:模型的输出张量必须是TensorFlow`层的输出`,python,tensorflow,machine-learning,keras,tensor,Python,Tensorflow,Machine Learning,Keras,Tensor,在遇到这个问题时,我正在Keras中使用最后一层中的一些张量流函数(reduce_sum和l2_normalize)构建一个模型。我一直在寻找一个解决方案,但所有这些都与“Keras张量”有关 这是我的密码: import tensorflow as tf; from tensorflow.python.keras import backend as K vgg16_model = VGG16(weights = 'imagenet', include_top = False, input_s
import tensorflow as tf;
from tensorflow.python.keras import backend as K
vgg16_model = VGG16(weights = 'imagenet', include_top = False, input_shape = input_shape);
fire8 = extract_layer_from_model(vgg16_model, layer_name = 'block4_pool');
pool8 = MaxPooling2D((3,3), strides = (2,2), name = 'pool8')(fire8.output);
fc1 = Conv2D(64, (6,6), strides= (1, 1), padding = 'same', name = 'fc1')(pool8);
fc1 = Dropout(rate = 0.5)(fc1);
fc2 = Conv2D(3, (1, 1), strides = (1, 1), padding = 'same', name = 'fc2')(fc1);
fc2 = Activation('relu')(fc2);
fc2 = Conv2D(3, (15, 15), padding = 'valid', name = 'fc_pooling')(fc2);
fc2_norm = K.l2_normalize(fc2, axis = 3);
est = tf.reduce_sum(fc2_norm, axis = (1, 2));
est = K.l2_normalize(est);
FC_model = Model(inputs = vgg16_model.input, outputs = est);
然后是错误:
ValueError:模型的输出张量必须是
TensorFlow层
(因此保存过去的层元数据)。发现:
张量(“l2_规范化_3:0”,形状=(?,3),数据类型=浮点32)
我注意到,在不将fc2层传递给这些函数的情况下,该模型运行良好:
FC_model = Model(inputs = vgg16_model.input, outputs = fc2);
有人能给我解释一下这个问题和一些解决问题的建议吗?我找到了解决这个问题的方法。 对于遇到相同问题的任何人,都可以使用Lambda层包装tensorflow操作,我就是这么做的:
from tensorflow.python.keras.layers import Lambda;
def norm(fc2):
fc2_norm = K.l2_normalize(fc2, axis = 3);
illum_est = tf.reduce_sum(fc2_norm, axis = (1, 2));
illum_est = K.l2_normalize(illum_est);
return illum_est;
illum_est = Lambda(norm)(fc2);
我有这个问题是因为我在模型中的某个地方添加了两个张量作为
x1+x2
,而不是使用Add()([x1,x2])
这就解决了问题。在错误地使用
连接层时遇到了相同的问题:它是连接()([])
。如果你忘记了前面的()
,你就会得到错误;不是添加,而是切片。