Skip to content

Instantly share code, notes, and snippets.

@JustinSDK
Created August 11, 2021 06:48
Show Gist options
  • Save JustinSDK/ed1ae5364a9ca4d7f10012d19795d8f3 to your computer and use it in GitHub Desktop.
Save JustinSDK/ed1ae5364a9ca4d7f10012d19795d8f3 to your computer and use it in GitHub Desktop.
NumPy API至PyTorch API
import torch
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(0)
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 torch.tensor([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 = torch.from_numpy(cv2.imread('PolynomialRegression-1.JPG', cv2.IMREAD_GRAYSCALE)) # 轉為張量
idx = torch.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 = torch.tensor([1.0, 0.0]),
x = x,
y = y
)
x = torch.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