How to map a function with additional parameter using the new Dataset api in TF1.3?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In the TensorFlow 1.3 Dataset API, map expects a function whose arguments match the structure of each dataset element. If you want to use an additional parameter, the usual pattern is to capture that value in a lambda or closure, or to make it part of the dataset element itself when it varies per record.
The Basic Rule Of Dataset.map
If your dataset yields one tensor per element, your map function should accept one argument. If the dataset yields a tuple such as (features, label), your map function should accept two arguments.
For example:
The lambda takes one argument because each dataset element is one scalar tensor.
Add A Constant Extra Parameter With A Closure
If the extra parameter is the same for every element, the easiest solution is to capture it in the map function.
This is the standard answer when the additional parameter is global configuration rather than per-example data.
You can also write a named function and wrap it:
The lambda exists only to bind the extra argument.
Include The Extra Value In The Dataset If It Varies Per Element
If the additional parameter changes for each example, do not capture a single external value. Make that parameter part of the dataset itself.
Now each element is a pair, so the map function takes two arguments.
This is the better design when the parameter is actually data.
Why Not Just Call The Function Directly
A common mistake is writing something like this:
That calls the function immediately while building the graph instead of passing a callable for TensorFlow to use on each dataset element.
map needs a function object, not the result of one function call.
That is why these two patterns are valid:
- '
dataset.map(lambda x: transform(x, scale))' - '
dataset.map(transform)when the function signature already matches the element structure'
Be Careful With Python Values Versus Tensors
In TF1.3, the extra argument can be a Python literal or a TensorFlow tensor depending on what the function does.
Example with a Python constant:
Example with a TensorFlow constant:
Both can work, but remember that TF1.3 is graph-based. If the extra parameter should participate in the graph, represent it as a tensor.
A Pattern For More Complex Preprocessing
Suppose you have images and want to resize every image to the same target width and height. Capture those dimensions in the closure.
This is still the same principle: use a closure for fixed extra parameters.
Performance And Readability
Using a lambda for parameter binding is normal in the Dataset API. Do not avoid it just because it feels indirect.
What matters more is clarity:
- use closure capture for fixed configuration
- put variable data into the dataset structure
- keep the map function signature aligned with the dataset element structure
That mental model scales cleanly to more complex pipelines.
Common Pitfalls
- Calling the transformation function immediately instead of passing a callable to
map. - Capturing one global parameter when the value should actually vary per element.
- Forgetting that the function signature must match the dataset element structure.
- Mixing Python-side logic and graph tensors carelessly in TF1.x input pipelines.
- Making the pipeline harder to read by hiding data dependencies in too many nested lambdas.
Summary
- In TF1.3,
Dataset.mapfunctions must match the structure of each dataset element. - For one fixed extra parameter, bind it with a lambda or closure.
- For per-example parameters, include them in the dataset and accept them as map arguments.
- Pass a callable to
map, not the result of calling your function. - Think in terms of dataset element structure first and the right mapping pattern becomes obvious.

