Tensors Talk to NumPy
Convert between torch tensors and NumPy arrays.
Tensors Talk to NumPy is a free Deep Learning Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Deep Learning Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Two Worlds, One Bridge
Lots of data starts life as a NumPy array. PyTorch makes it easy to move between arrays and tensors in both directions.
NumPy Arrays Are Like Tensors
A NumPy array also stores numbers in a shape, just without GPU support or autograd. Tensors add those superpowers.
import numpy as np
a = np.array([1, 2, 3])
print(a.shape) # (3,)From NumPy with from_numpy
Turn an array into a tensor with torch.from_numpy. It is the standard entry point for existing NumPy data.
a = np.array([1, 2, 3])
t = torch.from_numpy(a)
print(t) # tensor([1, 2, 3])Back to NumPy with .numpy
Going the other way, call .numpy on a tensor. Great for plotting or handing data to other libraries.
t = torch.tensor([1, 2, 3])
a = t.numpy()
print(type(a)) # numpy.ndarrayThey Share the Same Memory
By default the array and tensor share one block of memory. Changing one quietly changes the other.
a = np.array([1, 2, 3])
t = torch.from_numpy(a)
a[0] = 99
print(t[0]) # tensor(99)Copy to Break the Link
Want independent data? Call .clone on the tensor so edits stay separate from the original array.
a = np.array([1, 2, 3])
t = torch.from_numpy(a).clone()
a[0] = 99
print(t[0]) # tensor(1)Dtypes Carry Across
The dtype follows the data over the bridge. A float64 array becomes a float64 tensor unless you cast it.
a = np.array([1.0, 2.0])
t = torch.from_numpy(a)
print(t.dtype) # torch.float64Watch the Float64 Trap
NumPy defaults to float64, but models want float32. Cast after conversion to avoid mismatched dtype errors.
a = np.array([1.0, 2.0])
t = torch.from_numpy(a).float()
print(t.dtype) # torch.float32GPU Tensors Need a Trip Home
You can't call .numpy on a GPU tensor directly. Move it back with .cpu() first, then convert.
t = torch.tensor([1, 2, 3])
a = t.cpu().numpy()
print(a) # [1 2 3]Detach Before Converting Grads
If a tensor tracks gradients, call .detach before .numpy so you grab the values without the autograd graph.
t = torch.tensor([1.0], requires_grad=True)
a = t.detach().numpy()
print(a) # [1.]A Clean Conversion Recipe
The safe pattern for any tensor is detach then cpu then numpy. It works whether or not grads or GPUs are involved.
t = torch.tensor([1.0, 2.0])
a = t.detach().cpu().numpy()
print(a) # [1. 2.]Quick Check
One question about the array and tensor connection.
Recap: Tensors Talk to NumPy
You can bridge both ways with from_numpy and .numpy, watch the shared-memory trap, and use detach-cpu-numpy for a safe export. 🌉
Frequently asked questions
Is the “Tensors Talk to NumPy” lesson free?
Yes — the full text of “Tensors Talk to NumPy” is free to read here on the web, and the Deep Learning Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Deep Learning Academy course, upgrade to CoddyKit PRO.
What will I learn in “Tensors Talk to NumPy”?
Convert between torch tensors and NumPy arrays. You practise Deep Learning Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Deep Learning Academy?
No prior experience is required. Deep Learning Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Tensors Talk to NumPy” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Deep Learning Academy lesson?
Yes. Every Deep Learning Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.