Shapes, Dtypes & Indexing
Read a tensor's shape and slice into it.
Shapes, Dtypes & Indexing is a free Deep Learning Academy lesson on CoddyKit — lesson 1 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.
Tensors Are Just Numbers in a Grid
A tensor is a box of numbers arranged in rows, columns, or deeper. Everything in deep learning flows through them.
Shape Describes the Layout
The shape tells you how many values sit along each dimension. Read it like dimensions of a box: rows by columns. 📦
Check Shape with .shape
Ask any tensor for its .shape and PyTorch hands you the size of every dimension in order.
import torch
x = torch.tensor([[1, 2, 3], [4, 5, 6]])
print(x.shape) # torch.Size([2, 3])Dimensions Count with .ndim
The number of dimensions is the tensor's rank. A scalar is 0, a vector is 1, a matrix is 2, and images go higher.
x = torch.tensor([[1, 2], [3, 4]])
print(x.ndim) # 2Every Tensor Has a Dtype
The dtype is the kind of number stored, like float32 or int64. It controls precision and which math is allowed.
x = torch.tensor([1.0, 2.0, 3.0])
print(x.dtype) # torch.float32Float32 Is the Default for Learning
Most networks train in float32 because gradients need decimals. Integers can't hold the tiny fractional updates.
Cast Between Dtypes
Change a tensor's type with .to() or .float(). This is how you match a model's expected precision.
x = torch.tensor([1, 2, 3])
y = x.to(torch.float32)
print(y.dtype) # torch.float32Indexing Grabs One Value
Indexing uses square brackets to pull out a single element by its position, counting from zero.
x = torch.tensor([10, 20, 30])
print(x[0]) # tensor(10)Two Indices for a Matrix
For a 2D tensor, give a row then a column. The order always follows the shape you saw earlier.
x = torch.tensor([[1, 2, 3], [4, 5, 6]])
print(x[1, 2]) # tensor(6)Slicing Takes a Range
Use a colon to grab a whole slice. start:stop keeps everything up to but not including stop.
x = torch.tensor([10, 20, 30, 40])
print(x[1:3]) # tensor([20, 30])Slice Rows and Columns Together
A lone colon means take all of that dimension. This is how you select a whole column across every row.
x = torch.tensor([[1, 2], [3, 4]])
print(x[:, 0]) # tensor([1, 3])Quick Check
Time to test what shape and indexing really mean.
Recap: Shapes, Dtypes & Indexing
You can now read a tensor's shape, check its dtype, and slice into it. These three skills unlock every tensor operation ahead. 🎉
Frequently asked questions
Is the “Shapes, Dtypes & Indexing” lesson free?
Yes — the full text of “Shapes, Dtypes & Indexing” 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 “Shapes, Dtypes & Indexing”?
Read a tensor's shape and slice into it. 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 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Shapes, Dtypes & Indexing” 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.
All lessons in this course
- Shapes, Dtypes & Indexing
- Reshape, View, Squeeze & Unsqueeze
- Broadcasting Rules That Save You Loops
- Tensors Talk to NumPy