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()
我不断收到 TypeError: 'Tensor' object is not callable in the line outputs = prediction_layer(x)
。任何线索我可能做错了什么?
编辑: 添加了更多行以明确我在做什么
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()
答案 0 :(得分:1)
prediction_layer
,如第 5 行所述,将是 Dense
层的输出,因此只是一个 Tensor
而不是层。
您不需要 base_model.layers[-1].output
上的展平层,只需在 x
上进行即可。