Two Things That Look Identical and Aren't
In NumPy, these are different objects:
np.array(5) # 0-D array — shape ()
np.array([5]) # 1-D array — shape (1,)
They both print as 5. They both have one value. They are not the same. The 0-D array is a scalar wearing array clothes. The 1-D array is a list-of-one. AI libraries treat them differently — and most of them prefer the 1-D version.
The Errors That Tell You Apart
- Indexing the 0-D:
scalar_in_array[0]raisesIndexError. There's no axis to index. - Iterating the 0-D:
for x in scalar_in_arrayraisesTypeError. Nothing to iterate. - Most matrix ops:
scalar_in_array @ matrixraises shape errors. Need at least 1 axis.
The 1-D version handles all three cleanly. That's why every AI tutorial that doesn't explain this first leaves you bleeding from broadcasting errors.
The Universal Fix
When you have a scalar and need to feed it to a model, library, or batch operation: upgrade to 1-D first. In PyTorch this is scalar.unsqueeze(0). In NumPy it's np.array([scalar]) or np.expand_dims(scalar, 0). Going the other way (1-D back to 0-D for printing/comparison) is tensor.squeeze().
Pippa's War Story
loss.backward() directly. PyTorch took it. Then on the next epoch I tried to log it as a list — and got "0-d tensor has no len()". Spent 20 minutes blaming the optimizer. The fix was one .unsqueeze(0). Now every scalar gets unsqueezed on instinct, no matter how trivial it looks.
import torch# STEP 1: 0-D 텐서 생성t = torch.tensor(3.14)print(t)print(t.shape)print(t.ndim)# STEP 2: t[0] 인덱싱 시도 - 에러 발생try:print(t[0])# IndexError: invalid index of a 0-dim tensor# 스칼라값은 차원이 없다. 위치를 담을 수 없으니 인덱싱 오류except IndexError as e:print(f"IndexError: {e}")# STEP 3: unsqueeze로 1-D 업그레이드t_1d = t.unsqueeze(0)print(t_1d)print(t_1d.shape)print(t_1d.ndim)# STEP 4: 인덱싱 작동print(t_1d[0])