I’ve fixed some minor issues in the code (moved the optimizer before the for loop to avoid recreation etc.) and your code seems to work fine in most runs. Sometimes, probably due to bad init values, the loss exploded, but in most cases your experiment seems to work.