Issue
this is the code from the TensorFlow website, but doesn't explain well,
normalization_layer = tf.keras.layers.Rescaling(1./255)
train_data = train_data.map(lambda x, y: (normalization_layer(x), y)) # Where x—images, y—labels.
i know what is the goal of this code which is to normalize data and make it between 0 and 1 instead of 0 to 255, but I need to understand what does lambda means here.
Solution
The best way to explain I think is to see a simpler example:
list(map(lambda n: n * 3, [1, 2, 3, 4, 5]))
>> [3, 6, 9, 12, 15]
In this case you're applying to each (because of map
) element n
of the list [1, 2, 3, 4, 5]
the same operation n * 3
.
In your specific case it's a bit more complicated because you have two variables. What happens is that for each (x, y)
pair of your train_data
you are leaving y
as is, and applying the normalization to x
.
Update, more info on what is a lambda.
This site offers great examples and a good definition of lambda used inside a map
function, like in your case:
A lambda expression is a way of creating a little function inline, without all the syntax of a def. …
The code of the lambda is typically a single expression without variables or if-statements, and does not use "return". Lambda is perfect where you have a short computation to write inline.
Answered By - ClaudiaR
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.