I really recommend this explorable explanation: https://setosa.io/ev/ordinary-least-squares-regression/
And for actual gradient descent code, here is an older example of mine in PyTorch: https://github.com/stared/thinking-in-tensors-writing-in-pyt...