(I reformated the code with ``` to make it easier to read)
The code looks fine.
The final torch.mean() is not very useful as l1+l2 is already a single number.
Could you elaborate on the “errors” you have?