Skip to content

Instantly share code, notes, and snippets.

View JustinSDK's full-sized avatar
👨‍👩‍👧
Working from home

Justin Lin JustinSDK

👨‍👩‍👧
Working from home
View GitHub Profile
@JustinSDK
JustinSDK / AlgebraicType.java
Last active October 13, 2021 02:31
代數資料型態:Visitor 實現
package cc.openhome;
import java.util.Arrays;
interface List {
default Integer head() { return null; }
default List tail() { return null; }
Integer sum();
// 實現模式比對
@JustinSDK
JustinSDK / AlgebraicType.java
Last active October 12, 2021 06:22
代數資料型態:Java 17
package cc.openhome;
import java.util.Arrays;
sealed interface List<T> permits Nil, Cons<T> {
default T head() { return null; }
default List<T> tail() { return null; }
}
final class Nil implements List {
@JustinSDK
JustinSDK / regression_torch4.py
Last active August 14, 2021 09:29
PyTorch求線性迴歸
import torch
import cv2
import matplotlib.pyplot as plt
def training_loop(epochs, lr, params, x, y, verbose = False):
mx = torch.unsqueeze(x, 1).float()
my = torch.unsqueeze(y, 1).float()
model = torch.nn.Linear(mx.size(1), my.size(1))
mse_loss = torch.nn.MSELoss()
@JustinSDK
JustinSDK / regression_torch3.py
Created August 11, 2021 07:08
使用 Optimizer
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()
@JustinSDK
JustinSDK / regression_torch2.py
Last active August 11, 2021 07:03
PyTorch反向傳播求梯度
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()
@JustinSDK
JustinSDK / regression_torch.py
Created August 11, 2021 06:48
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()
@JustinSDK
JustinSDK / regression_numpy2.py
Created August 11, 2021 06:37
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()
@JustinSDK
JustinSDK / regression_numpy.py
Last active August 11, 2021 06:30
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()
@JustinSDK
JustinSDK / kernel_method2.py
Created July 14, 2021 01:30
雙拋物面提升維度
import numpy as np
import matplotlib.pyplot as plt
# 雙抛物面公式
def f(x, y):
return x ** 2 / 4 - y ** 2 / 4
# https://openhome.cc/Gossip/DCHardWay/ab.csv
data = np.loadtxt('ab.csv', delimiter=',')
@JustinSDK
JustinSDK / kernel_method.py
Last active July 14, 2021 01:22
二維線性不可分
import numpy as np
import matplotlib.pyplot as plt
# https://openhome.cc/Gossip/DCHardWay/ab.csv
data = np.loadtxt('ab.csv', delimiter=',')
a = data[:,0]
b = data[:,1]
label = data[:,2]