ValueError: 模型的输出张量必须是TensorFlow `Layer`的输出

我在Keras中构建模型时,在最后一层使用了一些TensorFlow函数(reduce_sum和l2_normalize),但遇到了这个问题。我已经搜索了解决方案,但所有相关内容都涉及到“keras张量”。

这是我的代码:

import tensorflow as tf;from tensorflow.python.keras import backend as Kvgg16_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 Layer的输出(因此保留过去的层元数据)。发现:Tensor(“l2_normalize_3:0”, shape=(?, 3), dtype=float32)

我注意到,如果不将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);

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注