Tensorflow - Failed to convert a NumPy array to a Tensor (Unsupported object type float)

Viewed 1458

I'm trying to convert a dataframe into a tf.data format. I'm using the following code:

def df_to_dataset(dataframe, shuffle=True, batch_size=32):
  dataframe = dataframe.copy()
  labels = dataframe.pop('sentiment')

  ds = tf.data.Dataset.from_tensor_slices((dict(dataframe), labels))

  if shuffle:

    ds = ds.shuffle(buffer_size=len(dataframe))
  #ds = ds.batch(batch_size)
  return ds

and my input dataframe is :

           text                                              sentiment
13000     doesnt know how to pimp out here Twitter page.          0
13001   just a question.. are you guys liek famous or...          0
13002   thats pretty bad quality and probably the wor...          2
13003  **** it. Totally gutted I decided not to go ou...          2
13004   You're not here. I hope you're still resting....          1

text         object
sentiment     int64
dtype: object

But whe I'm trying to run it, I'm facing this error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/data/util/structure.py in normalize_element(element)
     92       try:
---> 93         spec = type_spec_from_value(t, use_fallback=False)
     94       except TypeError:

10 frames
TypeError: Could not build a TypeSpec for 13000       doesnt know how to pimp out here Twitter page.
13001     just a question.. are you guys liek famous or...
13002     thats pretty bad quality and probably the wor...
13003    **** it. Totally gutted I decided not to go ou...
13004     You're not here. I hope you're still resting....
                               ...                        
19995     yep trying to beat the cold winter. Morning s...
19996             have a good day girl, ill call you later
19997     Forget completely abt uploading the assignemt...
19998     yeah I've been there, my son is two so we go ...
19999     when nina picked up my call lastnight i didnt...
Name: text, Length: 7000, dtype: object with type Series

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
     94       dtype = dtypes.as_dtype(dtype).as_datatype_enum
     95   ctx.ensure_initialized()
---> 96   return ops.EagerTensor(value, ctx.device_name, dtype)
     97 
     98 

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

I've tried googling the error myself, but its gone in vain. Any help would be truly appreciated. Thanks !

1 Answers

If you still haven't solved it, try converting the columns of the DataFrame to tensor arrays individually using tf.convert_to_tensor() method:

def df_to_dataset(dataframe, shuffle=True, batch_size=32):
  dataframe = dataframe.copy()
  labels = dataframe['labels'].values # Try these first, if these don't, 
  text = dataframe['text'].values     # work, try the next 2 commands.

  # labels = tf.convert_to_tensor(dataframe['labels'].values)
  # text = tf.convert_to_tensor(dataframe['text'].values)

  ds = tf.data.Dataset.from_tensor_slices((text, labels))

  if shuffle:
    ds = ds.shuffle(buffer_size=len(dataframe))
  #ds = ds.batch(batch_size)
  return ds

If you have solved it, can you please share the solution :)

Related