반응형
import numpy as np
# linear regression
x = np.random.rand(1000)
y = x * 2 + np.random.randn()
print(x, y)
# parameter
w = 0.0
b = 0.0
# hyperparameter
lr = 0.01
epochs = 100000
def linear_regression(w1:float, b1:float, x1:float) -> float:
# TODO
return w1 * x1 + b1
def loss_function(x2:np.array, y2:np.array, w2:float, b2:float) -> float:
# TODO
yhat = w2 * x2 + b2
return np.divide(np.sum((y2 - yhat) ** 2), x.shape[0])
def gradient_descent(x3:np.array, y3:np.array, w3:float, b3:float, lr:float) -> tuple:
# TODO
dw = 0.0
db = 0.0
N = x3.shape[0]
for xi, yi in zip(x3, y3):
dw += -2 * xi * (yi - linear_regression(w3, b3, xi))
db += -2 * (yi - linear_regression(w3, b3, xi))
w3 -= dw * lr * (1/N)
b3 -= db * lr * (1/N)
return w3, b3
for i in range(epochs):
w, b = gradient_descent(x,y,w,b,lr)
yhat = w * x + b
loss = np.divide(np.sum((y - yhat) ** 2), x.shape[0])
print(f"{i} weight {w:.4f} bias {b:.4f} loss {loss:.4f}")
반응형
'Machine Learning' 카테고리의 다른 글
metric learning / deep metric learning에 관하여 (1) | 2024.01.31 |
---|---|
tf 에서 confusion matrix에서 actual value와 prediction value가 다른 경우 (0) | 2023.08.18 |
Cloud(GCP)를 이용한 ML-Pipeline 만들기 - 1. Vertex AI AutoML (0) | 2023.05.03 |
label studio ml backend 구축 (1. label studio 소개, import, export 및 labeling 머지!) (0) | 2022.12.12 |
dvc 를 이용해서 데이터 버전 관리하기 (ft. mlops) (0) | 2022.04.14 |