How to read multiple columns as labels using make_csv_dataset in tensorflow 2?

Viewed 748

I'm trying to use the following code (that I found in Tensorflow tutorials here) to read the data from a CSV file:

def get_dataset(file_path, **kwargs):
  dataset = tf.data.experimental.make_csv_dataset(
      file_path,
      batch_size=5, # Artificially small to make examples easier to show.
      label_name=LABEL_COLUMN,
      na_value="?",
      num_epochs=1,
      ignore_errors=True, 
      **kwargs)
  return dataset

It works fine when you have one column as the label column. However, in my CSV file I have multiple columns as labels (I have 1008 features and 2 columns as labels). I'm wondering how I can read my data using this make_csv_dataset.

Thank you!

1 Answers

Tensorflow's make_csv_dataset() does not support data for multi-output models as of yet (It is experimental afterall). You can alternatively use pandas dataframe to read in the data and then use tf.data.Dataset.from_tensor_slices() method to get your dataset. I would recommend memory mapping the csv data while creating the pandas dataframe as it would be faster this way

Related