lucasw
大佬教麻
你可以叫我 000 / Lucas
建國中學資訊社37th學術長
建國中學電子計算機研習社44th學術
校際交流群創群者
不會音遊不會競程不會數學的笨
資訊技能樹亂點但都一樣爛
專案爛尾大師
IZCC x SCINT x Ruby Taiwan 聯課負責人
建國中學電子計算機研習社44th學術+總務
是的,我是總務。在座的你各位下次記得交社費束脩給我
技能樹貧乏
想拿機器學習做專題結果只學會使用API
上屆社展烙跑到資訊社的叛徒
科班墊神
還記得我們上回實作的
MLP(Multilayer Perceptron)
or
NN(Neural Network)
or
DNN(Deep Neural Network)
or
FNN(Feedforward Neural Network)
by chatgpt
而這
便是現在諸多機器學習相關資料結構的一塊必備零件
在進入更複雜的結構時
你會發現很像在拼拼圖
將一塊一塊不同功能的部件排進去
這部分也就是前饋神經網路(Feedforward NN)
通常與學習相關的部分最主要就是這東西去處理的
其他部件通常是針對使用用途去做特別的一些處理
而這也代表我們僅使用MLP便能達到很多功能
這堂課就是要來帶大家玩神經網路
簡單來說就是
xor常常被用來檢測非線性擬合
所以雖然我們跑過更進階的MNIST
不過還是可以來玩玩看xor
import numpy as np
import matplotlib.pyplot as plt
from nn import NeuralNetwork
from act import sigmoid
x_train = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y_train = np.array([[1, 0], [0, 1], [0, 1], [1, 0]])
nn = NeuralNetwork([2, 10, 2], sigmoid)
train_loss = nn.train(x_train, y_train, 10000, 1)
plt.plot(train_loss, label="Train Loss")
plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.legend()
plt.show()
while True:
input()
i = np.random.randint(0, 4)
x = x_train[i]
y = nn.forward(x)
print(f"Input: {x}, Predicted: {y.argmax()}, Expected: {y_train[i].argmax()}")
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from nn import NeuralNetwork
from act import relu, drelu
iris = load_iris()
x_data = (iris.data - np.mean(iris.data, axis=0)) / np.std(iris.data, axis=0)
y_data = np.eye(3)[iris.target]
x_trains, x_tests, y_trains, y_tests = train_test_split(
x_data, y_data, test_size=0.2, stratify=iris.target, random_state=42
)
nn = NeuralNetwork([4, 8, 6, 3], relu, drelu)
train_loss = nn.train(x_trains, y_trains, 500)
nn.predict(x_tests, y_tests)
plt.plot(train_loss, label="Train Loss")
plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.legend()
plt.show()
while True:
input()
i = np.random.randint(0, 150)
x = x_data[i]
y = nn.forward(x)
print(f"Input: {x}, Predicted: {y.argmax()}, Expected: {y_data[i].argmax()}")
你會發現
這東西強的可怕
你會發現
這東西強的可怕
那我們再來玩點別的
By lucasw