| μΌ | μ | ν | μ | λͺ© | κΈ | ν |
|---|---|---|---|---|---|---|
| 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 |
- 무결μ±
- 무결μ±μ μ§
- DROPTABLE
- SQL
- λΆκ½λ°μ΄ν°μ
- TDD
- 컨ν μ΄λκ°μ²΄
- knn_classify
- μ μ¬μμλͺ¨λΈ
- μ¬μ΄ν·λ°
- Python
- ν€ μ’ λ₯
- ν΄λ¦°μ½λ
- 무결μ±μ μ§λ©μ»€λμ¦
- ALTERTABLE
- latent factor
- Hyperlink Graphs
- μΈλλ³κ°λΉμ§μ»¬λ ν°
- latent factor model
- Key μ’ λ₯
- νμ΄μ¬
- sklearn
- SQLDDL
- CREATETABLE
- μκ³ λ¦¬μ¦
- λ°±μ€
- RENAMETABLE
- λ¬Έμμ΄
- ν
- μ£Όμ±λΆ μ°ΎκΈ°
- Today
- Total
DonHurry
step40. λΈλ‘λμΊμ€νΈ ν¨μ λ³Έλ¬Έ
π’ λ³Έ ν¬μ€ν μ λ°λ°λ₯λΆν° μμνλ λ₯λ¬λ3μ κΈ°λ°μΌλ‘ μμ±νμμ΅λλ€. λ°°μ΄ λ΄μ©μ κΈ°λ‘νκ³ , κ°μΈμ μΈ κ³΅λΆλ₯Ό μν΄ μμ±νλ ν¬μ€ν μ λλ€. μμΈν λ΄μ©μ κ΅μ¬ ꡬ맀λ₯Ό κ°λ ₯ μΆμ²λ립λλ€.

μ΄μ λ¨κ³μμλ DeZeroλ§μ sum ν¨μλ₯Ό ꡬννμ΅λλ€. μ΄λ broadcast_to ν¨μκ° μ¬μ©λμλλ°, μ΄λ² λ¨κ³μμ ꡬνν΄λ³΄κ² μ΅λλ€. μμ νμμλ μμ λνμ΄μ λΈλ‘λμΊμ€νΈλ₯Ό νμ©νμ§λ§, μμ νλ DeZeroλ§μ μ½λλ₯Ό ꡬνν΄λ΄μΌ ν©λλ€. μ°μ λΈλ‘λμΊμ€νΈ ν¨μμ sum ν¨μλ μνΈ μμ‘΄μ μΈ κ΄κ³μμ μμμΌ ν©λλ€.


μμ κ·Έλ¦Όκ³Ό κ°μ΄ μ½λλ₯Ό ꡬννλ©΄, broadcast_toμ μμ νμλ sum_toκ° sum_toμ μμ νμλ broadcast_toκ° νμ©λλ κ²μ νμΈν μ μμ΅λλ€.
class SumTo(Function):
def __init__(self, shape):
self.shape = shape
def forward(self, x):
self.x_shape = x.shape
y = utils.sum_to(x, self.shape)
return y
def backward(self, gy):
gx = broadcast_to(gy, self.x_shape)
return gx
def sum_to(x, shape):
if x.shape == shape:
return as_variable(x)
return SumTo(shape)(x)
class BroadcastTo(Function):
def __init__(self, shape):
self.shape = shape
def forward(self, x):
self.x_shape = x.shape
y = np.broadcast_to(x, self.shape)
return y
def backward(self, gy):
gx = sum_to(gy, self.x_shape)
return gx
def broadcast_to(x, shape):
if x.shape == shape:
return as_variable(x)
return BroadcastTo(shape)(x)
λ€μμΌλ‘ λΈλ‘λμΊμ€νΈ λμμ΄ νμν©λλ€. λνμ΄μμλ νμμ΄ λ€λ₯Έ λ€μ°¨μ λ°°μ΄λΌλ¦¬μ μ°μ°μ κ°λ₯νκ² ν©λλ€. DeZero μμ λ§μ°¬κ°μ§λ‘ νμμ΄ λ€λ₯Ό λ, κ³μ° κ³Όμ μμ λΈλ‘λμΊμ€νΈ κΈ°λ₯μ λμμν€λλ‘ νκ² μ΅λλ€. λ€μ μ½λλ λνμ΄μ λΈλ‘λμΊμ€νΈ κΈ°λ₯μ λλ€.
x0 = np.array([1, 2, 3])
x1 = np.array([10])
y = x0 + x1
print(y) # array([11, 12, 13])
ν΅μ¬μ μμ νμμ λΈλ‘λμΊμ€νΈκ° μΌμ΄λ¬λ€λ©΄, μμ νμμ λΈλ‘λμΊμ€νΈμ μμ νκ° μ΄λ£¨μ΄μ ΈμΌ νλ€λ μ μ λλ€. μμ 보μλ κ·Έλ¦Όκ³Ό κ°μ΄ broadcast_to ν¨μμ μμ νλ sum_to ν¨μμ λλ€. λ°λΌμ DeZeroμ Add ν΄λμ€λ₯Ό λ€μκ³Ό κ°μ΄ μμ ν΄μΌ ν©λλ€. Mul, Sub, Div ν΄λμ€ μμ μμ νμ¬μΌ ν©λλ€.
class Add(Function):
def forward(self, x0, x1):
self.x0_shape, self.x1_shape = x0.shape, x1.shape
y = x0 + x1
return y
def backward(self, gy):
gx0, gx1 = gy, gy
if self.x0_shape != self.x1_shape: # for broadcast
gx0 = dezero.functions.sum_to(gx0, self.x0_shape)
gx1 = dezero.functions.sum_to(gx1, self.x1_shape)
return gx0, gx1
λ§μ§λ§μΌλ‘ λ€μκ³Ό κ°μ ν μ€νΈλ€μ μ§νν μ μμ΅λλ€.
import numpy as np
from dezero import Variable
x0 = Variable(np.array([1, 2, 3]))
x1 = Variable(np.array([10]))
y = x0 + x1
print(y) # variable([11 12 13])
y.backward()
print(x0.grad) # variable([1 1 1])
print(x1.grad) # variable([3])
y = x0 * x1
print(y) # variable([10 20 30])
x0.cleargrad()
x1.cleargrad()
y.backward()
print(x0.grad) # variable([10 10 10])
print(x1.grad) # variable([6])'DeZero > π»μ 4κ³ μ§' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
| step42. μ ν νκ· (0) | 2023.02.18 |
|---|---|
| step41. νλ ¬μ κ³± (0) | 2023.02.16 |
| step39. ν©κ³ ν¨μ (0) | 2023.02.14 |
| step38. νμ λ³ν ν¨μ (0) | 2023.02.13 |
| step37. ν μλ₯Ό λ€λ£¨λ€ (0) | 2023.02.12 |