This is the heart of Neural Network training.
# 1. Forward Pass
output = model(input)
# 2. Compute Loss
loss = criterion(output, target)
# 3. Backward Pass (Compute Gradients)
loss.backward()
# 4. Update Weights
optimizer.step()How we calculate gradients for weights deep in the network.
# Gradient for weight w1 (deep layer)
# dLoss/dw1 = dLoss/dOutput * dOutput/dHidden * dHidden/dw1
grad_w1 = (error) * (weight_2) * (input)