I want to pop the last layer of the model. So I use the tf.keras.layers.pop()
, but it doesn't work.
base_model.summary()
base_model.layers.pop()
base_model.summary()
When I use tf.keras._layers.pop()
, it works.
base_model.summary()
base_model._layers.pop()
base_model.summary()
I don't find docs about this usage. Could someone help explain this?
I agree this is confusing. The reason is that model.layers
returns a shallow copy of the layers list so:
The tldr is dont use model.layers.pop()
to remove the last layer. Instead we should create a new model with all but the last layer. Perhaps something like this:
new_model = tf.keras.models.Sequential(base_model.layers[:-1])
Checkout this github issue for more details
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With