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.

Updated on: 18-Jan-2021

247 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements