Title: Lab 12 — Build and Train a Tiny PyTorch Neural Network
Course: AIINFRA 100: AI Systems & Development Environments > Section: Week 12 — Deep Learning Tooling: PyTorch

🛠️ Lab 12 — Build and Train a Tiny PyTorch Neural Network (50 pts)

Goal: Use a free Google Colab notebook to create tensors, confirm GPU availability, build and train a tiny one-hidden-layer neural network with autograd and SGD, and then move that same model to the GPU to see device-awareness in action. Steps:

1. Go to Google Colab and create a new notebook. Enable a GPU runtime: Runtime \> Change runtime type \> GPU, then save.
2. In the first code cell, import torch and create a few tensors — for example, a 1D tensor of numbers and a 2D tensor — and run basic math on them (addition, multiplication). Print their shape and dtype to confirm they behave like arrays.
3. Run torch.cuda.is_available() and print the result. If it prints True, also print the GPU name using torch.cuda.get_device_name(0).
4. Create a toy dataset: generate x values (e.g., 100 random numbers) and corresponding y values following y = 2x + 1 plus a small amount of random noise, so the relationship is realistic but not perfect.
5. Build a tiny neural network with one hidden layer as an nn.Module subclass (an nn.Linear layer, an activation like nn.ReLU, and a final nn.Linear output layer).
6. Set up an nn.MSELoss() loss function and an optimizer such as torch.optim.SGD(model.parameters(), lr=0.01).
7. Write the standard training loop and run it for about 100 epochs: forward pass → compute loss → optimizer.zero_grad() → loss.backward() → optimizer.step(). Print the loss every 10 epochs so you can see it decrease.
8. Now set device = torch.device('cuda' if torch.cuda.is_available() else 'cpu'), move your model with model.to(device) and your data tensors with .to(device), and re-run training to confirm everything still works on the GPU.
9. Add short markdown notes in your notebook (2–3 sentences each) explaining: (a) what you observed when checking for GPU availability, and (b) what happened to the loss over training.

Deliverables: Submit your Colab notebook (share link or downloaded .ipynb/PDF) showing all code cells run with visible output, including the printed loss values and the GPU availability check, plus your short reflection notes.