Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
miticollo opened this issue Jun 3, 2024 · 4 comments
Closed

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

miticollo opened this issue Jun 3, 2024 · 4 comments
Assignees
Labels
stale stat:awaiting response from contributor type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited.

Comments

@miticollo
Copy link

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.

@sachinprasadhs
Copy link
Collaborator

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 sachinprasadhs added type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited. stat:awaiting response from contributor labels Jun 4, 2024
Copy link

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 github-actions bot added the stale label Jun 19, 2024
Copy link

github-actions bot commented Jul 4, 2024

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 github-actions bot closed this as completed Jul 4, 2024
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale stat:awaiting response from contributor type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited.
Projects
None yet
Development

No branches or pull requests

2 participants