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

Tensor 가 진짜 뭐냐

~12 min · tensor, ndarray, shape

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

n 차원 array, 거기에 두 가지 super power

deep learning 의 신비주의를 벗기면 tensor 는 그냥 n 차원 array — NumPy ndarray 와 같은 개념이야. PyTorch tensor 가 NumPy 와 다른 두 가지:

  1. Device 투명성. 같은 코드가 인자 하나만 바꾸면 CPU, NVIDIA GPU (CUDA), Apple Silicon GPU (MPS) 위에서 다 돌아.
  2. Autograd. 모든 op 가 graph 에 자기를 기록할 수 있어서 PyTorch 가 나중에 reverse 로 재생해서 gradient 를 계산해.

차원 사다리는 이렇게 읽혀:

  • 0D — scalar: 숫자 하나 — torch.tensor(3.14)
  • 1D — vector: 숫자 한 줄 — torch.tensor([1, 2, 3])
  • 2D — matrix: 표 — 행 × 열
  • 3D: matrix 의 batch — sequence 에 흔함 (batch, seq_len, features)
  • 4D: image batch — PyTorch 의 NCHW convention 에서 (batch, channels, height, width)

방 안의 수학자는 'tensor' 가 covariance / contravariance 를 포함한 더 엄격한 의미라고 항의할 거야. deep learning 에선 이 단어를 비공식적으로 다시 빌려서 'autograd 가 붙은 n-dim array' 의 의미로 써. framework, docs, 모든 paper 가 그 의미야. 싸우지 마.

한 가지 부드러운 경고: shape 는 PyTorch 에서 가장 유용한 디버깅 도구야. 뭔가 깨지면 첫 동작은 항상 print(x.shape). 이 습관을 일찍 박아두자.

Code

모든 차원의 tensor·python
import torch

# 0D — scalar
scalar = torch.tensor(3.14)
print(scalar.ndim, scalar.shape)  # 0  torch.Size([])

# 1D — vector
vector = torch.tensor([1.0, 2.0, 3.0])
print(vector.shape)               # torch.Size([3])

# 2D — matrix
matrix = torch.tensor([[1, 2], [3, 4], [5, 6]])
print(matrix.shape)               # torch.Size([3, 2])

# 3D — batch of sequences (batch=2, seq=3, features=4)
seq_batch = torch.randn(2, 3, 4)
print(seq_batch.shape)            # torch.Size([2, 3, 4])

# 4D — batch of images (NCHW: batch=8, channels=3, H=224, W=224)
images = torch.randn(8, 3, 224, 224)
print(images.shape)               # torch.Size([8, 3, 224, 224])
PyTorch ↔ NumPy 정신 모델·python
import torch
import numpy as np

# A NumPy person already knows this:
arr = np.zeros((3, 4))
print(arr.shape, arr.dtype)        # (3, 4) float64

# PyTorch is the same plus device + autograd:
t = torch.zeros(3, 4)
print(t.shape, t.dtype, t.device)  # torch.Size([3, 4]) torch.float32 cpu

# Note the dtype default changed: NumPy → float64, PyTorch → float32.
# float32 is the deep-learning default because it's twice as fast on GPU
# and almost always 'good enough' precision for gradients.

External links

Exercise

PyTorch 가 설치된 Python REPL 을 열어. 0D 부터 4D 까지 모든 차원의 tensor 를 하나씩 만들어. 각각에 대해 .shape, .ndim, .numel() 을 print. print 하기 전에 값을 예측해 — 4D batch 의 .numel() 예측을 틀리는 게 scale 에서 사람들이 가장 자주 하는 shape 실수야.

Progress

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

댓글 0

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

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