Learn Before
Concept

Tensor to NumPy Array Conversion

Converting a deep learning tensor to a NumPy array (ndarray), or vice versa, is straightforward but involves framework-specific memory management behaviors. In PyTorch, the converted objects share the same underlying memory (e.g., via X.numpy() and torch.from_numpy()), meaning an in-place modification to one affects the other. Conversely, in frameworks like TensorFlow (via X.numpy() and tf.constant()), MXNet (via X.asnumpy() and np.array()), and JAX (via jax.device_get() and jax.device_put()), the converted result does not share memory. This isolation prevents the CPU or GPU from halting computations while waiting for Python's NumPy package to interact with the shared memory chunk.

0

1

Updated 2026-05-01

Contributors are:

Who are from:

Tags

D2L

Dive into Deep Learning @ D2L