@hal.littel
To read data from a CSV file in TensorFlow, you can use the tf.data.experimental.make_csv_dataset()
function. Here is an example code snippet that demonstrates how to read data from a CSV file using TensorFlow:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import tensorflow as tf # Define the file path to the CSV file file_path = 'data.csv' # Define the column names and data types columns = ['feature1', 'feature2', 'label'] column_defaults = [tf.float32, tf.float32, tf.int32] # Create a dataset from the CSV file dataset = tf.data.experimental.make_csv_dataset( file_path, batch_size=32, column_names=columns, column_defaults=column_defaults, label_name='label', num_epochs=1 ) # Iterate over the dataset and print the data for batch in dataset: print('Features:', batch['feature1'], batch['feature2']) print('Labels:', batch['label']) |
In this code snippet:
You can then iterate over the dataset to access the features and labels.