python
13 lines · 1 tab
Dr. Elena Vasquez
Apr 2026
1 tab
import torch
device = 'cuda' if torch.cuda.is_available() else 'cpu'
features = torch.tensor([[1.0, 2.0], [3.0, 4.0]], requires_grad=True, device=device)
weights = torch.tensor([[0.2], [0.8]], requires_grad=True, device=device)
logits = features @ weights
loss = (logits ** 2).mean()
loss.backward()
print('loss:', loss.item())
print('grad features:', features.grad)
print('grad weights:', weights.grad)
1 file · python
Explain with highlit
I treat PyTorch tensors like the main vocabulary of deep learning work. Understanding device placement, shape semantics, and autograd is more important than memorizing model classes. Once that foundation is solid, debugging training loops gets much easier.
Share this code
Here's the card — post it anywhere.