| ์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
|---|---|---|---|---|---|---|
| 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 |
- SQL
- ๋ฌด๊ฒฐ์ฑ์ ์ง๋ฉ์ปค๋์ฆ
- Python
- ๋ฐฑ์ค
- Key ์ข ๋ฅ
- ํค ์ข ๋ฅ
- ์๊ณ ๋ฆฌ์ฆ
- ํด๋ฆฐ์ฝ๋
- SQLDDL
- ๋ฌด๊ฒฐ์ฑ์ ์ง
- DROPTABLE
- RENAMETABLE
- TDD
- ํ
- ํ์ด์ฌ
- ์ ์ฌ์์๋ชจ๋ธ
- ์ฃผ์ฑ๋ถ ์ฐพ๊ธฐ
- ์ฌ์ดํท๋ฐ
- ์ธ๋๋ณ๊ฐ๋น์ง์ปฌ๋ ํฐ
- Hyperlink Graphs
- ALTERTABLE
- sklearn
- knn_classify
- latent factor model
- ๋ฌธ์์ด
- CREATETABLE
- ๋ฌด๊ฒฐ์ฑ
- latent factor
- ์ปจํ ์ด๋๊ฐ์ฒด
- ๋ถ๊ฝ๋ฐ์ดํฐ์
- Today
- Total
DonHurry
step16. ๋ณต์กํ ๊ณ์ฐ ๊ทธ๋ํ(๊ตฌํ ํธ) ๋ณธ๋ฌธ
๐ข ๋ณธ ํฌ์คํ ์ ๋ฐ๋ฐ๋ฅ๋ถํฐ ์์ํ๋ ๋ฅ๋ฌ๋3์ ๊ธฐ๋ฐ์ผ๋ก ์์ฑํ์์ต๋๋ค. ๋ฐฐ์ด ๋ด์ฉ์ ๊ธฐ๋กํ๊ณ , ๊ฐ์ธ์ ์ธ ๊ณต๋ถ๋ฅผ ์ํด ์์ฑํ๋ ํฌ์คํ ์ ๋๋ค. ์์ธํ ๋ด์ฉ์ ๊ต์ฌ ๊ตฌ๋งค๋ฅผ ๊ฐ๋ ฅ ์ถ์ฒ๋๋ฆฝ๋๋ค.

์ด๋ฒ ๋จ๊ณ์์๋ 15๋จ๊ณ์ ์ด๋ก ์ ์ง์ ๊ตฌํํฉ๋๋ค. ๋จผ์ ์์ ํ ์ ์ธ๋๋ฅผ ์ค์ ํ๊ฒ ์ต๋๋ค. Variableํด๋์ค์ Function ํด๋์ค์ ์ธ์คํด์ค ๋ณ์ generation์ ์ถ๊ฐํฉ๋๋ค. ๋ช ๋ฒ์งธ ์ธ๋์ ํจ์์ธ์ง ๋ํ๋ด๋ ๋ณ์์ ๋๋ค. set_creator ๋ฉ์๋๊ฐ ํธ์ถ๋ ๋ ์ธ๋๋ฅผ 1๋งํผ ๋๋ ค์ค๋๋ค. ์ฆ, ์ด๋ค ํจ์๋ก๋ถํฐ ๋์จ ๋ณ์๋ ํด๋น ํจ์๋ณด๋ค 1๋งํผ ํฐ ์ธ๋ ๊ฐ์ ๊ฐ์ต๋๋ค.
class Variable:
def __init__(self, data):
if data is not None:
if not isinstance(data, np.ndarray):
raise TypeError('{} is not supported'.format(type(data)))
self.data = data
self.grad = None
self.creator = None
self.generation = 0 # ์ธ๋ ์ ๊ธฐ๋ก
def set_creator(self, func):
self.creator = func
self.generation = func.generation + 1 # ์ธ๋ ๊ธฐ๋ก(๋ถ๋ชจ ์ธ๋ + 1)
...
๋ค์์ Function ํด๋์ค์ ๋๋ค. ์ฌ๊ธฐ์๋ ์ ๋ ฅ ๋ณ์๊ฐ ๋ ์ด์์ผ ๋, ๊ฐ์ฅ ํฐ ์ธ๋๋ฅผ ์ ํํ๋๋ก ํฉ๋๋ค.
class Function:
def __call__(self, *inputs):
xs = [x.data for x in inputs]
ys = self.forward(*xs)
if not isinstance(ys, tuple):
ys = (ys, )
outputs = [Variable(as_array(y)) for y in ys]
self.generation = max([x.generation for x in inputs])
for output in outputs:
output.set_creator(self)
self.inputs = inputs
self.outputs = outputs
return outputs if len(outputs) > 1 else outputs[0]
...
์ด์ ๋จ๊ณ์์, ์ธ๋๊ฐ ํฐ ํจ์๋ถํฐ ์ฒ๋ฆฌํ๋ฉด ์ฌ๋ฐ๋ฅธ ์์๋ก ์ญ์ ํ๊ฐ ๊ฐ๋ฅํ๋ค๊ณ ํ์ต๋๋ค. ๊ฐ์ฅ ํฐ ์์๋ฅผ ๊บผ๋ด๊ธฐ ์ํ ๋จ์ํ ๋ฐฉ๋ฒ ์ค ํ๋๋ ์ ๋ ฌ์ ์ด์ฉํ๋ ๊ฒ์ ๋๋ค. ํจ์จ์ ๊ณ ๋ คํ๋ฉด ์ฐ์ ์์ ํ๋ฅผ ์ด์ฉํด ๊ตฌํํ๋ ๊ฒ์ด ๋ง์ง๋ง, ์ฐ์ ์ ์ ๋ ฌ์ ์ด์ฉํ๊ฒ ์ต๋๋ค. ๊ทธ๋์์๋ DeZero์ ํจ์๋ค์ ๋ฆฌ์คํธ์ ์ถ๊ฐํ๋ ๋ฐฉ์์ ์ด์ฉํ์ง๋ง, add_func ํจ์๋ฅผ ํธ์ถํ๋๋ก ๋ณ๊ฒฝํ์์ต๋๋ค. add_func ํจ์๋ ํจ์๋ค์ ์ธ๋ ์์ผ๋ก ์ ๋ ฌํ๋ ์ญํ ์ด ํฌํจ๋์ด ์์ต๋๋ค.
class Variable:
...
def backward(self):
if self.grad is None:
self.grad = np.ones_like(self.data)
funcs = []
seen_set = set()
# ์ถํ ์ฐ์ ์์ ํ๋ก ๊ตฌํ
def add_func(f):
if f not in seen_set:
funcs.append(f)
seen_set.add(f)
funcs.sort(key=lambda x: x.generation)
add_func(self.creator)
while funcs:
f = funcs.pop()
gys = [output.grad for output in f.outputs]
gxs = f.backward(*gys)
if not isinstance(gxs, tuple):
gxs = (gxs, )
for x, gx in zip(f.inputs, gxs):
if x.grad is None:
x.grad = gx
else:
x.grad = x.grad + gx
if x.creator is not None:
add_func(x.creator) # funcs.append(x.creator)์์ ์์

์ด์ ์ด์ ์๋ ๊ตฌํํ์ง ๋ชปํ๋ ์์ ๊ฐ์ ๊ณ์ฐ ๊ทธ๋ํ๋ฅผ ํ ์คํธํด๋ณด๊ฒ ์ต๋๋ค. ๊ณ์ฐ ๊ทธ๋ํ๋ $y = (x^2)^2 + (x^2)^2$์ด๋ฏ๋ก $y=2x^4$์ ๋ฏธ๋ถํ๋ ๋ฌธ์ ๊ฐ ๋ฉ๋๋ค. ๋ฐ๋ผ์ ํ ์คํธ ๊ฒฐ๊ณผ๊ฐ ์ ๋๋ก ์ถ๋ ฅ๋ ๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค. ์ด๊ฒ์ผ๋ก DeZero๋ ์๋ฌด๋ฆฌ ๋ณต์กํ ์ฐ๊ฒฐ๋ ์ ๋๋ก ๋ฏธ๋ถํ ์ ์์ต๋๋ค.
x = Variable(np.array(2.0))
a = square(x)
y = add(square(a), square(a))
y.backward()
print(y.data) # 32.0
print(x.grad) # 64.0
'DeZero > ๐ป์ 2๊ณ ์ง' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| step18. ๋ฉ๋ชจ๋ฆฌ ์ ์ฝ ๋ชจ๋ (0) | 2023.01.19 |
|---|---|
| step17. ๋ฉ๋ชจ๋ฆฌ ๊ด๋ฆฌ์ ์ํ ์ฐธ์กฐ (0) | 2023.01.18 |
| step15. ๋ณต์กํ ๊ณ์ฐ ๊ทธ๋ํ(์ด๋ก ํธ) (0) | 2023.01.16 |
| step14. ๊ฐ์ ๋ณ์ ๋ฐ๋ณต ์ฌ์ฉ (0) | 2023.01.15 |
| step13. ๊ฐ๋ณ ๊ธธ์ด ์ธ์(์ญ์ ํ ํธ) (2) | 2023.01.14 |