
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How can Tensorflow be used to iterate through the dataset and display sample data using Python?
Tensorflow is a machine learning framework that is provided by Google. It is an open-source framework used in conjunction with Python to implement algorithms, deep learning applications and much more. It is used in research and for production purposes. It has optimization techniques that help in performing complicated mathematical operations quickly. This is because it uses NumPy and multi-dimensional arrays. These multi-dimensional arrays are also known as ‘tensors’. The framework supports working with deep neural networks. It is highly scalable and comes with many popular datasets. It uses GPU computation and automates the management of resources. It comes with a multitude of machine learning libraries and is well-supported and documented. The framework has the ability to run deep neural network models, train them, and create applications that predict relevant characteristics of the respective datasets.
The ‘tensorflow’ package can be installed on Windows using the below line of code −
pip install tensorflow
Tensor is a data structure used in TensorFlow. It helps connect edges in a flow diagram. This flow diagram is known as the ‘Data flow graph’. Tensors are nothing but a multidimensional array or a list. They can be identified using three main attributes −
Rank − It tells about the dimensionality of the tensor. It can be understood as the order of the tensor or the number of dimensions in the tensor that has been defined.
Type − It tells about the data type associated with the elements of the Tensor. It can be a one dimensional, two dimensional or n dimensional tensor.
Shape − It is the number of rows and columns together.
We are using Google Colaboratory to run the below code. Google Colab or Colaboratory helps run Python code over the browser and requires zero configuration and free access to GPUs (Graphical Processing Units). Colaboratory has been built on top of Jupyter Notebook.
Example
print("Iterating through the training data") for i, label in enumerate(raw_train_ds.class_names): print("Label", i, "maps to", label) print("The training parameters have been defined") raw_val_ds = preprocessing.text_dataset_from_directory( train_dir, batch_size=batch_size, validation_split=0.25, subset='validation', seed=seed) print("The test dataset is being prepared") test_dir = dataset_dir/'test' raw_test_ds = preprocessing.text_dataset_from_directory( test_dir, batch_size=batch_size)
Code credit − https://www.tensorflow.org/tutorials/load_data/text
Output
Iterating through the training data Label 0 maps to csharp Label 1 maps to java Label 2 maps to javascript Label 3 maps to python The training parameters have been defined Found 8000 files belonging to 4 classes. Using 2000 files for validation. The test dataset is being prepared Found 8000 files belonging to 4 classes.
Explanation
The training data is iterated through.
The number of rows for training, test, and validation set are displayed on the console.
The data is pre-processed using the ‘text_dataset_from_directory’ utility.
- Related Articles
- How can Tensorflow be used to display sample data from abalone dataset?
- How can Tensorflow be used to display sample data from the cats and dogs input dataset?
- How can Tensorflow be used to explore the dataset and see a sample file from the stackoverflow question dataset using Python?
- How can Tensorflow be used to view a sample of the vectorised data using Python?
- How can Tensorflow be used to load the Illiad dataset using Python?
- How can Tensorflow be used to train the Illiad dataset using Python?
- How can Tensorflow be used to visualize the flower dataset using Python?
- How can Tensorflow be used to download and explore the Illiad dataset using Python?
- How can Tensorflow be used to configure the stackoverflow question dataset using Python?
- How can Tensorflow and Python be used to verify the CIFAR dataset?
- How can Tensorflow be used to vectorise the text data associated with stackoverflow question dataset using Python?
- How can Tensorflow be used with Estimators to display metadata about the dataset?
- How can TensorFlow be used to download and explore the Fashion MNIST dataset using Python?
- How can Tensorflow be used with Illiad dataset to check how well the test data performs using Python?
- How can Tensorflow be used to prepare the dataset with stackoverflow questions using Python?
