keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
61.99k stars 19.48k forks source link

Add layer trainable information in model summary and plot model #15425

Closed arisliang closed 1 year ago

arisliang commented 3 years ago

System information.

TensorFlow version (you are using): 2.6 Are you willing to contribute it (Yes/No) : No

Describe the feature and the current behavior/state.

For GAN style models, whether the parameter is trainable correctly is important to model training correctness. Therefore it would be useful to be able to include that information in the summary and model plots, if possible.

Will this change the current api? How? Should not change model.summary function. May add a show_trainable flag for plot_model function.

Who will benefit from this feature? Users who develop GAN style models, or when layer's trainable parameter is important for the model correctness.

fchollet commented 3 years ago

Thanks for the suggestion. We think this could be a useful feature, so please feel free to open a PR to add an optional trainable column to the summary (let's make it as narrow as possible since it's just a boolean flag).

foxik commented 2 years ago

This could be closed as a patch implementing it was already merged.

jvishnuvardhan commented 2 years ago

Did this patch get merged? I cannot see the show_trainable argument in keras master https://github.com/mfidabel/keras/blob/master/keras/utils/layer_utils.py

I tried using show_trainable in a simple example from TF tutorial (with tf-nightly), and it throws an error as shown below. Please check the [gist]() for a reference.

TypeError: plot_model(https://colab.research.google.com/gist/jvishnuvardhan/59688cb7063772ff9e8a3827ac946044/untitled1174.ipynb) got an unexpected keyword argument 'show_trainable'

tilakrayal commented 2 years ago

@arisliang, The related PR patch gets merged and I can see show_trainable argument in keras master. https://github.com/mfidabel/keras/blob/master/keras/utils/layer_utils.py

image

Could you please take a look at the layer_utils.py file and let us know if you are facing a similar issue. Thank you!

mfidabel commented 2 years ago

@arisliang, The related PR patch gets merged and I can see show_trainable argument in keras master. https://github.com/mfidabel/keras/blob/master/keras/utils/layer_utils.py

image

Could you please take a look at the layer_utils.py file and let us know if you are facing a similar issue. Thank you!

I think he is trying to print which layers are trainable on the plot_model() visualization tool instead of the model.summary() function. AFAIK, this hasn't been implemented yet.

Captura de Pantalla 2022-10-12 a la(s) 18 48 25
mfidabel commented 2 years ago

Added show_trainable to plot_model. It should look something like this:

model_trainable

Where 'T' means the layer is trainable and 'NT' means the layer isn't trainable

tilakrayal commented 1 year ago

@arisliang & @mfidabel, The related PR #15459 patch gets merged and I can see show_trainable argument is available in keras master. https://github.com/mfidabel/keras/blob/master/keras/utils/layer_utils.py image

And the another PR #17145 was also merged where model.summary(show_trainable=True) was implemented and plot_model(model, show_trainable=True). I tried with the sample code on tf-nightly and was able to fetch the required output. Kindly find the gist of it here.

import tensorflow as tf

from tensorflow import keras

model = tf.keras.Sequential(
    [
        tf.keras.Input(shape=(230, 230, 3)),
        tf.keras.layers.Conv2D(3, 1),
        tf.keras.layers.MaxPooling2D(2),
        tf.keras.layers.Dense(10),
    ]
)

model.get_layer(index=0).trainable = False

tf.keras.utils.plot_model(
    model,
    to_file="model_trainable.png",
    show_trainable=True,
)

image

mfidabel commented 1 year ago

@tilakrayal Yes, It is already merged. I think you can close the issue.

google-ml-butler[bot] commented 1 year ago

This issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Thank you.

google-ml-butler[bot] commented 1 year ago

Closing as stale. Please reopen if you'd like to work on this further.