tensor 를 만드는 세 가지 방법: Python data 로부터, shape 로부터, 다른 tensor 로부터. factory zoo 가 넓지만, 실전에선 여섯 함수가 ~95% 를 cover 해.
함수
주는 것
전형적 용도
torch.tensor
Python list / scalar 에서 tensor
quick test data, label
torch.zeros / ones
0 또는 1 로 채움
bias init, mask, accumulator
torch.randn
Normal(0, 1)
weight init, dummy input
torch.rand
Uniform [0, 1)
probability, dropout-style noise
torch.arange
integer / float range
positional encoding, index
torch.full
상수로 채움
padding value, mask
like 가족 — torch.zeros_like(x), torch.randn_like(x), torch.empty_like(x) — 는 x 의 shape, dtype, AND device 를 복사해. result 용 sibling tensor 할당할 때 정확히 원하는 거 — 'expected cuda:0 got cpu' 에러의 90% 를 막아.
외워둘 두 가지 스타일 룰
torch.tensor(np.random.randn(N)) 보다 torch.randn(N) 선호. NumPy 우회는 불필요하고 dtype/device 계약을 silently 깨.
가능하면 만들 때 device=... 넘겨. CPU 에 할당 후 .to('cuda') 는 이유 없이 allocation 트래픽을 두 배로.
Code
Python data 와 shape 으로부터·python
import torch
# From Python lists / scalars
t1 = torch.tensor([1, 2, 3, 4]) # int64 by default for ints
t2 = torch.tensor([[1.0, 2.0], [3.0, 4.0]]) # float32 for floats
t3 = torch.tensor(3.14) # 0D scalar tensor
# Force a dtype
t4 = torch.tensor([1, 2, 3], dtype=torch.float32)
t5 = torch.tensor([0, 1, 1, 0], dtype=torch.bool)
# From shape — these all return a (3, 4) tensor
zeros = torch.zeros(3, 4)
ones = torch.ones(3, 4)
sevens = torch.full((3, 4), 7.0)
identity = torch.eye(4)
Random tensor 와 sequence·python
import torch
# Standard normal (mean 0, std 1) — the canonical weight init starting point
w = torch.randn(128, 64)
# Uniform [0, 1)
p = torch.rand(32)
# Random integers (low inclusive, high exclusive)
labels = torch.randint(0, 10, (32,)) # 32 labels in [0, 10)
# Sequences
torch.arange(0, 10, 2) # tensor([0, 2, 4, 6, 8])
torch.linspace(0.0, 1.0, 5) # tensor([0.0, 0.25, 0.5, 0.75, 1.0])
torch.logspace(0, 3, 4) # tensor([1., 10., 100., 1000.])
*_like 가족 — 친구의 metadata 복사·python
import torch
x = torch.randn(8, 3, 224, 224, device="cpu", dtype=torch.float32)
# Allocate a sibling tensor — same shape, dtype, AND device
buffer = torch.zeros_like(x)
noise = torch.randn_like(x)
# Without _like you'd have to repeat all three:
# buffer = torch.zeros(8, 3, 224, 224, dtype=x.dtype, device=x.device)
# That's the bug-magnet you avoid by using _like.
함수 init_xavier(in_features, out_features) 를 짜서, std = sqrt(2 / (in_features + out_features)) 의 normal 에서 (out_features, in_features) shape 의 weight tensor 를 반환. nn.Linear(in, out).weight.std() 와 너 출력의 std 비교 — 비슷한 범위에 있어야 해.
Progress
Progress is local-only — sign in to sync across devices.