简体   繁体   中英

TensorFlow 2 AttributeError: 'tuple' object has no attribute 'layer'

I was following a tutorial to use Keras pre-trained models but I am getting this error:

AttributeError: 'tuple' object has no attribute 'layer'

I am running my code on Google Colab, so I tried to downgrade TensorFlow to version 1.x, but the issue was not solved. So how can I solve this?

Here is my code fragment:

resnet = ResNet152V2(
             include_top=False,
             weights="imagenet",
             input_shape=(40,40,3)
)

for layer in resnet.layers:
    layer.trainable = False

x = resnet.output
x = Flatten()(x)
prediction = Dense(4, activation='softmax')(x)

model = Model(inputs=resnet.input, outputs=prediction)

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

What dependencies did you import?

Had the same problem just now, and I fixed it by making sure that my dependencies were consistent. Take note that tensorflow.keras is different from importing from keras.

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