Pytorch Lessons - Basics imports

I will start the Pytorch Series with interview-like formatted questions for us. Here, we will see questions on tutorials. Tutorial link
Usually, common libraries used are:
import torch
import torchvision
import torch.nn as nn
import numpy as np
import torchvision.transforms as transforms
What are these and why use?
1. torch
โ PyTorch Core Library
It's widely used for deep learning and tensor computation, similar to NumPy, but with GPU acceleration.
Common Uses:
Tensor creation and operations (like NumPy arrays but faster with GPU)
Defining and training neural networks
Autograd (automatic differentiation)
Optimizers for training models
2. torchvision
โ Vision-Specific Utilities for PyTorch
orchvision
is a library that provides computer vision utilities like:
Pretrained models (ResNet, AlexNet, etc.)
Image datasets (CIFAR10, MNIST, etc.)
Image transforms and data loaders
Common Uses:
Load and preprocess image data
Use pre-trained vision models for transfer learning
3. torch.nn
โ Neural Network Module
torch.nn
provides classes to help build neural networks easily. You define models as subclasses of nn.Module
.
๐น Common Uses:
Define layers like
nn.Linear
,nn.Conv2d
,nn.ReLU
, etc.Create custom neural networks
4. torchvision.transforms
โ Data Transformations
transforms
are used to modify and preprocess image data before feeding it to the model.
๐น Common Uses:
Convert images to tensors
Normalize image pixels
Resize, crop, rotate, and augment images
5. numpy
โ Numerical Python
Common Uses:
Matrix operations
Data manipulation and numerical analysis
Integration with PyTorch (converting between NumPy arrays and tensors)
Subscribe to my newsletter
Read articles from Vasu Soni directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
