@lia
Here is an example of how you can load and preprocess data in TensorFlow:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
import tensorflow as tf
from sklearn.model_selection import train_test_split
import numpy as np
# Load the data
data = np.random.random((1000, 10)) # Example data, replace with your own data
# Split the data into training and testing sets
train_data, test_data = train_test_split(data, test_size=0.2)
# Preprocess the data (e.g., normalization)
preprocessed_train_data = train_data / np.max(train_data)
preprocessed_test_data = test_data / np.max(test_data)
# Create TensorFlow Dataset objects
train_dataset = tf.data.Dataset.from_tensor_slices(preprocessed_train_data)
test_dataset = tf.data.Dataset.from_tensor_slices(preprocessed_test_data)
# Shuffle and batch the data
train_dataset = train_dataset.shuffle(buffer_size=len(train_dataset)).batch(32)
test_dataset = test_dataset.batch(32)
# Iterate over the data
train_iterator = iter(train_dataset)
for batch in train_iterator:
# Perform operations on the batched data
print(batch)
|
In this example, we first import TensorFlow and other necessary libraries. We then load some sample data, split it into training and testing sets, preprocess it by normalizing it, convert it into TensorFlow Dataset objects, shuffle and batch the training and testing datasets, and finally iterate over the training data in batches.
You can replace the example data with your own data and customize the preprocessing steps and batch sizes according to your requirements.