1

I have a dataframe that one of it's columns is like this:

values
-------------
| [0, 2]    |
| [0]       |
| [5, 1, 9] |
|    .      |
|    .      |
|    .      |
------------

The daya type for this column is object now. How can I convert this column into a tensorflow dataset?

  • Does this answer your question? [How to use the Tensorflow Dataset Pipeline for Variable Length Inputs?](https://stackoverflow.com/questions/50329855/how-to-use-the-tensorflow-dataset-pipeline-for-variable-length-inputs) – MYousefi Jun 09 '22 at 14:47

1 Answers1

1

Just try using a ragged structure:

import tensorflow as tf
import pandas as pd

df = pd.DataFrame(data={'values':[[0, 2], [0], [5, 1, 9]]})

ds = tf.data.Dataset.from_tensor_slices((tf.ragged.constant(df['values'])))

for d in ds:
  print(d)
tf.Tensor([0 2], shape=(2,), dtype=int32)
tf.Tensor([0], shape=(1,), dtype=int32)
tf.Tensor([5 1 9], shape=(3,), dtype=int32)

And if you want each tensor to be the same length:

ds = tf.data.Dataset.from_tensor_slices((tf.ragged.constant(df['values']).to_tensor()))
for d in ds:
  print(d)
tf.Tensor([0 2 0], shape=(3,), dtype=int32)
tf.Tensor([0 0 0], shape=(3,), dtype=int32)
tf.Tensor([5 1 9], shape=(3,), dtype=int32)
AloneTogether
  • 25,814
  • 5
  • 20
  • 39