tf.data.Dataset.map does not support randomization
Hello, the problem you are facing @kzhai is because in TensorFlow, the operations within the tf.data.Dataset.map() function are executed eagerly, meaning they are executed immediately during the iteration (as they are run). On the other hand, NumPy's random functions are executed eagerly by default outside the TensorFlow computation graph. This causes the random values to be generated only once and remain static throughout the execution of the tf.data.Dataset.map() function.

You can use the `tf.py_function` function- Wraps a python / NumPy function into a TensorFlow op that executes it eagerly. This allows you to execute the NumPy code as a TensorFlow operation. 
[See documentation of py_function here.](https://www.tensorflow.org/api_docs/python/tf/py_function)

- Change the NumPy function code in `test_function()` to:
`x[f] = tf.py_function(lambda: np.random.randint(low=low, high=high), [], tf.int64)`

OR you can use `tf.numpy_function` function - Wraps a python function and uses it as a TensorFlow op.
[See numpy_function documentation here.](https://www.tensorflow.org/api_docs/python/tf/numpy_function)

- Change the NumPy function code in `test_function()` to:
`x[f] = tf.numpy_function(lambda: np.random.randint(low=low, high=high), [], tf.int64)` 

This seems to work for me, hope it helps you a little to get started.

[Here is a stack overflow discussion you might want to take a look at.](https://stackoverflow.com/questions/57236864/is-there-a-way-to-call-a-numpy-function-inside-a-tensorflow-session) Hello @sushreebarsa, can you please let me know if I understood the problem correctly or not? and is the solution suggest by me above relevant to solving the issue? Thank you.

> Hello, the problem you are facing @kzhai is because in TensorFlow, the operations within the tf.data.Dataset.map() function are executed eagerly, meaning they are executed immediately during the iteration (as they are run). On the other hand, NumPy's random functions are executed eagerly by default outside the TensorFlow computation graph. This causes the random values to be generated only once and remain static throughout the execution of the tf.data.Dataset.map() function.
> 
> You can use the `tf.py_function` function- Wraps a python / NumPy function into a TensorFlow op that executes it eagerly. This allows you to execute the NumPy code as a TensorFlow operation. [See documentation of py_function here.](https://www.tensorflow.org/api_docs/python/tf/py_function)
> 
> * Change the NumPy function code in `test_function()` to:
>   `x[f] = tf.py_function(lambda: np.random.randint(low=low, high=high), [], tf.int64)`
> 
> OR you can use `tf.numpy_function` function - Wraps a python function and uses it as a TensorFlow op. [See numpy_function documentation here.](https://www.tensorflow.org/api_docs/python/tf/numpy_function)
> 
> * Change the NumPy function code in `test_function()` to:
>   `x[f] = tf.numpy_function(lambda: np.random.randint(low=low, high=high), [], tf.int64)`
> 
> This seems to work for me, hope it helps you a little to get started.
> 
> [Here is a stack overflow discussion you might want to take a look at.](https://stackoverflow.com/questions/57236864/is-there-a-way-to-call-a-numpy-function-inside-a-tensorflow-session)

 @aadityab7 Thank you for your response in this thread. 

@kzhai TensorFlow map() method of tf.data.Dataset used for transforming items in a dataset.  Randomization in tf.data.Dataset.map is not random if that comes from outside of TF.  It happens because the functions passed to tf.data transforms are traced and converted into eager execution. Incase of tracing the rd.random() and np.random.rand() are evaluated into constants.  You can use TF ops too for simplification. Another option is to wrap non-tensorflow code in [tf.py_function](https://www.tensorflow.org/api_docs/python/tf/py_function), which executes arbitrary python code as a tensorflow op. 

Thank you!

  This issue is stale because it has been open for 7 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 7 days since being marked as stale. Please reopen if you'd like to work on this further. Are you satisfied with the resolution of your issue?
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=Yes&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/60848">Yes</a>
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=No&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/60848">No</a>

<details><summary>Click to expand!</summary> 
 
 ### Issue Type

Bug

### Have you reproduced the bug with TF nightly?

Didn't try.

### Source

binary

### Tensorflow Version

tf 2.11.0

### Custom Code

Yes

### OS Platform and Distribution

MacOS

### Python version

Python 3.8

It seems like `tf.data.Dataset.map` does not support randomization from `numpy`.
For the sample code, it gives constant output from the `randint` function call, but if you switch the function from `numpy.random.randint` to `tf.random.uniform` (toggle the comment), then you get good randomization behavior. 
I am wondering if this is expected.

### Standalone code to reproduce the issue

```shell
a = [{"key": i, "value": np.random.random()} for i in range(5)]
dict_of_list = pd.DataFrame.from_records(a).to_dict(
    orient="list")
keys = dict_of_list.keys()

dataset = tf.data.Dataset.zip(
    tuple([
        tf.data.Dataset.from_tensor_slices(dict_of_list[key]) for key in keys
    ])
)
dataset = dataset.map(lambda *x: {key: x[i] for i, key in enumerate(keys)})

for i in range(2):
    for b in dataset:
        print("before map:", i, b)

def test_function(x, f="key", low=0, high=100):
    #x[f] = tf.random.uniform(shape=(), minval=1, maxval=5, dtype=tf.int32) # this is randomized
    x[f] = np.random.randint(low=low, high=high) # this is not randomized
    return x

dataset = dataset.map(test_function)

for i in range(2):
    for b in dataset:
        print("after map:", i, b)
```


### Relevant log output

```shell
before map: 0 {'key': <tf.Tensor: shape=(), dtype=int32, numpy=0>, 'value': <tf.Tensor: shape=(), dtype=float32, numpy=0.9599878>}
before map: 0 {'key': <tf.Tensor: shape=(), dtype=int32, numpy=1>, 'value': <tf.Tensor: shape=(), dtype=float32, numpy=0.5124935>}
before map: 0 {'key': <tf.Tensor: shape=(), dtype=int32, numpy=2>, 'value': <tf.Tensor: shape=(), dtype=float32, numpy=0.39335275>}
before map: 0 {'key': <tf.Tensor: shape=(), dtype=int32, numpy=3>, 'value': <tf.Tensor: shape=(), dtype=float32, numpy=0.1416868>}
before map: 0 {'key': <tf.Tensor: shape=(), dtype=int32, numpy=4>, 'value': <tf.Tensor: shape=(), dtype=float32, numpy=0.39475128>}
before map: 1 {'key': <tf.Tensor: shape=(), dtype=int32, numpy=0>, 'value': <tf.Tensor: shape=(), dtype=float32, numpy=0.9599878>}
before map: 1 {'key': <tf.Tensor: shape=(), dtype=int32, numpy=1>, 'value': <tf.Tensor: shape=(), dtype=float32, numpy=0.5124935>}
before map: 1 {'key': <tf.Tensor: shape=(), dtype=int32, numpy=2>, 'value': <tf.Tensor: shape=(), dtype=float32, numpy=0.39335275>}
before map: 1 {'key': <tf.Tensor: shape=(), dtype=int32, numpy=3>, 'value': <tf.Tensor: shape=(), dtype=float32, numpy=0.1416868>}
before map: 1 {'key': <tf.Tensor: shape=(), dtype=int32, numpy=4>, 'value': <tf.Tensor: shape=(), dtype=float32, numpy=0.39475128>}
after map: 0 {'key': <tf.Tensor: shape=(), dtype=int32, numpy=44>, 'value': <tf.Tensor: shape=(), dtype=float32, numpy=0.9599878>}
after map: 0 {'key': <tf.Tensor: shape=(), dtype=int32, numpy=44>, 'value': <tf.Tensor: shape=(), dtype=float32, numpy=0.5124935>}
after map: 0 {'key': <tf.Tensor: shape=(), dtype=int32, numpy=44>, 'value': <tf.Tensor: shape=(), dtype=float32, numpy=0.39335275>}
after map: 0 {'key': <tf.Tensor: shape=(), dtype=int32, numpy=44>, 'value': <tf.Tensor: shape=(), dtype=float32, numpy=0.1416868>}
after map: 0 {'key': <tf.Tensor: shape=(), dtype=int32, numpy=44>, 'value': <tf.Tensor: shape=(), dtype=float32, numpy=0.39475128>}
after map: 1 {'key': <tf.Tensor: shape=(), dtype=int32, numpy=44>, 'value': <tf.Tensor: shape=(), dtype=float32, numpy=0.9599878>}
after map: 1 {'key': <tf.Tensor: shape=(), dtype=int32, numpy=44>, 'value': <tf.Tensor: shape=(), dtype=float32, numpy=0.5124935>}
after map: 1 {'key': <tf.Tensor: shape=(), dtype=int32, numpy=44>, 'value': <tf.Tensor: shape=(), dtype=float32, numpy=0.39335275>}
after map: 1 {'key': <tf.Tensor: shape=(), dtype=int32, numpy=44>, 'value': <tf.Tensor: shape=(), dtype=float32, numpy=0.1416868>}
after map: 1 {'key': <tf.Tensor: shape=(), dtype=int32, numpy=44>, 'value': <tf.Tensor: shape=(), dtype=float32, numpy=0.39475128>}
```
</details>