Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added example and description about numpy_function() in Dataset.map() #37853

Merged
merged 4 commits into from Apr 2, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions tensorflow/python/data/ops/dataset_ops.py
Expand Up @@ -1619,6 +1619,19 @@ def map(self, map_func, num_parallel_calls=None, deterministic=None):
>>> list(d.as_numpy_iterator())
[b'HELLO', b'WORLD']

3) Use `tf.numpy_function`, which also allows you to write arbitrary
ashutosh1919 marked this conversation as resolved.
Show resolved Hide resolved
Python code. Note here that `tf.py_function` accepts `tf.Tensor` whereas
`tf.numpy_function` accepts numpy arrays and returns only numpy arrays.
For example:

>>> d = tf.data.Dataset.from_tensor_slices(['hello', 'world'])
>>> def upper_case_fn(t: np.ndarray):
... return t.decode('utf-8').upper()
>>> d = d.map(lambda x: tf.numpy_function(func=upper_case_fn,
... inp=[x], Tout=tf.string))
>>> list(d.as_numpy_iterator())
[b'HELLO', b'WORLD']

Performance can often be improved by setting `num_parallel_calls` so that
`map` will use multiple threads to process elements. If deterministic order
isn't required, it can also improve performance to set
Expand Down