Lists are one of the most commonly used data structures in Python. They are mutable, ordered, and can contain elements of different data types.
Dictionaries are collections that store key-value pairs. The keys must be unique.
Tuples are similar to lists but are immutable (cannot be changed).
Sets are unordered collections of unique elements.
In Python, the for loop is used to iterate over a collection.
The while loop runs as long as a specified condition is true.
Iterators are objects that allow you to access the elements of a collection one at a time.
Generators are a special type of iterator that generate values on the fly, which is memory efficient for large datasets.
Functions allow you to create reusable blocks of code.
*args accepts a variable number of positional arguments, while **kwargs accepts a variable number of keyword arguments.
Lambda functions are anonymous functions defined in a single line.
Classes are structures that bundle data and behavior together.
Inheritance allows one class to inherit attributes and methods from another class.
Special methods in Python classes, which start and end with double underscores, define special behaviors.
A Dataset is an abstract class representing a dataset and providing data access. In PyTorch, custom datasets are created by subclassing torch.utils.data.Dataset
.
A DataLoader is a helper class that loads data from a Dataset in batches. It also provides features like shuffling and parallel loading.
The collate function allows you to customize how the data in a batch is combined.
In PyTorch, neural networks are created by subclassing torch.nn.Module
.
The forward function defines the forward pass of the neural network. It determines how the data is processed as it flows through the network.
forward()
function is automatically called when using module(input)
. That is, model(x)
actually calls model.forward(x)
.
Steps in the forward function:
self.fc1(x)
F.relu(...)
Thanks to PyTorch's automatic differentiation, a computational graph is created during the forward pass, which is used for backpropagation.
All our neural network models in PyTorch are derived from the nn.Module
class. This inheritance provides: