Unit 1 of 8 · about 3 hours · explain, implement
PyTorch tensors: creation, indexing, storage and strides
Every later failure in this stage, from silent dtype promotion to aliasing bugs, starts as a wrong picture of what a tensor owns. Storage, stride and device are that picture.
You'll be able to
- Create tensors with explicit dtype and device, and predict shape, dtype and device after indexing, concatenation and device moves.
- Compute a storage offset from shape, stride and index, and classify a tensor as contiguous, transposed or sliced from its stride.
- Predict when a torch tensor and a NumPy array share memory, and when an op copies instead.
Covers
Read
In this order, only the parts listed
0 of 2 done
- Tensors: initialization, attributes, indexing and NumPy sharingPyTorch - Learn the Basics
Initializing a Tensor; Attributes of a Tensor; Operations on Tensors through Single-element tensors; Bridge with NumPy
- Tensor storage, strides and transpose sharingDeep Learning with PyTorch, 2nd edition - framework foundations
Book Sections 3.8 and 3.9 (printed pp. 55-64): storage, offset, size and stride, transposing without copying, contiguous tensors. Notebook cells: Cells using
points.storage(),storage_offset(),stride(), the transpose storage-identity check,is_contiguous()andcontiguous()Use only the storage, stride, transpose and contiguity cells. Skip the named-tensor notebook; named dimensions are out of scope here. The book calls
points.storage(); on PyTorch 2.14 that warns thatTypedStorageis deprecated, so useuntyped_storage()ordata_ptr()for the identity checks.
Focus on
- dtype is inferred at creation and the device is CPU unless you choose one.
Tensor.to()returns a tensor (the same one when nothing changes), so assign the result.Module.to()is the exception: it converts the module in place. - Element offset is
storage_offsetplus the sum over dims of index times stride. Transpose and slicing change stride and offset, not storage. - Basic indexing gives views.
.numpy()andtorch.from_numpy()share one CPU allocation, so in-place edits leak across the boundary. contiguous()returns the same tensor when already contiguous and copies otherwise. Never branch onreshapereturning a view.
Common mistakes · 3
- Reading transpose or permute as a copy. They return views, so writing through them changes the base.
- Trusting reshape to return a view. It may copy; check
data_ptrinstead of assuming. - Calling
x.to(device)on a tensor and then usingx. The call returns the moved tensor;xstays where it was. (model.to(device)does move the module in place, which is why the habit forms.)
Try it yourself
Closed-book, before checking anything
x = torch.zeros(3, 4). Write the shape, stride, storage_offset and is_contiguous of x, xt = x.t() and y = x[::2]. Show that xt[2][1] and x[1][2] name the same element, and assert the shared storage with data_ptr.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.
x = torch.zeros(3, 4). What are the strides ofxand ofx.t(), and is either contiguous?Answerxhas stride(4, 1)and is contiguous.x.t()has shape(4, 3)and stride(1, 4); it shares storage and is not contiguous.2.Which element does
xt[2][1]alias, and at what storage offset?Answerx[1][2], at offset from a zerostorage_offset.3.
t = torch.ones(5);n = t.numpy();t.add_(1). What isnnow, and why?AnswerAll twos. The tensor and the array share one CPU allocation, so the in-place add is visible through both.
4.When does
c = t.contiguous()copy, and when does it returntitself?AnswerIt copies only when
tis non-contiguous. On a contiguoustit returns the same tensor.
Practice
- Learn PyTorch 00: tensor creation, matmul, seeds and squeeze (tasks 2 to 10)
Exercises, tasks 2 to 10:
(7, 7)and(2, 3)creation, matmul with transpose,manual_seedreplays, GPU seed lookup, argmax and argmin, squeeze of(1, 1, 1, 10)Write the tasks in your own script, not in the solutions notebook. Task 1 is background reading; tasks 2 to 10 are the checked work. Run the GPU tasks on whatever device you have and say which one you used.
You're done when
Your written shape, stride and offset table for x, x.t() and x[::2] matches data_ptr and is_contiguous checks, and tasks 2 to 10 run without the solutions notebook.
Only when you can do this without notes.
Leads to
Notes
Resources · 2 for this unit, 5 more in the section
- PyTorch - Learn the BasicsPyTorch · primary
Complete Tensors, Datasets and DataLoaders, Transforms, Build Model, Autograd, Optimization, and Save and Load Model. Rebuild each essential example in a small src package instead of retaining a tutorial notebook.
- Deep Learning with PyTorch, 2nd edition - framework foundationsHoward Huang, Luca Antiga, Eli Stevens and Thomas Viehmann (Manning) · support
Read Sections 3.8-3.9 and 5.5 when tensor storage, strides or autograd need a second explanation, with the Chapter 3 and 5 companion notebooks for runnable cells. Inspect the Chapter 4 and 7 code only for a diagnosed data-representation or loading gap.
Also in this section
- PyTorch - Autograd mechanics, serialization and reproducibilityPyTorch · required
Read Autograd mechanics, Serialization semantics and Reproducibility. Test leaf behavior, saved tensors, grad modes, state dictionaries and the exact tolerance promised by one interrupted and resumed run.
- Learn PyTorch - selected framework exercisesDaniel Bourke · required
Complete exercise 00, tasks 1-5 in 01, tasks 1-7 in 02, tasks 5-13 in 03, tasks 1-3 in 05, and exercises 1-2 in 07. Recreate solutions independently and verify behavior against current official docs.
- NeetCode ML - PyTorch closing drillsNeetCode · optional
After the module gate, solve Pytorch Basics, Layer Normalization, Batch Normalization and RMS Normalization cold. Explain shapes, state and train-evaluation behavior aloud. These do not replace the package and resume test.
- PyTorch Automatic Mixed Precision recipe - Saving/ResumingPyTorch · reference
Read the Saving/Resuming section: save
scaler.state_dict()with the model and optimizer state, at the start of an iteration or afterscaler.update(), and how to resume with or without AMP. - Hands-On Machine Learning with Scikit-Learn and PyTorchAurelien Geron (O'Reilly, 2025) · support
Read Chapter 10, Implementing Mini-Batch Gradient Descent Using DataLoaders and Model Evaluation, for the train and evaluate loops and how batch metrics must be aggregated.