Before your second loop you are zeroing the gradients of weights and don’t use weights anymore after it.
How should the weight gradient be calculated? Do you want to keep the last gradient of weights and sum it to the new loss?