Skip to content

I get an invalid shape error when running tf.keras.losses.BinaryCrossentropy #89245

@sjh0849

Description

@sjh0849

Issue type

Bug

Have you reproduced the bug with TensorFlow Nightly?

No

Source

binary

TensorFlow version

2.18.0

Custom code

Yes

OS platform and distribution

Linux 22.04

Mobile device

N\A

Python version

3.9

Bazel version

N\A

GCC/compiler version

N\A

CUDA/cuDNN version

N\A

GPU model and memory

N\A

Current behavior?

I get an invalid shape error when running tf.keras.losses.BinaryCrossentropy

Standalone code to reproduce the issue

def test_binary_crossentropy_invalid_inputs(self):
        # Test with invalid inputs
        y_true = np.array([0, 1, 0, 1], dtype=np.float32)
        y_pred = np.array([0.1, 0.9, 0.2], dtype=np.float32)  # Mismatched shape
        
        bce = tf.keras.losses.BinaryCrossentropy()
        with self.assertRaises(ValueError):
            bce(y_true, y_pred)

Relevant log output

Traceback (most recent call last):
  File "/home/user/projects/api_guided_testgen/out/bug_detect_gpt4o/exec/basic_rag_apidoc/tf/tf.keras.losses.BinaryCrossentropy.py", line 70, in test_binary_crossentropy_invalid_inputs
    bce(y_true, y_pred)
  File "/home/user/anaconda3/lib/python3.8/site-packages/keras/losses.py", line 152, in __call__
    losses = call_fn(y_true, y_pred)
  File "/home/user/anaconda3/lib/python3.8/site-packages/keras/losses.py", line 284, in call
    return ag_fn(y_true, y_pred, **self._fn_kwargs)
  File "/home/user/anaconda3/lib/python3.8/site-packages/tensorflow/python/util/traceback_utils.py", line 153, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/home/user/anaconda3/lib/python3.8/site-packages/keras/losses.py", line 2176, in binary_crossentropy
    backend.binary_crossentropy(y_true, y_pred, from_logits=from_logits),
  File "/home/user/anaconda3/lib/python3.8/site-packages/keras/backend.py", line 5688, in binary_crossentropy
    bce = target * tf.math.log(output + epsilon())
tensorflow.python.framework.errors_impl.InvalidArgumentError: {{function_node __wrapped__Mul_device_/job:localhost/replica:0/task:0/device:CPU:0}} Incompatible shapes: [4] vs. [3] [Op:Mul]

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions