Deep Learning เบื้องต้นด้วย PyTorch
Jasmin Ludolf
Senior Data Science Content Developer, DataCamp
$$
อนุพันธ์แทนความชันของเส้นโค้ง
$$
$$

นี่คือฟังก์ชันนูน (convex function)

นี่คือฟังก์ชันไม่นูน (non-convex function)

$$

$$

$$
สมมติว่าเครือข่ายประกอบด้วยสามเลเยอร์:

# Run a forward pass model = nn.Sequential(nn.Linear(16, 8), nn.Linear(8, 4), nn.Linear(4, 2)) prediction = model(sample)# Calculate the loss and gradients criterion = CrossEntropyLoss() loss = criterion(prediction, target) loss.backward()
# Access each layer's gradients
model[0].weight.grad
model[0].bias.grad
model[1].weight.grad
model[1].bias.grad
model[2].weight.grad
model[2].bias.grad
# Learning rate is typically small lr = 0.001 # Update the weights weight = model[0].weight weight_grad = model[0].weight.gradweight = weight - lr * weight_grad# Update the biases bias = model[0].bias bias_grad = model[0].bias.gradbias = bias - lr * bias_grad
$$
สำหรับฟังก์ชันไม่นูน จะใช้ gradient descent
PyTorch ทำให้กระบวนการนี้ง่ายขึ้นด้วย optimizers
import torch.optim as optim # Create the optimizer optimizer = optim.SGD(model.parameters(), lr=0.001)# Perform parameter updates optimizer.step()
Deep Learning เบื้องต้นด้วย PyTorch