Improve the Keras error message that happens when users pass kerastensors to apis that do not support dispatching. The message should hopefully be more understandable and actionable.
@@ -242,14 +242,17 @@ class KerasTensor:
def __array__(self):
raise TypeError(
- 'Cannot convert a symbolic Keras input/output to a numpy array. '
- 'This error may indicate that you\'re trying to pass a symbolic value '
- 'to a NumPy call, which is not supported. Or, '
- 'you may be trying to pass Keras symbolic inputs/outputs '
- 'to a TF API that does not register dispatching, '
- 'preventing Keras from automatically '
- 'converting the API call to a lambda layer '
- 'in the Functional Model.')
+ f'You are passing {self}, an intermediate Keras symbolic input/output, '
+ 'to a TF API that does not allow registering custom dispatchers, such '
+ 'as `tf.cond`, `tf.function`, gradient tapes, or `tf.map_fn`. '
+ 'Keras Functional model construction only supports '
+ 'TF API calls that *do* support dispatching, such as `tf.math.add` or '
+ '`tf.reshape`. '
+ 'Other APIs cannot be called directly on symbolic Keras'
+ 'inputs/outputs. You can work around '
+ 'this limitation by putting the operation in a custom Keras layer '
+ '`call` and calling that layer '
+ 'on this symbolic input/output.')
@property
def is_tensor_like(self):
