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

Return ValueError in case of empty list input for tf.map_fn #39241

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions tensorflow/python/kernel_tests/map_fn_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ def testMapEmptyTensor(self):
self.assertAllEqual([0, 3, 2], map_return.get_shape().dims)
self.assertAllEqual([0, 3, 2], self.evaluate(map_return).shape)

@test_util.run_in_graph_and_eager_modes
def testMapEmptyList(self):
x = []
with self.assertRaisesRegexp(
ValueError, r"elems must be a Tensor or"):
_ = map_fn.map_fn(lambda e: e, x)

if __name__ == "__main__":
test.main()
Expand Down
11 changes: 9 additions & 2 deletions tensorflow/python/ops/map_fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def map_fn(fn,
elems: A tensor or (possibly nested) sequence of tensors, each of which will
be unstacked along their first dimension. `fn` will be applied to the
nested sequence of the resulting slices. `elems` may include ragged and
sparse tensors.
sparse tensors. `elems` must consist of at least one tensor.
dtype: Deprecated: Equivalent to `fn_output_signature`.
parallel_iterations: (optional) The number of iterations allowed to run in
parallel. When graph building, the default value is 10. While executing
Expand Down Expand Up @@ -296,7 +296,7 @@ def map_fn(fn,
TypeError: if `fn` is not callable or the structure of the output of
`fn` and `fn_output_signature` do not match.
ValueError: if the lengths of the output of `fn` and `fn_output_signature`
do not match.
do not match, or if the `elems` does not contain any tensor.

Examples:

Expand Down Expand Up @@ -375,6 +375,13 @@ def map_fn(fn,

# Flatten the input tensors, and get the TypeSpec for each one.
elems_flat = nest.flatten(elems)

# Check in case this is an empty list
if len(elems_flat) == 0:
raise ValueError(
"elems must be a Tensor or (possibly nested) sequence of Tensors. "
"Got {}, which does not contain any Tensors.".format(elems))

elems_flat_signature = [type_spec.type_spec_from_value(e) for e in elems_flat]
elems_unflatten = lambda x: nest.pack_sequence_as(elems, x)

Expand Down