Skip to content

Docs - Fixed Syntax Error in vectorized_map function example #21503

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

Merged
merged 1 commit into from
Jul 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions keras/src/ops/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1074,18 +1074,18 @@ def vectorized_map(function, elements):
in the case of a single tensor input `elements`:

```python
def vectorized_map(function, elements)
def vectorized_map(function, elements):
outputs = []
for e in elements:
outputs.append(function(e))
return stack(outputs)
return np.stack(outputs)
Comment on lines 1078 to +1081
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For improved conciseness and consistency with other examples in this file (like the map function's docstring), consider using a list comprehension. This is generally the preferred Pythonic way to build a list.

This would simplify the body of the example function to a single line.

Suggested change
outputs = []
for e in elements:
outputs.append(function(e))
return stack(outputs)
return np.stack(outputs)
return np.stack([function(e) for e in elements])

```

In the case of an iterable of tensors `elements`,
it implements the following:

```python
def vectorized_map(function, elements)
def vectorized_map(function, elements):
batch_size = elements[0].shape[0]
outputs = []
for index in range(batch_size):
Expand Down