keras-team / tf-keras

The TensorFlow-specific implementation of the Keras API, which was the default Keras from 2019 to 2023.
Apache License 2.0
63 stars 28 forks source link

Identity layer in Keras? #534

Closed AdityaKane2001 closed 1 year ago

AdityaKane2001 commented 2 years ago

@fchollet @qlzh727

I was trying to resolve #425 of keras-cv by simply adding a single Identity layer in the layers list in case it was empty or None.

However, there is no Identity layer in Keras and thus I had to do it using the Lambda layer. Thus, I thought it would be good to have an Identity layer in Keras as there is one in torch (nn.Identity). We can support a subset of the same (leaving out the argument insensitive part, since I think it will be a nuisance for serialization).

I will be happy to code it up if you give the go-ahead.

qlzh727 commented 2 years ago

Usually single op layer can use Lambda layer or will be auto wrapped by a TFOpLayer in functional model.

Layer usually should contain a block of predefined logic and I don't think we want to add any layer that only contains single op.

AdityaKane2001 commented 2 years ago

@qlzh727

I see. However, I think there are valid use cases of an Identity layer. For example, keras_cv.layers.RandomChoice can contain keras.layers.Identity as one of the possible augmentations. I believe it can also be used as a placeholder in many cases.

More importantly, I believe it helps with the object-oriented style of code writing. Thus I think this would be a worthy addition.

/cc @fchollet

LukeWood commented 2 years ago

@AdityaKane2001 feel free to send a PR for this!

fchollet commented 1 year ago

The layer has been added.

AdityaKane2001 commented 1 year ago

Thanks a lot for the follow up @fchollet