C.W.K.
Stream
Lesson 05 of 11 · published

Reshape, View, Permute, 그리고 Contiguous 함정

~14 min · reshape, view, permute, contiguous, stride

Level 0Tensor 호기심
0 XP0/62 lessons0/13 achievements
0/120 XP to next level120 XP to go0% complete

data 가 보이는 모양 만 바꾸는 세 op

이 op 들 중 어느 것도 byte 를 옮기지 않아 (옮겨야 하면 빼고). stride 만 바꿔 — PyTorch 가 메모리를 걷는 recipe. stride 이해는 tensor 를 알기와 tensor 를 사용하기의 차이.

네 op

  • reshape(*shape) — 가장 유연. memory 가 허락하면 view, 아니면 copy. 항상 작동.
  • view(*shape) — view only. tensor 가 contiguous 아니면 에러.
  • transpose(d0, d1) / .T — 두 dim swap. 항상 view 반환, 결과는 non-contiguous.
  • permute(*dims) — 모든 dim reorder. view, non-contiguous.

contiguous 함정이 가장 흔한 미묘한 PyTorch 버그야. transpose 나 permute 후 tensor 는 메모리에 non-contiguous 하게 놓여. 일부 op (특히 .view() 와 많은 옛 custom CUDA kernel) 가 contiguous 메모리 요구. 해결: .contiguous() (새 contiguous block 으로 copy) 또는 .view() 대신 .reshape() 사용.

크기-1 dim 추가/제거

unsqueeze(dim)dim 위치에 size-1 dim 추가. squeeze(dim=None) 은 size-1 dim 제거. 둘 다 순수 metadata 변경 — copy 없음. broadcast 위해 shape 맞추는 데 끊임없이 필요해 (row vs column vector 상황).

Code

Reshape vs view — 언제 어느 걸·python
import torch

t = torch.arange(12)

# reshape: most flexible. Always works.
a = t.reshape(3, 4)
b = t.reshape(2, -1)   # -1 means "infer this dim" → 2 x 6
c = t.reshape(-1, 3)   # → 4 x 3

# view: identical to reshape for contiguous tensors, errors otherwise.
v = t.view(3, 4)

# Rule of thumb: use reshape unless you specifically need the
# 'fail loudly when non-contiguous' guarantee that view gives you.
Transpose, permute, contiguous fix·python
import torch

x = torch.randn(2, 3, 4)   # (batch=2, seq=3, features=4)

# 2D transpose shorthand
m = torch.randn(3, 4)
mt = m.T            # equivalent to m.transpose(0, 1)
print(mt.is_contiguous())  # False!

# permute reorders ALL dims
y = x.permute(0, 2, 1)     # (2, 4, 3)
print(y.is_contiguous())   # False

# y.view(-1)  # RuntimeError: view size is not compatible
y_flat = y.contiguous().view(-1)  # works
y_flat = y.reshape(-1)             # also works (reshape handles it)

# Image format conversion: NHWC → NCHW
img = torch.randn(8, 224, 224, 3)
img_pytorch = img.permute(0, 3, 1, 2).contiguous()
print(img_pytorch.shape)   # torch.Size([8, 3, 224, 224])
unsqueeze, squeeze, broadcasting setup·python
import torch

v = torch.tensor([1, 2, 3])     # shape (3,)
v.unsqueeze(0).shape            # torch.Size([1, 3]) — row vector
v.unsqueeze(1).shape            # torch.Size([3, 1]) — column vector
v[None, :].shape                # same as unsqueeze(0)
v[:, None].shape                # same as unsqueeze(1)

# squeeze removes size-1 dims
x = torch.randn(1, 3, 1, 4)
x.squeeze().shape               # torch.Size([3, 4])
x.squeeze(0).shape              # torch.Size([3, 1, 4]) — only dim 0
x.squeeze(2).shape              # torch.Size([1, 3, 4]) — only dim 2

External links

Exercise

(8, 3, 32, 32) 4D image batch 잡기. permute 로 NHWC layout 으로 변환, non-contiguous 확인, NCHW 로 다시 변환. (32, 3, 224, 224) tensor 에서 'permute then view' (with contiguous) 와 'permute then reshape' 둘 다 시간 측정.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.