Rate this Page

torch.Tensor.numpy#

Tensor.numpy(*,force=False)numpy.ndarray#

Returns the tensor as a NumPyndarray.

Ifforce isFalse (the default), the conversionis performed only if the tensor is on the CPU, does not require grad,does not have its conjugate bit set, and is a dtype and layout thatNumPy supports. The returned ndarray and the tensor will share theirstorage, so changes to the tensor will be reflected in the ndarrayand vice versa.

Ifforce isTrue this is equivalent tocallingt.detach().cpu().resolve_conj().resolve_neg().numpy().If the tensor isn’t on the CPU or the conjugate or negative bit is set,the tensor won’t share its storage with the returned ndarray.Settingforce toTrue can be a useful shorthand.

Parameters

force (bool) – ifTrue, the ndarray may be a copy of the tensorinstead of always sharing memory, defaults toFalse.