keras icon indicating copy to clipboard operation
keras copied to clipboard

[Question] When is it absolutely necessary to use a `Lambda` layer?

Open miticollo opened this issue 1 year ago • 2 comments

Reading Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow by Aurélien Géron I noticed that to use a preprocess function (like preprocess_input) it is necessary to put it inside a Lambda layer if you build a Sequential model otherwise an error is raised.

Now, the question is this requirement is required also for Functional model?

It seems no. Digging into Keras' repos I found three examples:

  • DeeplabV3Plus that uses preprocess_input without Lambda.
  • video_transformers like the previous one.
  • ddim use Lambda (maybe?) because preprocess_input occurs inside a Sequential model.

Furthermore, I discovered that Functional API can also include raw Keras 3 ops.

If possible, I would want to add to my Functional model tf.expand_dims, but in this case I suppose(?) that I must use a Lambda layer. To be sure I could always use a Lambda layer (or even better a custom one), but I would want to understand when a Lambda layer is strictly necessary.

miticollo avatar Jun 03 '24 04:06 miticollo

Lambda layer allows you to apply custom function to the input data, ex: preprocess_input, One of the example from the sequential model is https://keras.io/examples/generative/ddim/#kernel-inception-distance

Lambda can be used in both Functional and Sequential models. For more details on Lambda you can refer https://keras.io/api/layers/core_layers/lambda/

You can also use preprocess_input in the custom function like below.

def preprocess(image, label):
      resized_image = tf.image.resize(image, [224, 224])
      final_image = keras.applications.xception.preprocess_input(resized_image)
      return final_image, label

sachinprasadhs avatar Jun 04 '24 07:06 sachinprasadhs

This issue is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you.

github-actions[bot] avatar Jun 19 '24 01:06 github-actions[bot]

This issue was closed because it has been inactive for 28 days. Please reopen if you'd like to work on this further.

github-actions[bot] avatar Jul 04 '24 01:07 github-actions[bot]

Are you satisfied with the resolution of your issue? Yes No

google-ml-butler[bot] avatar Jul 04 '24 01:07 google-ml-butler[bot]