perceptron1:

have loss.backward(retain_graph=True).


Are you sure you actually need that? I’m asking because many users are tempted to enable it prematurely by that error message.
Best regards
Thomas