It seems you might be running out of memory by fixing the issues, i.e. by avoiding to detach the tensor.
You also cannot use item(), as it would also detach the result from the computation graph and would return a plain Python scalar.
To lower the memory usage you could e.g. decrease the batch size etc.