Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions keras/src/layers/merging/concatenate.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,13 @@ def compute_mask(self, inputs, mask=None):
# Input is unmasked. Append all 1s to masks,
masks.append(ops.ones_like(input_i, dtype="bool"))
elif mask_i.ndim < input_i.ndim:
# Mask is smaller than the input, expand it
masks.append(
ops.broadcast_to(
ops.expand_dims(mask_i, axis=-1), ops.shape(input_i)
)
# Broadcast mask shape to match in a way where we capture the
# input as a symbolic input in the op graph.
mask_i = ops.logical_or(
Copy link
Collaborator

Choose a reason for hiding this comment

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

So you're doing an ops.logical_or just to do the broadcast.

Do you understand why this works and ops.broadcast_to doesn't?

In other words, should we try to fix ops.broadcast_to? Is it a bug in tf.broadcast_to?

ops.expand_dims(mask_i, axis=-1),
ops.zeros_like(input_i, dtype="bool"),
)
masks.append(mask_i)
else:
masks.append(mask_i)
concatenated = ops.concatenate(masks, axis=self.axis)
Expand Down
12 changes: 12 additions & 0 deletions keras/src/layers/merging/merging_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,18 @@ def test_concatenate_with_mask(self):
)
self.assertAllClose(output._keras_mask, [[1, 1, 1, 1]])

def test_concatenate_with_mask_symbolic(self):
input1 = layers.Input((4, 2))
input2 = layers.Input((4, 2))
mask = layers.Masking()
output = layers.Concatenate(axis=1)([mask(input1), input2])
model = models.Model(
inputs=[input1, input2], outputs=output._keras_mask
)
x1 = backend.convert_to_tensor([[[0, 0], [1, 2], [0, 0], [3, 4]]])
x2 = backend.convert_to_tensor([[[0, 0], [0, 0], [1, 2], [3, 4]]])
self.assertAllClose(model([x1, x2]), [[0, 1, 0, 1, 1, 1, 1, 1]])

def test_concatenate_errors(self):
# This should work
x1 = np.ones((1, 1, 1, 1, 5))
Expand Down