forked from PyTorchKorea/tutorials-kr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforward_ad_usage.py
More file actions
226 lines (180 loc) ยท 11.2 KB
/
forward_ad_usage.py
File metadata and controls
226 lines (180 loc) ยท 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# -*- coding: utf-8 -*-
"""
์์ ํ ๋ชจ๋ ์๋ ๋ฏธ๋ถ(Beta)
=============================================
**๋ฒ์ญ**: `๊น๊ฒฝ๋ฏผ <https://github.com/BcKmini>`_
์ด ํํ ๋ฆฌ์ผ์ ์์ ํ ๋ชจ๋ ์๋ ๋ฏธ๋ถ(Forward-mode Automatic Differentiation)์ ์ฌ์ฉํ์ฌ ๋ฐฉํฅ์ฑ ๋ํจ์(directional derivative) ๋๋ ์ผ์ฝ๋น์-๋ฒกํฐ ๊ณฑ(Jacobian-vector product)์ ๊ณ์ฐํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ค๋๋ค.
์๋ ํํ ๋ฆฌ์ผ์ 1.11 ์ด์ ๋ฒ์ (๋๋ ๋์ดํ๋ฆฌ ๋น๋)์์๋ง ์ฌ์ฉํ ์ ์๋ ์ผ๋ถ API๋ฅผ ์ฌ์ฉํฉ๋๋ค.
๋ํ, ์์ ํ ๋ชจ๋ ์๋ ๋ฏธ๋ถ์ ํ์ฌ ๋ฒ ํ ๋ฒ์ ์
๋๋ค. ๋ฐ๋ผ์ API๊ฐ ๋ณ๊ฒฝ๋ ์ ์์ผ๋ฉฐ, ์์ง ์ผ๋ถ ์ฐ์ฐ์๋ ์ง์๋์ง ์์ ์ ์์ต๋๋ค.
๊ธฐ๋ณธ ์ฌ์ฉ๋ฒ
--------------------------------------------------------------------
์ญ์ ํ ๋ชจ๋ ์๋ ๋ฏธ๋ถ(Reverse-mode Automatic Differentiation)๊ณผ ๋ฌ๋ฆฌ, ์์ ํ ๋ชจ๋ ์๋ ๋ฏธ๋ถ์ ์์ ํ(forward pass)๋ฅผ ์งํํ๋ฉฐ ๊ธฐ์ธ๊ธฐ(gradient)๋ฅผ ์ฆ์(๊ณ์ฐ์ ๋ฏธ๋ฃจ์ง ์๊ณ ) ๊ณ์ฐํฉ๋๋ค. ์์ ํ ๋ชจ๋ ์๋ ๋ฏธ๋ถ์ผ๋ก ๋ฐฉํฅ์ฑ ๋ํจ์๋ฅผ ๊ณ์ฐํ๋ ค๋ฉด, ๋จผ์ ์
๋ ฅ์ ๋ฐฉํฅ์ฑ ๋ํจ์์ ๋ฐฉํฅ์ ๋ํ๋ด๋ ๋ค๋ฅธ tensor(์ผ์ฝ๋น์-๋ฒกํฐ ๊ณฑ์ `v`์ ํด๋น)์ ์ฐ๊ฒฐํ ๋ค ์ด์ ๊ณผ ๊ฐ์ด ์์ ํ๋ฅผ ์ํํ๋ฉด ๋ฉ๋๋ค. 'primal'์ด๋ผ๊ณ ๋ถ๋ฅด๋ ์
๋ ฅ์ด 'tangent'๋ผ๊ณ ๋ถ๋ฅด๋ '๋ฐฉํฅ' tensor์ ์ฐ๊ฒฐ๋ ๋, ๊ฒฐ๊ณผ๋ก ๋์ค๋ ์๋ก์ด tensor ๊ฐ์ฒด๋ ์ด์ค์(dual numbers) [0] ์์ ๊ด๋ จ์ฑ ๋๋ฌธ์ '์ด์ค tensor(dual tensor)'๋ผ๊ณ ๋ถ๋ฆฝ๋๋ค.
์์ ํ๊ฐ ์ํ๋ ๋, ์
๋ ฅ tensor ์ค ํ๋๋ผ๋ ์ด์ค tensor์ด๋ฉด ํจ์์ '๋ฏผ๊ฐ๋(sensitivity)'๋ฅผ ์ ํํ๊ธฐ ์ํด ์ถ๊ฐ์ ์ธ ์ฐ์ฐ์ด ์ํ๋ฉ๋๋ค.
"""
import torch
import torch.autograd.forward_ad as fwAD
primal = torch.randn(10, 10)
tangent = torch.randn(10, 10)
def fn(x, y):
return x ** 2 + y ** 2
# ๋ชจ๋ ์์ ํ ์๋ ๋ฏธ๋ถ ์ฐ์ฐ์ ``dual_level`` ์ปจํ
์คํธ ์์์ ์ํํด์ผ ํฉ๋๋ค.
# ์ด ์ปจํ
์คํธ์์ ์์ฑ๋ ๋ชจ๋ ์ด์ค tensor์ ํ์ ํธ๋ ์ปจํ
์คํธ๋ฅผ ๋ฒ์ด๋ ๋ ์๋ฉธ๋ฉ๋๋ค.
# ์ด๋ ํด๋น ์ฐ์ฐ์ ์ถ๋ ฅ์ด๋ ์ค๊ฐ ๊ฒฐ๊ณผ๊ฐ ํฅํ ๋ค๋ฅธ ์์ ํ ์๋ ๋ฏธ๋ถ ์ฐ์ฐ์ ์ฌ์ฌ์ฉ๋ ๋,
# ํ์ฌ ์ฐ์ฐ์ ์ํ ํ์ ํธ๊ฐ ๋์ค ์ฐ์ฐ์ ํ์ ํธ์ ํผ๋๋๋ ๊ฒ์ ๋ฐฉ์งํ๊ธฐ ์ํจ์
๋๋ค.
with fwAD.dual_level():
# ์ด์ค tensor๋ฅผ ๋ง๋ค๋ ค๋ฉด 'primal' tensor๋ฅผ ๊ฐ์ ํฌ๊ธฐ์ ๋ค๋ฅธ tensor,
# ์ฆ 'ํ์ ํธ(tangent)'์ ์ฐ๊ฒฐํฉ๋๋ค.
# ๋ง์ฝ ํ์ ํธ์ ๋ ์ด์์์ด primal์ ๋ ์ด์์๊ณผ ๋ค๋ฅด๋ฉด,
# ํ์ ํธ์ ๊ฐ์ primal๊ณผ ๋์ผํ ๋ฉํ๋ฐ์ดํฐ๋ฅผ ๊ฐ๋ ์ tensor์ ๋ณต์ฌ๋ฉ๋๋ค.
# ๊ทธ๋ ์ง ์์ผ๋ฉด ํ์ ํธ ์์ฒด๊ฐ ๊ทธ๋๋ก ์ฌ์ฉ๋ฉ๋๋ค.
#
# ``make_dual`` ๋ก ์์ฑ๋ ์ด์ค tensor๋ primal tensor์ **๋ทฐ(๋ฐ์ดํฐ๋ฅผ ๊ณต์ ํ๋ ์ฐธ์กฐ)** ๋ผ๋ ์ ๋
# ์ค์ํฉ๋๋ค.
dual_input = fwAD.make_dual(primal, tangent)
assert fwAD.unpack_dual(dual_input).tangent is tangent
# ํ์ ํธ๊ฐ ๋ณต์ฌ๋๋ ๊ฒฝ์ฐ๋ฅผ ๋ณด์ฌ์ฃผ๊ธฐ ์ํด,
# primal๊ณผ ๋ค๋ฅธ ๋ ์ด์์์ ๊ฐ์ง ํ์ ํธ๋ฅผ ์ ๋ฌํฉ๋๋ค.
dual_input_alt = fwAD.make_dual(primal, tangent.T)
assert fwAD.unpack_dual(dual_input_alt).tangent is not tangent
# ํ์ ํธ๊ฐ ์ฐ๊ฒฐ๋์ง ์์ tensor๋ ์๋์ผ๋ก
# ๊ฐ์ shape์ ๊ฐ์ง๋ฉฐ 0์ผ๋ก ์ฑ์์ง ํ์ ํธ๋ฅผ ๊ฐ์ง ๊ฒ์ผ๋ก ๊ฐ์ฃผ๋ฉ๋๋ค.
plain_tensor = torch.randn(10, 10)
dual_output = fn(dual_input, plain_tensor)
# ์ด์ค tensor๋ฅผ ํ๋ฉด(unpack) ``primal`` ๊ณผ ``tangent`` ๋ฅผ
# ์์ฑ์ผ๋ก ๊ฐ๋ ``namedtuple`` ์ด ๋ฐํ๋ฉ๋๋ค.
jvp = fwAD.unpack_dual(dual_output).tangent
assert fwAD.unpack_dual(dual_output).tangent is None
######################################################################
# ๋ชจ๋๊ณผ ํจ๊ป ์ฌ์ฉํ๊ธฐ
# --------------------------------------------------------------------
# ``nn.Module`` ์ ์์ ํ ์๋ ๋ฏธ๋ถ๊ณผ ํจ๊ป ์ฌ์ฉํ๋ ค๋ฉด, ์์ ํ๋ฅผ ์ํํ๊ธฐ ์ ์
# ๋ชจ๋ธ์ ๋งค๊ฐ๋ณ์(parameter)๋ฅผ ์ด์ค tensor๋ก ๊ต์ฒดํด์ผ ํฉ๋๋ค. ํ์ฌ ์ด์ค tensor๋ก ๋
# `nn.Parameter` ๋ ์์ฑํ ์ ์์ต๋๋ค. ์ด์ ๋ํ ํด๊ฒฐ ๋ฐฉ๋ฒ์ผ๋ก,
# ์ด์ค tensor๋ฅผ ๋ชจ๋์ ๋งค๊ฐ๋ณ์๊ฐ ์๋ ์ผ๋ฐ ์์ฑ์ผ๋ก ๋ฑ๋กํด์ผ ํฉ๋๋ค.
import torch.nn as nn
model = nn.Linear(5, 5)
input = torch.randn(16, 5)
params = {name: p for name, p in model.named_parameters()}
tangents = {name: torch.rand_like(p) for name, p in params.items()}
with fwAD.dual_level():
for name, p in params.items():
delattr(model, name)
setattr(model, name, fwAD.make_dual(p, tangents[name]))
out = model(input)
jvp = fwAD.unpack_dual(out).tangent
######################################################################
# ํจ์ํ ๋ชจ๋ API ์ฌ์ฉํ๊ธฐ (Beta)
# --------------------------------------------------------------------
# ``nn.Module`` ์ ์์ ํ ์๋ ๋ฏธ๋ถ๊ณผ ํจ๊ป ์ฌ์ฉํ๋ ๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ์
# ํจ์ํ ๋ชจ๋ API๋ฅผ ํ์ฉํ๋ ๊ฒ์
๋๋ค. (์ํ๊ฐ ์๋ ๋ชจ๋ API๋ผ๊ณ ๋ ํจ)
from torch.func import functional_call
# functional_call์ ๋ชจ๋ธ์ ๋งค๊ฐ๋ณ์๊ฐ ๋ฑ๋ก๋์ด ์์ด์ผ ํ๋ฏ๋ก
# ์๋ก์ด ๋ชจ๋์ด ํ์ํฉ๋๋ค.
model = nn.Linear(5, 5)
dual_params = {}
with fwAD.dual_level():
for name, p in params.items():
# ์ ์น์
๊ณผ ๋์ผํ ``tangents`` ๋ฅผ ์ฌ์ฉํฉ๋๋ค.
dual_params[name] = fwAD.make_dual(p, tangents[name])
out = functional_call(model, dual_params, input)
jvp2 = fwAD.unpack_dual(out).tangent
# ๊ฒฐ๊ณผ ํ์ธ
assert torch.allclose(jvp, jvp2)
######################################################################
# ์ฌ์ฉ์ ์ ์ autograd ํจ์
# --------------------------------------------------------------------
# ์ฌ์ฉ์ ์ ์ ํจ์ ๋ํ ์์ ํ ๋ชจ๋ ์๋ ๋ฏธ๋ถ์ ์ง์ํฉ๋๋ค. ์์ ํ ๋ชจ๋ ์๋ ๋ฏธ๋ถ์
# ์ง์ํ๋ ์ฌ์ฉ์ ์ ์ ํจ์๋ฅผ ๋ง๋ค๋ ค๋ฉด ``jvp()`` ์ ์ ๋ฉ์๋๋ฅผ
# ๋ฑ๋กํด์ผ ํฉ๋๋ค. ์ฌ์ฉ์ ์ ์ ํจ์๊ฐ ์์ ํ์ ์ญ์ ํ ์๋ ๋ฏธ๋ถ์ ๋ชจ๋ ์ง์ํ๋ ๊ฒ๋
# ๊ฐ๋ฅํ์ง๋ง ํ์๋ ์๋๋๋ค. ๋ ์์ธํ ์ ๋ณด๋
# `๋ฌธ์ <https://pytorch.org/docs/master/notes/extending.html#forward-mode-ad>`_
# ๋ฅผ ์ฐธ๊ณ ํ์ธ์.
class Fn(torch.autograd.Function):
@staticmethod
def forward(ctx, foo):
result = torch.exp(foo)
# ``ctx`` ์ ์ ์ฅ๋ tensor๋ ์ดํ์ ์์ ํ ๊ธฐ์ธ๊ธฐ
# ๊ณ์ฐ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
ctx.result = result
return result
@staticmethod
def jvp(ctx, gI):
gO = gI * ctx.result
# ``ctx`` ์ ์ ์ฅ๋ tensor๊ฐ ์ญ์ ํ์ ์ฌ์ฉ๋์ง ์์ ๊ฒฝ์ฐ,
# ``del`` ์ ์ฌ์ฉํ์ฌ ์๋์ผ๋ก ๋ฉ๋ชจ๋ฆฌ์์ ํด์ ํ ์ ์์ต๋๋ค.
del ctx.result
return gO
fn = Fn.apply
primal = torch.randn(10, 10, dtype=torch.double, requires_grad=True)
tangent = torch.randn(10, 10)
with fwAD.dual_level():
dual_input = fwAD.make_dual(primal, tangent)
dual_output = fn(dual_input)
jvp = fwAD.unpack_dual(dual_output).tangent
# ์ฌ์ฉ์ ์ ์ autograd ํจ์๊ฐ ๊ธฐ์ธ๊ธฐ๋ฅผ ์ฌ๋ฐ๋ฅด๊ฒ ๊ณ์ฐํ๋์ง ํ์ธํ๋ ค๋ฉด
# ``autograd.gradcheck`` ๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ค์ํฉ๋๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก
# ``gradcheck`` ๋ ์ญ์ ํ ๋ชจ๋(reverse-mode) ์๋ ๋ฏธ๋ถ ๊ธฐ์ธ๊ธฐ๋ง ํ์ธํฉ๋๋ค.
# ``check_forward_ad=True`` ๋ฅผ ์ง์ ํ์ฌ ์์ ํ ๊ธฐ์ธ๊ธฐ๋ ํ์ธํ๋๋ก ํ ์ ์์ต๋๋ค.
# ๋ง์ฝ ํจ์์ ๋ํ ์ญ์ ํ๋ฅผ ๊ตฌํํ์ง ์์๋ค๋ฉด, ``check_backward_ad=False``,
# ``check_undefined_grad=False``, ``check_batched_grad=False`` ๋ฅผ ์ง์ ํ์ฌ
# ``gradcheck`` ๊ฐ ์ญ์ ํ ๋ชจ๋ ์๋ ๋ฏธ๋ถ์ด ํ์ํ ํ
์คํธ๋ฅผ ๊ฑด๋๋ฐ๋๋ก ํ ์ ์์ต๋๋ค.
torch.autograd.gradcheck(Fn.apply, (primal,), check_forward_ad=True,
check_backward_ad=False, check_undefined_grad=False,
check_batched_grad=False)
######################################################################
# ํจ์ํ API (Beta)
# --------------------------------------------------------------------
# Functorch๋ ์ผ์ฝ๋น์-๋ฒกํฐ ๊ณฑ์ ๊ณ์ฐํ๊ธฐ ์ํ ๊ณ ์์ค ํจ์ํ API๋
# ์ ๊ณตํ๋ฉฐ, ์ฌ์ฉ ์ฌ๋ก์ ๋ฐ๋ผ ๋ ๊ฐ๋จํ๊ฒ ์ฌ์ฉํ ์ ์์ต๋๋ค.
#
# ํจ์ํ API์ ์ฅ์ ์ ์ ์์ค์ ์ด์ค tensor API๋ฅผ ์ดํดํ๊ฑฐ๋ ์ฌ์ฉํ
# ํ์๊ฐ ์์ผ๋ฉฐ, ๋ค๋ฅธ `functorch ๋ณํ(vmap ๋ฑ)๊ณผ ๊ฒฐํฉ <https://pytorch.org/functorch/stable/notebooks/jacobians_hessians.html>`_
# ํ ์ ์๋ค๋ ๊ฒ์
๋๋ค. ๋จ์ ์ ์ธ๋ฐํ ์ ์ด๊ฐ ์ด๋ ต๋ค๋ ์ ์
๋๋ค.
#
# ์ด ํํ ๋ฆฌ์ผ์ ๋๋จธ์ง ๋ถ๋ถ์ ์คํํ๋ ค๋ฉด functorch
# (https://github.com/pytorch/functorch) ๊ฐ ํ์ํฉ๋๋ค.
# ์ค์น ๋ฐฉ๋ฒ์ ํด๋น ๋งํฌ์์ ํ์ธํด์ฃผ์ธ์.
import functorch as ft
primal0 = torch.randn(10, 10)
tangent0 = torch.randn(10, 10)
primal1 = torch.randn(10, 10)
tangent1 = torch.randn(10, 10)
def fn(x, y):
return x ** 2 + y ** 2
# ์ ํจ์์ JVP๋ฅผ ๊ณ์ฐํ๋ ๊ธฐ๋ณธ ์์ ์
๋๋ค.
# ``jvp(func, primals, tangents)`` ๋ ``func(*primals)`` ์ ๊ฒฐ๊ณผ์ ๊ณ์ฐ๋
# ์ผ์ฝ๋น์-๋ฒกํฐ ๊ณฑ(JVP)์ ํจ๊ป ๋ฐํํฉ๋๋ค. ๊ฐ primal์ ๊ฐ์ shape์ ํ์ ํธ์
# ์ฐ๊ฒฐ๋์ด์ผ ํฉ๋๋ค.
primal_out, tangent_out = ft.jvp(fn, (primal0, primal1), (tangent0, tangent1))
# ``functorch.jvp`` ๋ ๋ชจ๋ primal์ด ํ์ ํธ์ ์ฐ๊ฒฐ๋ ๊ฒ์ ์๊ตฌํฉ๋๋ค.
# ๋ง์ฝ ``fn`` ์ ํน์ ์
๋ ฅ์๋ง ํ์ ํธ๋ฅผ ์ฐ๊ฒฐํ๊ณ ์ถ๋ค๋ฉด,
# ํ์ ํธ๊ฐ ์๋ ์
๋ ฅ์ ๋ฐ๋ ์๋ก์ด ํจ์๋ฅผ ๋ง๋ค์ด์ผ ํฉ๋๋ค.
primal = torch.randn(10, 10)
tangent = torch.randn(10, 10)
y = torch.randn(10, 10)
import functools
new_fn = functools.partial(fn, y=y)
primal_out, tangent_out = ft.jvp(new_fn, (primal,), (tangent,))
######################################################################
# ํจ์ํ API๋ฅผ ๋ชจ๋๊ณผ ํจ๊ป ์ฌ์ฉํ๊ธฐ
# --------------------------------------------------------------------
# ``nn.Module`` ๊ณผ ``functorch.jvp`` ๋ฅผ ํจ๊ป ์ฌ์ฉํ์ฌ ๋ชจ๋ธ ๋งค๊ฐ๋ณ์์ ๋ํ
# ์ผ์ฝ๋น์-๋ฒกํฐ ๊ณฑ์ ๊ณ์ฐํ๋ ค๋ฉด, ``nn.Module`` ์ ๋ชจ๋ธ ๋งค๊ฐ๋ณ์์
# ๋ชจ๋์ ์
๋ ฅ์ ๋ชจ๋ ์ธ์๋ก ๋ฐ๋ ํจ์๋ก ์ฌ๊ตฌ์ฑํด์ผ ํฉ๋๋ค.
model = nn.Linear(5, 5)
input = torch.randn(16, 5)
tangents = tuple([torch.rand_like(p) for p in model.parameters()])
# ``ft.make_functional_with_buffers`` ๋ ์ฃผ์ด์ง ``torch.nn.Module`` ์์
# ์ํ(``params`` ์ ๋ฒํผ)๋ฅผ ์ถ์ถํ๊ณ , ํจ์์ฒ๋ผ ํธ์ถํ ์ ์๋
# ํจ์ํ ๋ฒ์ ์ ๋ชจ๋ธ์ ๋ฐํํฉ๋๋ค.
# ์ฆ, ๋ฐํ๋ ``func`` ๋ ``func(params, buffers, input)`` ์ฒ๋ผ ํธ์ถํ ์ ์์ต๋๋ค.
# ``ft.make_functional_with_buffers`` ๋ ์ด์ ์ ๋ณด์๋ ``nn.Module`` ์ ์ํ ์๋ API์
# ์ ์ฌํ๋ฉฐ, ์ด ๋์ ํตํฉํ๋ ์์
์ด ์งํ ์ค์
๋๋ค.
func, params, buffers = ft.make_functional_with_buffers(model)
# ``jvp`` ๋ ๋ชจ๋ ์
๋ ฅ์ด ํ์ ํธ์ ์ฐ๊ฒฐ๋ ๊ฒ์ ์๊ตฌํ๋ฏ๋ก,
# ๋งค๊ฐ๋ณ์๋ฅผ ๋ฐ์์ ๋ ์ถ๋ ฅ์ ์์ฑํ๋ ์๋ก์ด ํจ์๋ฅผ ๋ง๋ค์ด์ผ ํฉ๋๋ค.
def func_params_only(params):
return func(params, buffers, input)
model_output, jvp_out = ft.jvp(func_params_only, (params,), (tangents,))
######################################################################
# [0] https://en.wikipedia.org/wiki/Dual_number