What is a dataloader in pytorch

This recipe explains what is a dataloader in pytorch

Recipe Objective

What is a dataloader in pytorch?

As the name suggest Dataloader is nothing but a class for pytorch data loading utility. The important argument for the dataloader is nothing but the dataset from which the data is going to be imported. There two different types of datasets map-style datasets and iterable-style datasets.

Build a Job Winning Data Science Portfolio with Solved End-to-End Data Science Projects.

Step 1 - Import library

import torch
import matplotlib.pyplot as plt
from torchvision import datasets, transforms

Step 2 - Transform for normalization

trans_norm = transforms.Compose([transforms.ToTensor(),transforms.Normalize((0.5,), (0.5,))])

Step 3 - Load data using dataloader

train_dataset = datasets.MNIST('~/.pytorch/MNIST_data/', download=True, train=True, transform=trans_norm)
loader_train = torch.utils.data.DataLoader(train_dataset, batch_size=64, shuffle=True)

Step 4 - Access the images

iter_func = iter(loader_train)
image_data, label_data = iter_func.next()
print("This is the size of image data",image_data.shape)
print("This is the size of labels data",label_data.shape)
plt.imshow(image_data[1].numpy().squeeze(), cmap='Greys_r')

What Users are saying..

profile image

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Isolation Forest Model and LOF for Anomaly Detection in Python
Credit Card Fraud Detection Project - Build an Isolation Forest Model and Local Outlier Factor (LOF) in Python to identify fraudulent credit card transactions.

Build an Image Segmentation Model using Amazon SageMaker
In this Machine Learning Project, you will learn to implement the UNet Architecture and build an Image Segmentation Model using Amazon SageMaker

Build a Similar Images Finder with Python, Keras, and Tensorflow
Build your own image similarity application using Python to search and find images of products that are similar to any given product. You will implement the K-Nearest Neighbor algorithm to find products with maximum similarity.

Build Regression (Linear,Ridge,Lasso) Models in NumPy Python
In this machine learning regression project, you will learn to build NumPy Regression Models (Linear Regression, Ridge Regression, Lasso Regression) from Scratch.

Build Time Series Models for Gaussian Processes in Python
Time Series Project - A hands-on approach to Gaussian Processes for Time Series Modelling in Python

Build a Text Classification Model with Attention Mechanism NLP
In this NLP Project, you will learn to build a multi class text classification model with attention mechanism.

Langchain Project for Customer Support App in Python
In this LLM Project, you will learn how to enhance customer support interactions through Large Language Models (LLMs), enabling intelligent, context-aware responses. This Langchain project aims to seamlessly integrate LLM technology with databases, PDF knowledge bases, and audio processing agents to create a comprehensive customer support application.

Image Classification Model using Transfer Learning in PyTorch
In this PyTorch Project, you will build an image classification model in PyTorch using the ResNet pre-trained model.

Build CNN for Image Colorization using Deep Transfer Learning
Image Processing Project -Train a model for colorization to make grayscale images colorful using convolutional autoencoders.