수학 함수
지수 · 로그 함수
섹션 제목: “지수 · 로그 함수”딥러닝에서 지수와 로그 함수는 활성화 함수(softmax, sigmoid), 손실 함수(cross-entropy) 등 곳곳에 등장합니다.
import torchimport math
t = torch.tensor([0.0, 1.0, 2.0, math.e])
# 지수 함수: e^xprint(torch.exp(t))# tensor([ 1.0000, 2.7183, 7.3891, 15.1543])
# 자연 로그: ln(x) = log_e(x)pos = torch.tensor([1.0, math.e, math.e**2])print(torch.log(pos))# tensor([0., 1., 2.])
# 밑이 2인 로그print(torch.log2(torch.tensor([1.0, 2.0, 4.0, 8.0])))# tensor([0., 1., 2., 3.])
# 밑이 10인 로그print(torch.log10(torch.tensor([1.0, 10.0, 100.0])))# tensor([0., 1., 2.])
# log(1 + x): 수치 안정성이 높아 x가 0에 가까울 때 유용print(torch.log1p(torch.tensor([0.0, 1e-8, 1.0])))# tensor([0.0000e+00, 1.0000e-08, 6.9315e-01])실전: Softmax 직접 구현
섹션 제목: “실전: Softmax 직접 구현”logits = torch.tensor([2.0, 1.0, 0.1])
# softmax = exp(x) / sum(exp(x))exp_logits = torch.exp(logits)softmax = exp_logits / exp_logits.sum()print(softmax)# tensor([0.6590, 0.2424, 0.0986])
# PyTorch 내장 함수와 비교print(torch.softmax(logits, dim=0))# tensor([0.6590, 0.2424, 0.0986])삼각함수
섹션 제목: “삼각함수”# 라디안 단위 입력angles = torch.tensor([0.0, math.pi/6, math.pi/4, math.pi/3, math.pi/2])
print(torch.sin(angles))# tensor([0.0000, 0.5000, 0.7071, 0.8660, 1.0000])
print(torch.cos(angles))# tensor([1.0000, 0.8660, 0.7071, 0.5000, 0.0000])
print(torch.tan(angles[:4])) # pi/2에서 tan은 무한대# tensor([0.0000, 0.5774, 1.0000, 1.7321])도(degree) 단위를 사용할 때는 먼저 변환합니다.
degrees = torch.tensor([0.0, 30.0, 45.0, 60.0, 90.0])radians = torch.deg2rad(degrees)print(torch.sin(radians))# tensor([0.0000, 0.5000, 0.7071, 0.8660, 1.0000])
# 역삼각함수x = torch.tensor([0.0, 0.5, 1.0])print(torch.asin(x)) # arcsin (라디안)print(torch.acos(x)) # arccos (라디안)print(torch.atan(x)) # arctan (라디안)올림 · 내림 · 반올림
섹션 제목: “올림 · 내림 · 반올림”| 함수 | 설명 | 예시 (1.7) | 예시 (-1.7) |
|---|---|---|---|
torch.ceil(t) | 올림 (ceiling) | 2.0 | -1.0 |
torch.floor(t) | 내림 (floor) | 1.0 | -2.0 |
torch.round(t) | 반올림 (짝수 우선) | 2.0 | -2.0 |
torch.trunc(t) | 소수점 버림 (0 방향) | 1.0 | -1.0 |
torch.frac(t) | 소수 부분만 | 0.7 | -0.7 |
t = torch.tensor([-1.7, -0.5, 0.5, 1.2, 1.7, 2.5])
print(torch.ceil(t)) # tensor([-1., -0., 1., 2., 2., 3.])print(torch.floor(t)) # tensor([-2., -1., 0., 1., 1., 2.])print(torch.round(t)) # tensor([-2., -0., 0., 1., 2., 2.])print(torch.trunc(t)) # tensor([-1., -0., 0., 1., 1., 2.])clamp(): 값 범위 제한
섹션 제목: “clamp(): 값 범위 제한”clamp() (또는 clip()) 는 값을 지정된 최솟값과 최댓값 사이로 제한합니다. 그래디언트 클리핑, 픽셀 값 정규화 등에 자주 사용합니다.
t = torch.tensor([-3.0, -1.0, 0.0, 1.0, 3.0, 5.0])
# 0과 2 사이로 클램핑print(torch.clamp(t, min=0.0, max=2.0))# tensor([0., 0., 0., 1., 2., 2.])
# min만 지정 (ReLU와 동일)print(torch.clamp(t, min=0.0))# tensor([0., 0., 0., 1., 3., 5.])
# max만 지정print(torch.clamp(t, max=1.0))# tensor([-3., -1., 0., 1., 1., 1.])
# 이미지 픽셀 값 [0, 255] 범위 제한pixel = torch.tensor([-10.0, 0.0, 128.0, 255.0, 300.0])print(torch.clamp(pixel, 0, 255))# tensor([ 0., 0., 128., 255., 255.])절댓값 · 제곱근
섹션 제목: “절댓값 · 제곱근”t = torch.tensor([-4.0, -1.0, 0.0, 1.0, 4.0, 9.0])
# 절댓값print(torch.abs(t))# tensor([4., 1., 0., 1., 4., 9.])
# 제곱근 (음수에는 nan)print(torch.sqrt(torch.abs(t)))# tensor([2., 1., 0., 1., 2., 3.])
# 역제곱근: 1/sqrt(x)pos = torch.tensor([1.0, 4.0, 9.0, 16.0])print(torch.rsqrt(pos))# tensor([1.0000, 0.5000, 0.3333, 0.2500])
# 제곱print(torch.square(torch.tensor([1.0, 2.0, 3.0])))# tensor([1., 4., 9.])
# 거듭제곱print(torch.pow(torch.tensor([2.0, 3.0, 4.0]), 3))# tensor([ 8., 27., 64.])함수 요약표
섹션 제목: “함수 요약표”| 카테고리 | 함수 | 설명 |
|---|---|---|
| 지수/로그 | exp(t) | e^x |
| 지수/로그 | log(t) | ln(x) |
| 지수/로그 | log2(t), log10(t) | 밑이 2, 10인 로그 |
| 지수/로그 | log1p(t) | ln(1+x), 수치 안정 |
| 삼각함수 | sin(t), cos(t), tan(t) | 라디안 입력 |
| 삼각함수 | asin(t), acos(t), atan(t) | 역삼각함수 |
| 삼각함수 | deg2rad(t), rad2deg(t) | 단위 변환 |
| 반올림 | ceil(t), floor(t), round(t) | 올림/내림/반올림 |
| 반올림 | trunc(t), frac(t) | 정수부/소수부 |
| 범위 제한 | clamp(t, min, max) | 값 범위 클램핑 |
| 절댓값 | abs(t) | 절댓값 |
| 제곱근 | sqrt(t), rsqrt(t) | 제곱근 / 역제곱근 |
| 제곱 | square(t), pow(t, n) | 제곱 / 거듭제곱 |