Home > Back-end >  Converting Numpy text array to tf.data.Dataset
Converting Numpy text array to tf.data.Dataset

Time:12-05

i have 2 a numpy nd arrays of shape (2000,) where each element is a list containing words as items. Thus each list is a sentence. The other nd array are just the binary labels.
I want to convert this to tensorflow data Dataset where each item is a sentence with a label.
I tried:

 tf.data.Dataset.from_tensor_slices((dataset, labels))

but i get :

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type list).

How could this be done?

CodePudding user response:

This error usually occurs when each list in your array has a different number of elements (words). Try using a ragged representation:

tf.data.Dataset.from_tensor_slices((tf.ragged.constant(dataset), labels))
  • Related