Example #1
0
"""
교재 p.160 그림 5-15의 빈칸 채우기.
apple = 100원, n_a = 2개
orange = 150원, n_o = 3개
tax = 1.1
라고 할 때, 전체 과일 구매 금액을 AddLayer와 MultiplyLayer를 사용해서 계산하세요.
df/dapple, df/dn_a, df/dorange, df/dn_o, df/dtax 값들도 각각 계산하세요.
"""
from ch05.ex01_basic_layer import MultiplyLayer, AddLayer

apple, n_a = 100, 2
orange, n_o = 150, 3
tax = 1.1

apple_mul = MultiplyLayer()  # 뉴런 생성
apple_price = apple_mul.forward(apple, n_a)  # forward propagation
print('apple_price =', apple_price)

orange_mul = MultiplyLayer()  # 뉴런 생성
orange_price = orange_mul.forward(orange, n_o)  # forward propagation
print('orange_price =', orange_price)

add_gate = AddLayer()
price = add_gate.forward(apple_price, orange_price)
print('price =', price)

tax_mul = MultiplyLayer()
total_price = tax_mul.forward(price, tax)
print('total_price =', total_price)

# 역전파(backward propagation, back-propagation)
Example #2
0
교재 p.160 그림 5-15의 빈칸 채우기
apple = 100원, n_a = 2개
orange = 150원, n_o = 3ro
tax = 1.1
라고 할 때, 전체 과일 구매 금액을 AddLayer와 MultiplyLayer를 사용해서 계산하세요
df/dapple, df/dn_a, df/dorange, df/dn_o, df/dtax 값들도 각각 계산하세요.
"""
from ch05.ex01_basic_layer import MultiplyLayer, AddLayer

# 초기값 설정:
apple, n_a, orange, n_o, tax = 100, 2, 150, 3, 1.1

# FORWARD PROPAGATION: ((apple * n_a) + (orange * n_o)) * tax
# (apple * n_a), (orange * n_o)
# MultiplyLayer 생성
mul_layer = MultiplyLayer()
mul_apple = mul_layer.forward(apple, n_a)
mul_orange = mul_layer.forward(orange, n_o)  # 새로운 뉴런을 생성했어야함

# (apple * n_a) + (orange * n_o)
add_layer = AddLayer()
add_ao = add_layer.forward(mul_apple, mul_orange)

# ((apple * n_a) + (orange * n_o)) * tax
mul_add_ao = mul_layer.forward(add_ao, tax)

# BACKWARD PROPAGATION
# mul_add_ao -> add_ao
ds3, dt = mul_layer.backward(1)
print('dse =', ds3)
print('dt =', dt)
Example #3
0
    f = q * z 이므로, df/dq = z, df/dz = q
    위의 결과를 이용하면,
    df/dx = (df/dq)(dq/dx) = z
    df/dy = (df/dq)(dq/dy) = z

numerical_gradient 함수에서 계산된 결과와 비교
"""
from ch05.ex01_basic_layer import AddLayer, MultiplyLayer

x, y, z = -2, 5, -4

add_gate = AddLayer()
q = add_gate.forward(x, y)
print('q =', q)

mul_gate = MultiplyLayer()
f = mul_gate.forward(q, z)
print('f =', f)

dq, dz = mul_gate.backward(1)
print('dq =', dq)
print('dz =', dz)

dx, dy = add_gate.backward(dq)
print('dx =', dx)
print('dy =', dy)


def f(x, y, z):
    return (x + y) * z
"""
The solution for ch05 ex03
"""
from ch05.ex01_basic_layer import MultiplyLayer, AddLayer

apple, n_a = 100, 2
orange, n_o = 150, 3
tax = 1.1

# 뉴런 생성 및 forward propagation

# get the total price of apple and orange
apple_mul = MultiplyLayer()  # 뉴런 생성
apple_price = apple_mul.forward(apple, n_a)  #forward propagation
print('apple_price =', apple_price)

orange_mul = MultiplyLayer()
orange_price = orange_mul.forward(orange, n_o)  #forward propagation
print('orange_price =', orange_price)

# Add the prices of apple and orange
add_gate = AddLayer()
price = add_gate.forward(apple_price, orange_price)
print('price =', price)

# get the total price with tax
tax_multiplier = MultiplyLayer()
total_price = tax_multiplier.forward(price, tax)
print('total_price', total_price)

# Backward propagation