Mapping a tensorflow dataset with a function object

I am having difficulties to understand why the following code doesn’t work and how to make it correct

import tensorflow as tf

ds =[1,2,3,4,5],[6,7,8,9,10]))

class f:
    def __call__(self, couple):
        x,y = couple
        return x, y

fn = f()

out =

I get the error

TypeError: outer_factory.<locals>.inner_factory.<locals>.tf____call__() takes 2 positional arguments but 3 were given

Indeed, the following works


so I don’t understand why the first code wouldn’t work. Thank you for the help!

Hi @edamondo, when you print the dataset using as_numpy_iterator( ) it looks like

[(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)]

when you apply map function, the function applies transformation to each element of this dataset

so when you apply the map function of the above dataset, for 1st iteration the values 1,6 are passed separately but in the above function you have defined only 1 argument for the 2 values. so the error was triggered.

You can modify the function as below to overcome the error

class f:
    def __call__(self, x, y):
        return x, y

Thank You.

1 Like

Hi Kiran,

Thank you for the answer. It works now. However, I don’t understand it conceptually. Indeed the first element of the dataset is the couple (1,6), the second element of the dataset is the couple (2,7) and so on, so it should be the couples that are inputted to the function and not the separated values right?

Hi @edamondo, If the first element has a series of elements each value is read individually. For example if you have 3 values like (1,2,3) you have to pass 3 variables.

class f:
    def __call__(self, x, y, z):
        return x, y, z

Thank You.

1 Like