votes up 6

x and y must be either both None or both non-None

Package:
Exception Class:
ValueError

Raise code

""" 
      shape is broadcast-compatible with `x`, `y`, and `condition`.

  Raises:
    ValueError: When exactly one of `x` or `y` is non-`None`; or when
      `condition`, `x`, and `y` have incompatible shapes.
  """
  if (x is None) != (y is None):
    raise ValueError('x and y must be either both None or both non-None')

  with ops.name_scope('RaggedWhere', name, [condition, x, y]):
    condition = ragged_tensor.convert_to_tensor_or_ragged_tensor(
        condition, name='condition')
    if x is None:
      return _coordinate_where(condition)
    else:
🙏 Scream for help to Ukraine
Today, 25th May 2022, Russia continues bombing and firing Ukraine. Don't trust Russia, they are bombing us and brazenly lying in same time they are not doing this 😠, civilians and children are dying too! We are screaming and asking exactly you to help us, we want to survive, our families, children, older ones.
Please spread the information, and ask your governemnt to stop Russia by any means. We promise to work extrahard after survival to make the world safer place for all.

Ways to fix

votes up 1 votes down

x shouldn't be None while y is not None or vice versa. Both of them should be None together or not None.

How to reproduce the error:

pipenv install tensorflow

import tensorflow as tf
x = tf.ragged.constant([['A', 'B'], ['C', 'D', 'E']])
y = tf.ragged.constant([['a', 'b'], ['c', 'd', 'e']])
condition = tf.ragged.constant([[True, False], [True, False, True]])
result = tf.where(condition,x,None)
print(result)

The error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs)
    205     try:
--> 206       return target(*args, **kwargs)
    207     except (TypeError, ValueError):

/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/array_ops.py in where_v2(condition, x, y, name)
   4691   else:
-> 4692     raise ValueError("x and y must both be non-None or both be None.")
   4693 

ValueError: x and y must both be non-None or both be None.
During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-39-a29909fd81bb> in <module>()
      2 y = tf.ragged.constant([['a', 'b'], ['c', 'd', 'e']])
      3 condition = tf.ragged.constant([[True, False], [True, False, True]])
----> 4 result = tf.where(condition,x,None)
      5 print(result)

/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs)
    208       # Note: convert_to_eager_tensor currently raises a ValueError, not a
    209       # TypeError, when given unexpected types.  So we need to catch both.
--> 210       result = dispatch(wrapper, args, kwargs)
    211       if result is not OpDispatcher.NOT_SUPPORTED:
    212         return result

/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py in dispatch(op, args, kwargs)
    120   """
    121   for dispatcher in getattr(op, DISPATCH_ATTR):
--> 122     result = dispatcher.handle(args, kwargs)
    123     if result is not OpDispatcher.NOT_SUPPORTED:
    124       return result

/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/ragged/ragged_dispatch.py in handle(self, args, kwargs)
    257   def handle(self, args, kwargs):
    258     if self.is_supported(args, kwargs):
--> 259       return self._ragged_op(*args, **kwargs)
    260     else:
    261       return self.NOT_SUPPORTED

/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/ragged/ragged_where_op.py in where_v2(condition, x, y, name)
     76   """
     77   if (x is None) != (y is None):
---> 78     raise ValueError('x and y must be either both None or both non-None')
     79 
     80   with ops.name_scope('RaggedWhere', name, [condition, x, y]):

ValueError: x and y must be either both None or both non-None

Fixed code:

x and y should be both not None or None at the same time. one None and the not None cause the error.

import tensorflow as tf
x = tf.ragged.constant([['A', 'B'], ['C', 'D', 'E']])
y = tf.ragged.constant([['a', 'b'], ['c', 'd', 'e']])
condition = tf.ragged.constant([[True, False], [True, False, True]])
result = tf.where(condition,x,y)
print(result)

Correct output:

<tf.RaggedTensor [[b'A', b'b'], [b'C', b'd', b'E']]>

Jul 02, 2021 kellemnegasi answer
kellemnegasi 29.0k

Add a possible fix

Please authorize to post fix