Home > Blockchain >  normalize trained data with tensorflow
normalize trained data with tensorflow

Time:08-16

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.

CodePudding user response:

I'm assuming train_data is a tf.dataset

Each element inside a tf.dataset is stored in the form of a tuple of X and y values like this (X,y).

To access say, each element in a tuple in python, you can do

for X,y in tuple_list:
    print(X)
    print(y)

This is essentially what map with lambda does in the tf.dataset

This is what happens inside the map function

  • x-images of train data is assigned x
  • y-labels of train data is assigned y
  • You create a new tuple where x becomes normalization_layer(x)and y stays y
  • This is done for every example in train_data
  • This is fed back and stored in train_data (Since you are overwriting the variable)

See here for more information on how the Lambda function generally works in Python. Would specifically draw attention to general lambda syntax

lambda args: expression

CodePudding user response:

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.

  • Related