# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # 손실 줄이기

# ## 반복방식

# ### 손실계산 예제

# * 가정: $y' = prediction(x) = b + w_1 x_1$

def prediction(b, w1, x1):
    return b + w1 * x1


# * 데이터 하나의 제곱손실($L_2$ 손실)은 다음과 같다.
#
# $$L_2 = (y - prediction(x))^2$$

# * 평균제곱오차(MSE)는 다음과 같다.
#
# $$MSE = \frac 1 N \sum_{(x,y)\in D}(y - prediction(x))^2$$

def L2_loss(y, b, w1, x1):
    return (y - prediction(b, w1, x1))**2


# * 데이터셋: 
#
# ```
# [[10, 5],
#  [12, 6]]
# ```

# * 변수 설정 1: $b = w_1 = 0$

# * 예측하기 1: 

prediction(0, 0, 10)

prediction(0, 0, 12)

# * 손실계산 1

e1 = L2_loss(5, 0, 0, 10)
print(e1)

e2 = L2_loss(6, 0, 0, 12)
print(e2)

mse = (e1 + e2)/2
print(mse)

# * 변수 설정 2: $b = w_1 = 0.1$
#
# * 예측하기 2

prediction(0.1, 0.1, 10)

prediction(0.1, 0.1, 12)

# * 손실계산 2

e1 = L2_loss(5, 0.1, 0.1, 10)
print(e1)

e2 = L2_loss(6, 0.1, 0.1, 12)
print(e2)

mse = (e1 + e2)/2
print(mse)

# * 변수 설정 3: $b = 0.1, w_1 = 0.2$
#
# * 예측하기 3

prediction(0.1, 0.2, 10)

prediction(0.1, 0.2, 12)

# * 손실계산 3

e1 = L2_loss(5, 0.1, 0.2, 10)
print(e1)

e2 = L2_loss(6, 0.1, 0.2, 12)
print(e2)

mse = (e1 + e2)/2
print(mse)

# 기타 등등...

StarcoderdataJupyterScriptsDedupFiltered