Question 1: Short answer
mediumAI-checkedDeep Learning FrameworksA training loop emulates a batch of 4 examples with two micro-batches of 2, calling backward()
on each before a single optimizer step:
import torch
w = torch.tensor(1.0, requires_grad=True)
x = torch.tensor([1.0, 2.0, 3.0, 4.0])
for chunk in x.split(2): # two micro-batches of 2 examples
loss = ((w * chunk) ** 2).mean()
loss.backward()
print(w.grad)
What number does the last line print?