What happens when we call cpu().data.numpy() on a PyTorch tensor?

Viewed 2425

I am working on a project and need to pass the data in loss tensor to a plotting library.

What happens when I perform this call -> loss.cpu().data.numpy()

Is there a risk of detaching the tensor from the computation graph?

1 Answers

.cpu() copies the tensor to the CPU, but if it is already on the CPU nothing changes.

.numpy() creates a NumPy array from the tensor. The tensor and the array share the underlying memory, therefore if the NumPy array is modified in-place, the changes will be reflected in the original tensor. If you plan to make in-place modifications to the NumPy array, you should generally create a copy of it. In the case where loss was on the GPU, loss.cpu() already creates a copy, hence the in-place modifications would only affect the intermediate CPU tensor, which you aren't using.

Is there a risk of detaching the tensor from the computation graph?

No, the original tensor loss is not affected by this in regards to the computational graph.

Related