Jump to content

PyTorch: Difference between revisions

215 bytes added ,  6 October 2020
Line 72: Line 72:
* For non-scalar items, use <code>my_var.detach().cpu().numpy()</code>
* For non-scalar items, use <code>my_var.detach().cpu().numpy()</code>


* <code>detach()</code> deletes the item from the autograd edge
* [https://pytorch.org/docs/stable/autograd.html#torch.Tensor.detach <code>detach()</code> ]deletes the item from the autograd edge.
* <code>cpu()</code> copies the tensor to the CPU
* [https://pytorch.org/docs/stable/tensors.html?highlight=cpu#torch.Tensor.cpu <code>cpu()</code>] moves the tensor to the CPU.
* <code>numpy()</code> returns a numpy view of the tensor
* [https://pytorch.org/docs/stable/tensors.html#torch.Tensor.numpy <code>numpy()</code>] returns the underlying numpy tensor.


When possible, use functions which return new views of existing tensors rather than making duplicates of tensors:
When possible, use functions which return new views of existing tensors rather than making duplicates of tensors: