การเรียนรู้ของเครื่อง
การเรียนรู้คือการวนซ้ำ
โมเดล ML ได้รับการฝึกโดย การ วนซ้ำข้อมูลหลายครั้ง
สำหรับการทำซ้ำแต่ละครั้งค่าน้ำหนักจะถูกปรับ
การฝึกอบรมจะเสร็จสมบูรณ์เมื่อการทำซ้ำล้มเหลวในการลดต้นทุน
ฝึกให้ฉันหาแนวที่เหมาะสมที่สุด:
โคตรลาด
Gradient Descentเป็นอัลกอริธึมยอดนิยมสำหรับแก้ปัญหา AI
ตัวแบบ การถดถอยเชิงเส้นอย่างง่ายสามารถใช้เพื่อสาธิตการลงระดับเกรเดียนต์ได้
เป้าหมายของการถดถอยเชิงเส้นคือการปรับกราฟเชิงเส้นให้พอดีกับชุดของจุด (x,y) สามารถแก้ไขได้ด้วยสูตรคณิตศาสตร์ แต่อัลกอริธึมการเรียนรู้ของเครื่องก็สามารถแก้ปัญหานี้ได้
นี่คือสิ่งที่ตัวอย่างด้านบนทำ
มันเริ่มต้นด้วยพล็อตกระจายและโมเดลเชิงเส้น (y = wx + b)
จากนั้นจะฝึกโมเดลเพื่อหาเส้นที่เข้ากับโครงเรื่อง ทำได้โดยการเปลี่ยนน้ำหนัก (ความชัน) และความเอนเอียง (การสกัดกั้น) ของเส้น
ด้านล่างนี้คือโค้ดสำหรับTrainer Objectที่สามารถแก้ปัญหานี้ได้ (และปัญหาอื่นๆ อีกมากมาย)
วัตถุเทรนเนอร์
สร้างวัตถุ Trainer ที่สามารถรับค่า (x,y) จำนวนเท่าใดก็ได้ในสองอาร์เรย์ (xArr,yArr)
ตั้งค่าทั้งน้ำหนักและอคติเป็นศูนย์
ต้องตั้งค่าคงที่การเรียนรู้ (learnc) และต้องกำหนดตัวแปรต้นทุน:
ตัวอย่าง
function Trainer(xArray, yArray) {
this.xArr = xArray;
this.yArr = yArray;
this.points = this.xArr.length;
this.learnc = 0.00001;
this.weight = 0;
this.bias = 1;
this.cost;
ฟังก์ชันต้นทุน
วิธีมาตรฐานในการแก้ปัญหาการถดถอยคือการใช้ "ฟังก์ชันต้นทุน" ที่วัดว่าวิธีแก้ปัญหานั้นดีเพียงใด
ฟังก์ชันนี้ใช้น้ำหนักและอคติจากโมเดล (y = wx + b) และส่งคืนข้อผิดพลาด โดยขึ้นอยู่กับว่าเส้นพอดีกับพล็อตมากน้อยเพียงใด
วิธีคำนวณข้อผิดพลาดนี้คือวนซ้ำจุด (x,y) ทั้งหมดในโครงเรื่อง และรวมระยะห่างระหว่างค่า y ของแต่ละจุดกับเส้น
วิธีธรรมดาที่สุดคือการกำหนดระยะห่าง (เพื่อให้แน่ใจว่ามีค่าบวก) และเพื่อให้ฟังก์ชันข้อผิดพลาดสามารถหาอนุพันธ์ได้
this.costError = function() {
total = 0;
for (let i = 0; i < this.points; i++) {
total += (this.yArr[i] - (this.weight * this.xArr[i] + this.bias)) **2;
}
return total / this.points;
}
อีกชื่อหนึ่งสำหรับฟังก์ชันต้นทุนคือError Function
สูตรที่ใช้ในฟังก์ชันเป็นจริงดังนี้:
- Eคือข้อผิดพลาด (ต้นทุน)
- Nคือจำนวนการสังเกตทั้งหมด (จุด)
- yคือค่า (ฉลาก) ของการสังเกตแต่ละครั้ง
- xคือค่า (คุณสมบัติ) ของการสังเกตแต่ละครั้ง
- mคือความชัน (น้ำหนัก)
- bคือการสกัดกั้น (อคติ)
- mx + bคือการทำนาย
- 1/N * N∑1 is the squared mean value
The Train Function
We will now run a gradient descent.
The gradient descent algorithm should walk the cost function towards the best line.
Each iteration should update both m and b towards a line with a lower cost (error).
To do that, we add a train function that loops over all the data many times:
this.train = function(iter) {
for (let i = 0; i < iter; i++) {
this.updateWeights();
}
this.cost = this.costError();
}
An Update Weights Function
The train function above should update the weights and biases in each iteration.
The direction to move is calculated using two partial derivatives:
this.updateWeights = function() {
let wx;
let w_deriv = 0;
let b_deriv = 0;
for (let i = 0; i < this.points; i++) {
wx = this.yArr[i] - (this.weight * this.xArr[i] + this.bias);
w_deriv += -2 * wx * this.xArr[i];
b_deriv += -2 * wx;
}
this.weight -= (w_deriv / this.points) * this.learnc;
this.bias -= (b_deriv / this.points) * this.learnc;
}
Create Your Own Library
Library Code
function Trainer(xArray, yArray) {
this.xArr = xArray;
this.yArr = yArray;
this.points = this.xArr.length;
this.learnc = 0.000001;
this.weight = 0;
this.bias = 1;
this.cost;
// Cost Function
this.costError = function() {
total = 0;
for (let i = 0; i < this.points; i++) {
total += (this.yArr[i] - (this.weight * this.xArr[i] + this.bias)) **2;
}
return total / this.points;
}
// Train Function
this.train = function(iter) {
for (let i = 0; i < iter; i++) {
this.updateWeights();
}
this.cost = this.costError();
}
// Update Weights Function
this.updateWeights = function() {
let wx;
let w_deriv = 0;
let b_deriv = 0;
for (let i = 0; i < this.points; i++) {
wx = this.yArr[i] - (this.weight * this.xArr[i] + this.bias);
w_deriv += -2 * wx * this.xArr[i];
b_deriv += -2 * wx;
}
this.weight -= (w_deriv / this.points) * this.learnc;
this.bias -= (b_deriv / this.points) * this.learnc;
}
} // End Trainer Object
Now you can include the library in HTML:
<script src="myailib.js"></script>