Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
How to load CIFAR10 Dataset in Pytorch?
Next article icon

How to use a DataLoader in PyTorch?

Last Updated : 24 Feb, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

Operating with large datasets requires loading them into memory all at once. In most cases, we face a memory outage due to the limited amount of memory available in the system. Also, the programs tend to run slowly due to heavy datasets loaded once. PyTorch offers a solution for parallelizing the data loading process with automatic batching by using DataLoader. Dataloader has been used to parallelize the data loading as this boosts up the speed and saves memory.

The dataloader constructor resides in the torch.utils.data package. It has various parameters among which the only mandatory argument to be passed is the dataset that has to be loaded, and the rest all are optional arguments.

Syntax:

DataLoader(dataset, shuffle=True, sampler=None, batch_size=32)

DataLoaders on Custom Datasets:

To implement dataloaders on a custom dataset we need to override the following two subclass functions: 

  • The _len_() function: returns the size of the dataset.
  • The _getitem_() function: returns a sample of the given index from the dataset.
Python3
# importing the required libraries import torch from torch.utils.data import Dataset from torch.utils.data import DataLoader  # defining the Dataset class class data_set(Dataset):     def __init__(self):         numbers = list(range(0, 100, 1))         self.data = numbers      def __len__(self):         return len(self.data)      def __getitem__(self, index):         return self.data[index]   dataset = data_set()  # implementing dataloader on the dataset and printing per batch dataloader = DataLoader(dataset, batch_size=10, shuffle=True) for i, batch in enumerate(dataloader):     print(i, batch) 

Output:

DataLoaders on Built-in Datasets:

Python3
# importing the required libraries import torch from torch.utils.data import Dataset from torch.utils.data import DataLoader import seaborn as sns from torch.utils.data import TensorDataset  # defining the dataset consisting of  # two columns from iris dataset iris = sns.load_dataset('iris') petal_length = torch.tensor(iris['petal_length']) petal_width = torch.tensor(iris['petal_width']) dataset = TensorDataset(petal_length, petal_width)  # implementing dataloader on the dataset  # and printing per batch dataloader = DataLoader(dataset,                          batch_size=5,                          shuffle=True)  for i in dataloader:     print(i) 

Output:


Next Article
How to load CIFAR10 Dataset in Pytorch?
author
d2anubis
Improve
Article Tags :
  • Python
  • Python-PyTorch
Practice Tags :
  • python

Similar Reads

  • How to use GPU acceleration in PyTorch?
    PyTorch is a well-liked deep learning framework that offers good GPU acceleration support, enabling users to take advantage of GPUs' processing power for quicker neural network training. This post will discuss the advantages of GPU acceleration, how to determine whether a GPU is available, and how t
    7 min read
  • How to Split a Dataset Using PyTorch
    Splitting a dataset is an important step in training machine learning models. It helps to separate the data into different sets, typically training, and validation, so we can train our model on one set and validate its performance on another. In this article, we are going to discuss the process of s
    6 min read
  • PyTorch DataLoader
    PyTorch's DataLoader is a powerful tool for efficiently loading and processing data for training deep learning models. It provides functionalities for batching, shuffling, and processing data, making it easier to work with large datasets. In this article, we'll explore how PyTorch's DataLoader works
    14 min read
  • How to load CIFAR10 Dataset in Pytorch?
    The CIFAR-10 dataset is a popular resource for training machine learning models, especially in the field of image recognition. It consists of 60,000 32x32 color images in 10 different classes, with 6,000 images per class. The dataset is divided into 50,000 training images and 10,000 testing images.
    3 min read
  • How to Use Multiple GPUs in PyTorch
    PyTorch, a popular deep learning framework, provides robust support for utilizing multiple GPUs to accelerate model training. Leveraging multiple GPUs can significantly reduce training time and improve model performance. This article explores how to use multiple GPUs in PyTorch, focusing on two prim
    5 min read
  • How to deploy PyTorch models on Vertex AI
    PyTorch is a freely available machine learning library that can be imported and used inside the code for performing machine learning operations based on requirements. The front-end api is written in Python and the tensor operations are implemented using C++. It is developed by Facebook's AI Research
    12 min read
  • How to Get the Data Type of a Pytorch Tensor?
    In this article, we are going to create a tensor and get the data type. The Pytorch is used to process the tensors. Tensors are multidimensional arrays. PyTorch accelerates the scientific computation of tensors as it has various inbuilt functions. Vector: A vector is a one-dimensional tensor that ho
    3 min read
  • How to Compute Gradients in PyTorch
    PyTorch is a leading deep-learning library that offers flexibility and a dynamic computing environment, making it a preferred tool for researchers and developers. One of its most praised features is the ease of computing gradients automatically, which is crucial for training neural networks. In this
    5 min read
  • How to use PyTorch for sentiment analysis on textual data?
    Sentiment Analysis is a natural language processing (NLP) task that involves identifying the emotion present in a given text. The amount of textual data on social media, consumer reviews, and other platforms is increasing, making sentiment analysis more and more crucial. In this article, we present
    8 min read
  • How to Get the Value of a Tensor in PyTorch
    When working with PyTorch, a powerful and flexible deep learning framework, you often need to access and manipulate the values stored within tensors. Tensors are the core data structures in PyTorch, representing multi-dimensional arrays that can store various types of data, including scalars, vector
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences