Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 1 addition & 3 deletions keras/src/backend/common/backend_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import functools
import operator
import re
import warnings

Expand Down Expand Up @@ -262,14 +261,13 @@ def compute_conv_transpose_output_shape(

def canonicalize_axis(axis, num_dims):
"""Canonicalize an axis in [-num_dims, num_dims) to [0, num_dims)."""
axis = operator.index(axis)
if not -num_dims <= axis < num_dims:
raise ValueError(
f"axis {axis} is out of bounds for an array with dimension "
f"{num_dims}."
)
if axis < 0:
axis = axis + num_dims
axis += num_dims
return axis


Expand Down
3 changes: 2 additions & 1 deletion keras/src/ops/operation_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ def compute_expand_dims_output_shape(input_shape, axis):
axis = to_tuple_or_list(axis)
out_ndim = len(axis) + len(input_shape)
axis = [canonicalize_axis(a, out_ndim) for a in axis]
axis_set = set(axis)
shape_iter = iter(input_shape)
new_shape = [
1 if ax in axis else next(shape_iter) for ax in range(out_ndim)
1 if ax in axis_set else next(shape_iter) for ax in range(out_ndim)
]
return tuple(new_shape)

Expand Down