Status: stub — scaffold cells only; expand before relying on this notebook for a course offering.
Fit y = sin(x) with a small MLP when PyTorch is available.
try:
import torch
from torch import nn
x = torch.linspace(-3, 3, 200).unsqueeze(1)
y = torch.sin(x)
model = nn.Sequential(nn.Linear(1, 32), nn.Tanh(), nn.Linear(32, 1))
opt = torch.optim.Adam(model.parameters(), lr=1e-2)
for _ in range(1000):
opt.zero_grad()
loss = ((model(x) - y) ** 2).mean()
loss.backward()
opt.step()
print('torch loss', float(loss))
except ImportError:
print('PyTorch not installed in this env — use course_core with pytorch.')