यंत्र अधिगम
सीखना लूपिंग है
एक एमएल मॉडल को कई बार डेटा पर लूप करके प्रशिक्षित किया जाता है।
प्रत्येक पुनरावृत्ति के लिए, वजन मान समायोजित किए जाते हैं।
प्रशिक्षण पूरा हो गया है जब पुनरावृत्तियों लागत को कम करने में विफल रहता है ।
सर्वोत्तम फिट की लाइन खोजने के लिए मुझे प्रशिक्षित करें:
ढतला हुआ वंश
एआई समस्याओं को हल करने के लिए ग्रेडिएंट डिसेंट एक लोकप्रिय एल्गोरिथम है।
एक साधारण रैखिक प्रतिगमन मॉडल का उपयोग एक ढाल वंश को प्रदर्शित करने के लिए किया जा सकता है।
एक रेखीय प्रतिगमन का लक्ष्य एक रैखिक ग्राफ को (x, y) बिंदुओं के एक सेट में फिट करना है। इसे गणित के फॉर्मूले से हल किया जा सकता है। लेकिन एक मशीन लर्निंग एल्गोरिथम भी इसे हल कर सकता है।
ऊपर दिया गया उदाहरण यही करता है।
यह एक स्कैटर प्लॉट और एक रैखिक मॉडल (y = wx + b) से शुरू होता है।
फिर यह मॉडल को एक ऐसी रेखा खोजने के लिए प्रशिक्षित करता है जो प्लॉट के अनुकूल हो। यह वजन (ढलान) और रेखा के पूर्वाग्रह (अवरोध) को बदलकर किया जाता है।
नीचे एक ट्रेनर ऑब्जेक्ट के लिए कोड है जो इस समस्या (और कई अन्य समस्याओं) को हल कर सकता है।
एक ट्रेनर वस्तु
एक ट्रेनर ऑब्जेक्ट बनाएं जो दो सरणियों (xArr, yArr) में किसी भी संख्या में (x, y) मान ले सके।
वजन और पूर्वाग्रह दोनों को शून्य पर सेट करें।
एक सीखने का स्थिरांक (सीखना) निर्धारित किया जाना चाहिए, और एक लागत चर परिभाषित किया जाना चाहिए:
उदाहरण
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;
}
कॉस्ट फंक्शन का दूसरा नाम एरर फंक्शन है ।
फ़ंक्शन में प्रयुक्त सूत्र वास्तव में यह है:
- ई त्रुटि है (लागत)
- N प्रेक्षणों की कुल संख्या है (अंक)
- y प्रत्येक अवलोकन का मान (लेबल) है
- x प्रत्येक अवलोकन का मान (विशेषता) है
- मी ढलान (वजन) है
- बी अवरोधन (पूर्वाग्रह) है
- एमएक्स + बी भविष्यवाणी है
- 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>