2

When I run this following code :

import numpy as np
import tensorflow as tf

arr = np.array([2., 4., 5., 9.])
tf.keras.utils.to_categorical(arr, 10)

everything goes right, and the output is:

array([[0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 1., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]], dtype=float32)

But if i replace arr by tf.constant(arr) (or I guess but without certainty by any Tensor) like in the following code :

tf.keras.utils.to_categorical(tf.constant(arr), 10)

I get the following error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-139-507d6ce7c0a3> in <module>()
----> 1 tf.keras.utils.to_categorical(tf.constant(arr), 10)

.../miniconda2/lib/python2.7/site-packages/tensorflow/python/keras/utils/np_utils.pyc in to_categorical(y, num_classes, dtype)
     38       last.
     39   """
---> 40   y = np.array(y, dtype='int')
     41   input_shape = y.shape
     42   if input_shape and input_shape[-1] == 1 and len(input_shape) > 1:

ValueError: setting an array element with a sequence.

Configuration:

  • Python: 2.7
  • Tensorflow: 1.13.1
  • Keras (inside Tensorflow): 2.2.4-tf
  • OS: Ubuntu 16.04

How could I get rid / workaround this issue ?

A little bit of context:

My main issue is, when I call model.compile with the following loss:

def loss(y_true, y_pred):
    # Cross entropy loss
    bin_true = y_true[:, 0]
    print bin_true.eval()

    dum = tf.keras.utils.to_categorical(bin_true, 66)
    cls_loss = tf.keras.losses.categorical_crossentropy(dum, y_pred, True)

    # MSE loss
    cont_true = y_true[:, 1]
    pred_cont = tf.keras.backend.sum(tf.nn.softmax(y_pred) * idx_tensor, 1) * 3 - 99
    mse_loss = tf.keras.losses.mean_squared_error(cont_true, pred_cont)

    # Total loss
    return cls_loss + 0.5 * mse_loss

I get the exact same error at line dum = tf.keras.utils.to_categorical(bin_true, 66))

(I give some context because my whole "way to do" may be wrong ...)

2 Answers 2

2

You could try using tf.one_hot instead of keras to_categorical

Sign up to request clarification or add additional context in comments.

1 Comment

Your solution works, but for that I need to call specifically TensorFlow while I would like to stay "high level" with Keras. Nice workaround anyway, thanks!
2

The naive answer to your question is to use get_value(x) as in

bin_true_array = tf.keras.backend.get_value(bin_true)
dum = tf.keras.utils.to_categorical(bin_true_array, 66)
....

this would retrieve the value of bin_true as a numpy array and then feed that into the numpy utility utils.to_categorical. Here is your example (adjusted):

import numpy as np
import tensorflow as tf

arr = np.array([2., 4., 5., 9.])
tf.keras.utils.to_categorical(arr, 10)

tensor = tf.constant(arr)
tensor_as_array = tf.keras.backend.get_value(tensor)
tf.keras.utils.to_categorical(tensor_as_array, 10)

The problem with this approach is that you exit the backend, go back into python and numpy (which comes with all the python goodies such as GIL), and then push things back down into the backend. This may (or may not) create a bottleneck in your pipeline. In this case a backend dependent solution, like the one @Robin Beilvert suggested, might serve you better.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.