I’m not sure where your code is exactly failing, but the error message is raised if you try to convert a tensor with more than a single element to a Python scalar value:
torch.randn(2).item()
> ValueError: only one element tensors can be converted to Python scalars

torch.randn(1).item() # works

PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier.