This note is the practical, hands-on counterpart to Tensors โ every way to actually create, inspect, and convert PyTorch tensors in real code.
Creating Tensors
import torch
torch.tensor([1, 2, 3]) # from a Python list
torch.zeros(3, 4) # a (3,4) tensor of all zeros
torch.ones(2, 2) # a (2,2) tensor of all ones
torch.randn(3, 3) # random values from a standard normal distribution
torch.arange(0, 10, 2) # [0, 2, 4, 6, 8]
torch.linspace(0, 1, 5) # 5 evenly spaced values from 0 to 1
import numpy as np
torch.from_numpy(np.array([1, 2, 3])) # convert a NumPy array to a tensor (shares memory!)
Inspecting a Tensor
x = torch.randn(3, 4)
print(x.shape) # torch.Size([3, 4])
print(x.dtype) # torch.float32 (the default)
print(x.device) # cpu (or cuda:0 if moved to GPU)
print(x.ndim) # 2 -- number of dimensions
The NumPy Conversion Gotcha
torch.from_numpy() shares the underlying memory with the original NumPy array โ modifying one modifies the other. Use .clone() if you need an independent copy:
arr = np.array([1.0, 2.0, 3.0])
t = torch.from_numpy(arr)
t[0] = 99.0
print(arr) # [99. 2. 3.] -- the NumPy array changed too! They SHARE memory
t_independent = torch.from_numpy(arr).clone() # a genuine independent copy
Common Mistakes
- Assuming
torch.from_numpy()creates an independent copy โ it shares memory by default; unexpected mutations in one can silently affect the other. - Creating tensors with an unintended dtype (e.g. integer division producing an integer tensor when a float was expected) โ always check
.dtypewhen results look unexpectedly wrong.
Interview Relevance
Q: "What's a subtle gotcha with converting a NumPy array to a PyTorch tensor via torch.from_numpy()?" The resulting tensor shares the same underlying memory as the original NumPy array โ modifying one modifies the other, since no data is actually copied. Using .clone() (or the newer torch.tensor() constructor, which does copy) is necessary when an independent copy is genuinely needed.
Practice Question
Create a tensor of shape (2, 3) filled with the value 7, without listing every element manually.