Converting Numpy text array to tf.data.Dataset

Converting Numpy text array to tf.data.Dataset

Problem Description:

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?

Solution – 1

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))
Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject