โจทย์
การลดเกรเดียนต์ ปรับปรุงน้ำหนักทีละน้อยโดยขยับไปในทิศตรงข้ามกับเกรเดียนต์:
w ← w - learning_rate × dL/dw
จงฝึกนิวรอน sigmoid ด้วยการลดเกรเดียนต์แบบสโตแคสติก ในแต่ละ epoch ให้ไล่ตัวอย่าง ตามลำดับที่กำหนด และสำหรับแต่ละตัวอย่าง:
- z = sample · weights + bias, y = 1 / (1 + e^(-z))
- delta = 2 × (y - target) × y × (1 - y)
- weights[i] ← weights[i] - learning_rate × delta × sample[i] (สำหรับทุก i) แล้วจึง bias ← bias - learning_rate × delta
จงเขียน train_neuron(samples, targets, weights, bias, learning_rate, epochs)
อินพุต
samples: ตัวอย่างฝึก m ตัว แต่ละตัวมี n ค่า
targets: เอาต์พุตที่ถูกต้อง m ค่า (ระหว่าง 0 ถึง 1)
weights, bias: น้ำหนักเริ่มต้น (n ค่า) และไบแอส
learning_rate, epochs: ขนาดก้าวและจำนวนรอบที่วนผ่านข้อมูล
เอาต์พุต
คืนค่าน้ำหนักที่ฝึกแล้ว ตามด้วยไบแอสที่ฝึกแล้ว (n + 1 ค่า)
เงื่อนไข
1 ≤ m ≤ 50, 1 ≤ n ≤ 10, 1 ≤ epochs ≤ 1,000
0 < learning_rate ≤ 5
-5 ≤ แต่ละค่า ≤ 5
ตัวอย่าง
อินพุต:
bias = 0.0
epochs = 1
samples = [
[1.0]
]
targets = [1.0]
weights = [0.0]
learning_rate = 1.0
ผลลัพธ์ที่คาดหวัง:
expected = [0.25, 0.25]
อินพุต:
double bias = 0.0;
long long epochs = 1;
std::vector<std::vector<double>> samples = {
{1.0}
};
std::vector<double> targets = {1.0};
std::vector<double> weights = {0.0};
double learning_rate = 1.0;
ผลลัพธ์ที่คาดหวัง:
std::vector<double> expected = {0.25, 0.25};
อินพุต:
double bias = 0.0;
long epochs = 1;
double[][] samples = new double[][] {
new double[] {1.0}
};
double[] targets = new double[] {1.0};
double[] weights = new double[] {0.0};
double learning_rate = 1.0;
ผลลัพธ์ที่คาดหวัง:
double[] expected = new double[] {0.25, 0.25};
คำอธิบาย
y = σ(0) = 0.5, delta = 2 × (0.5 - 1) × 0.25 = -0.25 น้ำหนัก: 0 - 1 × (-0.25) × 1 = 0.25 ไบแอส: 0 - 1 × (-0.25) = 0.25 ผลลัพธ์: [0.25, 0.25]
อินพุต:
bias = 0.0
epochs = 500
samples = [
[0.0, 0.0],
[0.0, 1.0],
[1.0, 0.0],
[1.0, 1.0]
]
targets = [0.0, 0.0, 0.0, 1.0]
weights = [0.0, 0.0]
learning_rate = 2.0
ผลลัพธ์ที่คาดหวัง:
expected = [6.346558, 6.339863, -9.592092]
อินพุต:
double bias = 0.0;
long long epochs = 500;
std::vector<std::vector<double>> samples = {
{0.0, 0.0},
{0.0, 1.0},
{1.0, 0.0},
{1.0, 1.0}
};
std::vector<double> targets = {0.0, 0.0, 0.0, 1.0};
std::vector<double> weights = {0.0, 0.0};
double learning_rate = 2.0;
ผลลัพธ์ที่คาดหวัง:
std::vector<double> expected = {6.346558, 6.339863, -9.592092};
อินพุต:
double bias = 0.0;
long epochs = 500;
double[][] samples = new double[][] {
new double[] {0.0, 0.0},
new double[] {0.0, 1.0},
new double[] {1.0, 0.0},
new double[] {1.0, 1.0}
};
double[] targets = new double[] {0.0, 0.0, 0.0, 1.0};
double[] weights = new double[] {0.0, 0.0};
double learning_rate = 2.0;
ผลลัพธ์ที่คาดหวัง:
double[] expected = new double[] {6.346558, 6.339863, -9.592092};
คำอธิบาย
นี่คือเกต AND หลังจาก 500 epoch น้ำหนักทั้งสองค่ามีค่ามากและเป็นบวก ส่วนไบแอสติดลบมาก ดังนั้นมีเพียงอินพุต [1, 1] เท่านั้นที่ให้เอาต์พุตมากกว่า 0.5
แหล่งอ้างอิง
โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน