- Deeplearning AI
- Pytorch book
- PyTorch for Deep Learning Bootcamp
- Pytorch popular transfomers
- OCR for Latex
PyTorch 4 most import modules: [torch.nn](https://pytorch.org/docs/stable/nn.html)
, [torch.optim](https://pytorch.org/docs/stable/optim.html)
, [torch.utils.data.Dataset](https://pytorch.org/docs/stable/data.html#torch.utils.data.Dataset)
and [torch.utils.data.DataLoader](https://pytorch.org/docs/stable/data.html)
.
- Forward pass - The model goes through all of the training data once, performing its
forward()
function calculations (model(x_train)
). - Calculate the loss - The model's outputs (predictions) are compared to the ground truth and evaluated to see how wrong they are (
loss = loss_fn(y_pred, y_train
). - Zero gradients - The optimizers gradients are set to zero (they are accumulated by default) so they can be recalculated for the specific training step (
optimizer.zero_grad()
). - Perform backpropagation on the loss - Computes the gradient of the loss with respect for every model parameter to be updated (each parameter with
requires_grad=True
). This is known as backpropagation, hence "backwards" (loss.backward()
). - Step the optimizer (gradient descent) - Update the parameters with
requires_grad=True
with respect to the loss gradients in order to improve them (optimizer.step()
).
- Common Layer Types
- Linear Layers
- Convolutional Layers
- Recurrent Layers
- Transformers
build custom datasets
- Subclass
torch.utils.data.Dataset
. - Initialize our subclass with a
targ_dir
parameter (the target data directory) andtransform
parameter (so we have the option to transform our data if needed). - Create several attributes for
paths
(the paths of our target images),transform
(the transforms we might like to use, this can beNone
),classes
andclass_to_idx
(from ourfind_classes()
function). - Create a function to load images from file and return them, this could be using
PIL
or[torchvision.io](https://pytorch.org/vision/stable/io.html#image)
(for input/output of vision data). - Overwrite the
__len__
method oftorch.utils.data.Dataset
to return the number of samples in theDataset
, this is recommended but not required. This is so you can calllen(Dataset)
. - Overwrite the
__getitem__
method oftorch.utils.data.Dataset
to return a single sample from theDataset
, this is required.
https://poloclub.github.io/cnn-explainer/
from torchinfo import summary
Freezing the base model and changing the output layer
weights = torchvision.models.EfficientNet_B0_Weights.DEFAULT # .DEFAULT = best available weights
model = torchvision.models.efficientnet_b0(weights=weights).to(device)
# Freeze all base layers in the "features" section of the model (the feature extractor) by setting requires_grad=False
for param in model.features.parameters():
param.requires_grad = Falsec
# Get the length of class_names (one output unit for each class)
output_shape = len(class_names)
# Recreate the classifier layer and seed it to the target device
model.classifier = torch.nn.Sequential(
torch.nn.Dropout(p=0.2, inplace=True),
torch.nn.Linear(in_features=1280,
out_features=output_shape, # same number of output units as our number of classes
bias=True)).to(device)