8/31
Part 2 of 6 · How A and B startLearn and do

If both started at zero, neither would ever move

Why start only B at zero, and not both?

def grad_sizes(A: torch.Tensor, B: torch.Tensor) -> tuple[float, float]:
    """A: (r, in), B: (out, r). Returns each one's largest first gradient."""
    A, B = A.clone().requires_grad_(), B.clone().requires_grad_()
    (x @ A.T @ B.T).sum().backward()
    return A.grad.abs().max().item(), B.grad.abs().max().item()


torch.manual_seed(0)
rand_A, rand_B = torch.randn(4, 16), torch.randn(12, 4)
zero_A, zero_B = torch.zeros(4, 16), torch.zeros(12, 4)
starts = [("B zero", rand_A, zero_B), ("A zero", zero_A, rand_B),
          ("both zero", zero_A, zero_B)]
for name, A, B in starts:
    dA, dB = grad_sizes(A, B)
    print(f"{name:<10} A gets {dA:5.2f}   B gets {dB:5.2f}")
Predict

Three starts: B zero, A zero, or both zero. Which one gives both factors a zero gradient?

Pick one to see what it prints.
Pick an answer above to see the rest