简体   繁体   中英

I keep getting the error TypeError: 'Tensor' object is not callable

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()

I keep getting TypeError: 'Tensor' object is not callable at the line outputs = prediction_layer(x) . Any clue what I may be doing wrong?

Edit: Added a few more lines to make it clear what I am doing

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()

prediction_layer , as mentioned on line 5, would be the output of the Dense layer, and hence be just a Tensor and not a layer.
You do not require the flatten layer on base_model.layers[-1].output , and can just do it on x .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM