Skip to content

Instantly share code, notes, and snippets.

@JustinSDK
Created August 11, 2021 06:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JustinSDK/a5351166e2b5569172b901c26b380bc5 to your computer and use it in GitHub Desktop.
Save JustinSDK/a5351166e2b5569172b901c26b380bc5 to your computer and use it in GitHub Desktop.
NumPy實現線性迴歸(二)
import numpy as np
import cv2
import matplotlib.pyplot as plt
def model(x, w, b):
return w * x + b
def mse_loss(p, y):
return ((p - y) ** 2).mean()
def grad_f(x, y, p, w, b):
# 基於損失函數手動偏微分
def dloss_dp(p, y):
return (2 * (p - y)) / p.size
def dp_dw(x, w, b):
return x
def dp_db(x, w, b):
return 1.0
# 反向傳播計算梯度
dloss_dtp = dloss_dp(p, y)
dloss_dw = dloss_dtp * dp_dw(x, w, b)
dloss_db = dloss_dtp * dp_db(x, w, b)
return np.array([dloss_dw.sum(), dloss_db.sum()])
def training_loop(epochs, lr, params, x, y, verbose = False):
for epoch in range(1, epochs + 1):
w, b = params
p = model(x, w, b)
grad = grad_f(x, y, p, w, b)
params = params - lr * grad
if verbose:
print('週期', epoch, '--')
print('\t損失:', float(mse_loss(p, y)))
print('\t模型參數:', params)
return params
# https://openhome.cc/Gossip/DCHardWay/images/PolynomialRegression-1.JPG
img = cv2.imread('PolynomialRegression-1.JPG', cv2.IMREAD_GRAYSCALE)
idx = np.where(img < 127) # 黑點的索引
x = idx[1]
y = -idx[0] + img.shape[0] # 反轉 y 軸
plt.gca().set_aspect(1)
plt.scatter(x, y)
w, b = training_loop(
epochs = 100,
lr = 0.001, # 學習率
params = np.array([1.0, 0.0]),
x = x,
y = y
)
x = np.linspace(0, 50, 50)
y = w * x + b
plt.plot(x, y)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment