Python 我不断得到错误类型error:';张量';对象不可调用

Python 我不断得到错误类型error:';张量';对象不可调用,python,tensorflow,keras,Python,Tensorflow,Keras,我不断得到TypeError:“Tensor”对象不能在outputs=prediction\u layer(x)行调用。知道我做错了什么吗 编辑: 增加了几行文字,以明确我在做什么 global_average_layer = keras.layers.GlobalAveragePooling2D() feature_batch_average = global_average_layer(feature_batch) print(feature_batch_average.shape) fl

我不断得到TypeError:“Tensor”对象不能在
outputs=prediction\u layer(x)
行调用。知道我做错了什么吗

编辑: 增加了几行文字,以明确我在做什么

global_average_layer = keras.layers.GlobalAveragePooling2D()
feature_batch_average = global_average_layer(feature_batch)
print(feature_batch_average.shape)
flatten = keras.layers.Flatten()(base_model.layers[-1].output)
dense1 = keras.layers.Dense(256, activation = "relu")(flatten)
prediction_layer = keras.layers.Dense(3, activation = "softmax")(dense1)
x = base_model(inputs, training=False)
x = global_average_layer(x)
x = keras.layers.Dropout(0.5)(x)
outputs = prediction_layer(x)
model = keras.Model(inputs, outputs)
model.summary()

如第5行所述,
预测层
将是
密集层
的输出,因此只是一个
张量
,而不是一个层。

您不需要在
base\u model.layers[-1]。output
上使用展平层,只需在
x

上使用展平层,这可能与我试图合并到模型中的第6-8行的
x
发生的情况重复。我怎样才能把它放进去。因为如果我使用
prediction\u层
,就像你所说的
密集
层的输出,那么这些永远不会被纳入我的model@user966615我不理解模型的结构,你能澄清一下吗?所以我尝试使用DenseNet121预训练模型对一组图像进行分类。所以上面的代码是我试图用迁移学习来实现的。如果您查看编辑,从带有
base\u model
的行到
global\u average\u layer
,我只是建立我想要使用的层。从
展平
行到
输出
我只是在堆叠或创建我计划用于图像分类的模型。我相信你在展平层出错了。在获得输出张量之前,您应该初始化
x
x
,然后
展平
,然后
密度1
,然后
预测层
,然后将预测层用作模型中的输出?
base_model = keras.applications.DenseNet121(input_shape=IMG_SHAPE,
                                               include_top=False,
                                               weights='imagenet')

image_batch, label_batch = next(iter(train_dataset))
feature_batch = base_model(image_batch)
global_average_layer = keras.layers.GlobalAveragePooling2D()
feature_batch_average = global_average_layer(feature_batch)
print(feature_batch_average.shape)
flatten = keras.layers.Flatten()(base_model.layers[-1].output)
dense1 = keras.layers.Dense(256, activation = "relu")(flatten)
prediction_layer = keras.layers.Dense(3, activation = "softmax")(dense1)
x = base_model(inputs, training=False)
x = global_average_layer(x)
x = keras.layers.Dropout(0.5)(x)
outputs = prediction_layer(x)
model = keras.Model(inputs, outputs)
model.summary()