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 ...)