PyTorch basics: tensors, autograd and MLP training

Learn PyTorch tensor operations, autograd and custom data loading, then train a small MLP for binary classification in Google Colab.

Player not loading? Watch on YouTube

This English tutorial from NPTEL - Indian Institute of Science, Bengaluru teaches PyTorch basics through worked examples. It assumes familiarity with Python and earlier NumPy exercises. The presenter uses hosted Google Colab, selects a Tesla T4 runtime and checks CUDA availability with a CPU fallback. Local installation is outside the lesson.

The tensor examples cover creation, shapes, data types, indexing and slicing. The presenter demonstrates reshape and view, adds and removes singleton dimensions, and changes dimension order with permute. Arithmetic, matrix multiplication and device transfers follow. In the demonstrated GPU workflow, the model and its input batches use the same device.

Autograd begins with scalar and vector derivatives. A manual linear regression example shows mean squared error, backward calls, parameter updates under torch.no_grad and gradient clearing. The presenter then implements regression with nn.Module, nn.Linear and SGD. Later examples introduce ReLU, sigmoid, tanh, softmax and cross-entropy loss. Cross-entropy receives logits directly.

A custom Dataset contains 100 synthetic examples with two features. Labels depend on whether the feature sum exceeds zero. A DataLoader supplies shuffled batches of 16. The final MLP has two hidden layers of 32 units, ReLU activations and two output logits. It trains with cross-entropy loss and Adam at a learning rate of 0.001 for 20 epochs.

The presenter reports training accuracy around 98% near epoch 15. The lesson does not show evaluation on held-out samples. CNN and RNN implementations are reserved for the next segment.