The loop shouldn’t be the problem here.
Could you try to overfit your model on a small subset of your data, e.g. just 10 samples?
If your model cannot overfit these samples nearly perfectly, some other bugs might be in the code.