torch.Tensor.numpy¶
- Tensor.numpy(*, force=False) numpy.ndarray ¶
将 tensor 返回为 NumPy
ndarray
。如果
force
为False
(默认值),则仅当 tensor 在 CPU 上、不计算梯度、没有设置共轭位、并且是 NumPy 支持的 dtype 和 layout 时,才执行转换。返回的 ndarray 和 tensor 将共享它们的 storage,因此对 tensor 的更改将反映在 ndarray 中,反之亦然。如果
force
为True
,则相当于调用t.detach().cpu().resolve_conj().resolve_neg().numpy()
。如果 tensor 不在 CPU 上,或设置了共轭位或负号位,tensor 将不会与返回的 ndarray 共享其 storage。将force
设置为True
可以作为一种有用的简写方式。- 参数
force (bool) – 如果为
True
,则 ndarray 可能是 tensor 的副本而不是始终共享内存,默认为False
。