ພື້ນຖານ Deep Learning
Neural network, loss ແລະ optimizer ໃນ 20 ແຖວ.
Neural network ແມ່ນຊັ້ນຂອງການຄູນ matrix ສະຫຼັບກັບ activation (ReLU). ການຝຶກແມ່ນການປັບ weight ໃຫ້ loss ນ້ອຍລົງດ້ວຍ gradient descent ແລະ optimizer ເຊັ່ນ Adam.
ໂຄດຕົວຢ່າງຝຶກ MLP ຈຳແນກຕົວເລກລາຍມື 8×8 (ມາກັບ scikit-learn ບໍ່ຕ້ອງດາວໂຫຼດ) ໄດ້ຄວາມແມ່ນຍຳປະມານ 90%.
y = softmax( W₂ · ReLU( W₁·x + b₁ ) + b₂ )
loss = −Σ y_true · log y
w ← w − lr · ∂loss/∂w
| ສ່ວນ | ໜ້າທີ່ |
|---|---|
| Dense / Linear | ຄູນ matrix + bias |
| Conv2d | ຊອກລາຍໃນຮູບດ້ວຍ filter ນ້ອຍໆ |
| BatchNorm · Dropout | ໃຫ້ຝຶກສະຖຽນ ແລະ ບໍ່ overfit |
| Adam | optimizer ທີ່ປັບ learning rate ໃຫ້ແຕ່ລະ weight |
ສິ່ງທີ່ຄວນຈື່
- ທຸກ generative model ໃນຊີຣີສ໌ນີ້ສ້າງຈາກຊິ້ນສ່ວນເຫຼົ່ານີ້
- ສິ່ງທີ່ປ່ຽນໄປໃນແຕ່ລະບົດ ແມ່ນ loss function
import torch, torch.nn as nn
from sklearn.datasets import load_digits
torch.manual_seed(0)
X, y = load_digits(return_X_y=True) # 1,797 images of 8x8 digits
X = torch.tensor(X / 16.0, dtype=torch.float32)
y = torch.tensor(y)
train, test = slice(0, 1400), slice(1400, None)
model = nn.Sequential(nn.Linear(64, 128), nn.ReLU(),
nn.Linear(128, 10)) # 10 classes
opt = torch.optim.Adam(model.parameters(), lr=1e-2)
for epoch in range(60):
loss = nn.functional.cross_entropy(model(X[train]), y[train])
opt.zero_grad(); loss.backward(); opt.step()
if epoch % 20 == 19:
acc = (model(X[test]).argmax(1) == y[test]).float().mean()
print(f"epoch {epoch + 1:2d} loss {loss.item():.3f} test acc {acc:.2%}")
ຜົນລັບ
epoch 20 loss 0.158 test acc 87.66% epoch 40 loss 0.042 test acc 89.67% epoch 60 loss 0.020 test acc 90.93%
- #GenerativeAI
- #DeepLearning
- #PythonສຳລັບຄົນLaos
- #AI
- #ບອກຕໍ່ຄວາມຮູ້