- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between Dataset.from_tensors and Dataset.from_tensor_slices
Dataset.from_tensors and Dataset.from_tensor_slices are methods in the TensorFlow library that are used to create datasets. Dataset.from_tensor creates a dataset from a single tensor whereas Dataset.from_tensor_slices creates data set by slicing a tensor along the first dimension. In this article, we will understand the difference between the two methods and how they are used in different use cases.
from_tensor |
from_tensor_slices |
---|---|
Creates a dataset from a single tensor |
Creates a dataset by slicing a tensor along the first dimension |
Useful for small datasets that can fit in memory |
Useful for large datasets that cannot be loaded into memory all at once |
Creates a dataset with only one element |
Creates a dataset with multiple elements, one for each slice along the first dimension of the tensor |
Accepts a single tensor |
Accepts a tuple of tensors and slices them together along the first dimension |
Syntax
Dataset.from_tensor(tensor_data)
Here tensor_data is the tensor of any shape with which you want to create a dataset. It returns a tensor dataset where each input tensor is like a row of your dataset and this row be manipulated by performing data manipulation operations.
Example
In the below example we have first imported the tensorflow library and then created a tensor with two elements. The tensor is then converted to the dataset using the from_tensor method.
import tensorflow as tf data = tf.constant([[1, 2], [3, 4]]) dataset = tf.data.Dataset.from_tensors(data) for element in dataset: print(element.numpy())
Output
[[1 2] [3 4]]
Dataset.from_tensor_slices
The Dataset.from_tensor_slices creates a dataset by slicing a dataset along the first dimension of the tensor. The from_tensor_slices method is used when the data size is large and cannot fit in the memory at once. The resulting dataset will have one element for each element in the tensor along the first dimension.
Syntax
Dataset.from_tensor_slices(tensor_data)
Here tensor_data is the tensor of any shape with which you want to create a dataset.It returns a dataset where each input tensor is the column of your data. This Column of data can then be used to perform different operations.
Example
import tensorflow as tf data = tf.constant([[1, 2], [3, 4]]) dataset = tf.data.Dataset.from_tensor_slices(data) for element in dataset: print(element.numpy())
Output
[1 2] [3 4]
Conclusion
In this article we discussed the differences between Dataset.from_tensor method and Dataset.from_tensor_slices method are used to create a dataset from tensor data. The from_tensor method creates a dataset using a single tensor while the from_tensor_slices method creates a dataset by slicing the tensor along the first dimension.