We use datasets from tensors to easily handle and process data in machine learning. It helps us feed data step-by-step to models.
0
0
Dataset from tensors in TensorFlow
Introduction
When you have data already in memory as arrays or tensors and want to prepare it for training.
When you want to shuffle, batch, or repeat your data for better model training.
When you want to create a simple pipeline to feed data to a TensorFlow model.
When you want to convert multiple tensors into a single dataset for easy iteration.
Syntax
TensorFlow
tf.data.Dataset.from_tensor_slices(tensors)
tensors can be a single tensor or a tuple/dictionary of tensors.
This method creates a dataset where each element is one slice (row) from the tensors.
Examples
This creates a dataset from a 1D tensor and prints each element.
TensorFlow
import tensorflow as tf # Single tensor data = tf.constant([10, 20, 30]) dataset = tf.data.Dataset.from_tensor_slices(data) for item in dataset: print(item.numpy())
This creates a dataset from features and labels tensors and prints each pair.
TensorFlow
import tensorflow as tf # Multiple tensors features = tf.constant([[1, 2], [3, 4], [5, 6]]) labels = tf.constant([0, 1, 0]) dataset = tf.data.Dataset.from_tensor_slices((features, labels)) for x, y in dataset: print(f"Features: {x.numpy()}, Label: {y.numpy()}")
Sample Model
This program creates a dataset from feature and label tensors, shuffles the data, groups it into batches of 2, and prints each batch.
TensorFlow
import tensorflow as tf # Create tensors for features and labels features = tf.constant([[5.0, 10.0], [15.0, 20.0], [25.0, 30.0]]) labels = tf.constant([1, 0, 1]) # Create dataset from tensors dataset = tf.data.Dataset.from_tensor_slices((features, labels)) # Shuffle and batch the dataset dataset = dataset.shuffle(buffer_size=3).batch(2) # Iterate and print batches for batch_features, batch_labels in dataset: print("Batch features:", batch_features.numpy()) print("Batch labels:", batch_labels.numpy())
OutputSuccess
Important Notes
All tensors must have the same first dimension size (number of samples).
Using shuffle helps the model learn better by mixing data order.
Batching groups data into smaller sets for efficient training.
Summary
Dataset from tensors lets you turn arrays into data pipelines for TensorFlow.
It slices tensors row-wise to create dataset elements.
You can shuffle and batch datasets to prepare data for training.