keras
keras copied to clipboard
[Question] When is it absolutely necessary to use a `Lambda` layer?
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_inputwithoutLambda. - video_transformers like the previous one.
- ddim use
Lambda(maybe?) becausepreprocess_inputoccurs inside aSequentialmodel.
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.
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
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.
This issue was closed because it has been inactive for 28 days. Please reopen if you'd like to work on this further.