Difference Between Dataset.from_tensors and Dataset.from_tensor_slices
Last Updated :
26 Apr, 2025
In this article, we will learn the difference between from_tensors and from_tensor_slices. Both of these functionalities are used to iterate a dataset or convert a data to TensorFlow data pipeline but how it is done difference lies there. Suppose we have a dataset represented as a Numpy matrix of shape (num_features, num_examples) and we wish to convert it to Tensorflow type tf.data.Dataset.
Difference between Dataset.from_tensors and Dataset.from_tensor_slices
Now we have two methods to do this - Dataset.from_tensors and Dataset.from_tensor_slices.
from_tensors - This method is used to combine several smaller datasets to form a large dataset.
from_tensor_slices - This method is generally used while training machine learning models using data input pipeline. This methods help us to combine the independent features and their target as one dataset.
We will try to understand this one by one using code examples of the same. First of all, the main condition to using the from_tensor_slices is that the dimension of the matrix at the 0th rank must be the same.
Necessary Condition of Shapes in from_tensors and from_tensor_slices
There is a condition to using the from_tensor_slices function but there is no such in the case of from_tensors. The condition is that the input data or tensor's shape must be the same if one wants to use the from_tensor_slices method. This condition is also referred to as the same dimension at the 0th rank of the input matrix.
Python3
import tensorflow as tf
ds1 = (
tf.data.Dataset
.from_tensors((tf.random.uniform([10, 4]),
tf.random.uniform([9])))
)
ds1
Output:
<TensorDataset element_spec=(TensorSpec(shape=(10, 4), dtype=tf.float32, name=None),
TensorSpec(shape=(10,), dtype=tf.float32, name=None))>
Now if we will try to use this same function using the from_tensor_slices method then we will get an error message of incompatibility.
Python3
ds2 = (
tf.data.Dataset
.from_tensor_slices((tf.random.uniform([10, 4]),
tf.random.uniform([9])))
)
ds2
Output:
#ERROR
The above code will give an error because the necessary condition of the same dimension at the 0th rank does not meet.
Way of Combining Input Data in from_tensors & .from_tensor_slices
from_tensors method combine smaller dataset to form a large data set but the from_tensor_slices don't do any such thing. Let's look at this using the below implementation.
Python3
dataset1 = tf.data.Dataset.from_tensors(
[tf.random.uniform([2, 3]), tf.random.uniform([2, 3])])
print(dataset1)
Output:
<TensorDataset element_spec=TensorSpec(shape=(2, 2, 3), dtype=tf.float32, name=None)>
From the shape of the above dataset, we can say that it has combined the two data into a single data.
Python3
dataset2 = tf.data.Dataset.from_tensor_slices(
[tf.random.uniform([2, 3]), tf.random.uniform([2, 3])])
print(dataset2)
Output:
<TensorSliceDataset element_spec=TensorSpec(shape=(2, 3),
dtype=tf.float32, name=None)>
Way of Interpreting Input Data in from_tensors and from_tensor_slices
The next difference lies in the way data is being treated by these two functions.
Python3
t1 = tf.constant([[1, 2], [3, 4]])
ds1 = tf.data.Dataset.from_tensors(t1)
[x for x in ds1]
Output:
[<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[1, 2],
[3, 4]], dtype=int32)>]
Now let's look at the from_tensor_slices output for the same data or input matrix.
Python3
t2 = tf.constant([[1, 2], [3, 4]])
ds2 = tf.data.Dataset.from_tensor_slices(t2)
[x for x in ds2]
Output:
[<tf.Tensor: shape=(2,), dtype=int32, numpy=array([1, 2], dtype=int32)>,
<tf.Tensor: shape=(2,), dtype=int32, numpy=array([3, 4], dtype=int32)>]
From the above output, we can say that from_tensors combines the whole data as a single entity. But the same is not true with from_tensor_slices it creates slices of the input data row-wise. From the above format in which the content of these two datasets has been printed even after the data was the same. We can say that ds1 has shape (2, 2) but in the case of ds2, it is (2, 2, 3).
Similar Reads
Tensorflow | tf.data.Dataset.from_tensor_slices()
With the help of tf.data.Dataset.from_tensor_slices() method, we can get the slices of an array in the form of objects by using tf.data.Dataset.from_tensor_slices() method. Syntax : tf.data.Dataset.from_tensor_slices(list) Return : Return the objects of sliced elements. Example #1 : In this example
1 min read
Difference between detach, clone, and deepcopy in PyTorch tensors
In PyTorch, managing tensors efficiently while ensuring correct gradient propagation and data manipulation is crucial in deep learning workflows. Three important operations that deal with tensor handling in PyTorch are detach(), clone(), and deepcopy(). Each serves a unique purpose when working with
6 min read
Difference Between Scalar, Vector, Matrix and Tensor
In the context of mathematics and machine learning, scalar, vector, matrix, and tensor are all different types of mathematical objects that represent different concepts and have different properties. Here in this article, we will discuss in detail scalars, vectors, matrixes, tensors, and finally the
5 min read
What's the Difference Between torch.stack() and torch.cat() Functions?
Effective tensor manipulation in PyTorch is essential for creating and refining deep learning models. 'torch.stack()' and 'torch.cat()' are two frequently used functions for merging tensors. While they are both intended to combine tensors, their functions are different and have different application
7 min read
Difference Between detach() and with torch.no_grad() in PyTorch
In PyTorch, managing gradients is crucial for optimizing models and ensuring efficient computations. Two commonly used methods to control gradient tracking are detach() and with torch.no_grad(). Understanding the differences between these two approaches is essential for effectively managing computat
6 min read
Difference between Tensor and Variable in Pytorch
In this article, we are going to see the difference between a Tensor and a variable in Pytorch. Pytorch is an open-source Machine learning library used for computer vision, Natural language processing, and deep neural network processing. It is a torch-based library. It contains a fundamental set of
3 min read
Differences between a Matrix and a Tensor
"Matrix" and "Tensor" may seem similar but they serve different purposes and possess distinct characteristics. In this article, weâll explore matrices and tensors. Matrix: A Structured 2-D ArrayA matrix is a two-dimensional array of numbers arranged in rows and columns. Hereâs an example of a 4 \tim
6 min read
Difference between Variable and get_variable in TensorFlow
In this article, we will try to understand the difference between the Variable() and get_variable() function available in the TensorFlow Framework. Variable() Method in TensorFlow A variable maintains a shared, persistent state manipulated by a program. If one uses this function then it will create
1 min read
Difference Between tf.Session() And tf.InteractiveSession() Functions in Python Tensorflow
In this article, we are going to see the differences between  tf.Session() and tf.InteractiveSession(). tf.Session() In TensorFlow, the computations are done using graphs. But when a graph is created, the values and computations are not defined. So a session is used to run the graph. The sessions pl
2 min read
What's the Difference Between Reshape and View in PyTorch?
PyTorch, a popular deep learning framework, offers two methods for reshaping tensors: torch.reshape and torch.view. While both methods can be used to change the shape of tensors, they have distinct differences in their behavior, constraints, and implications for memory usage. This article delves int
5 min read