စက်သင်ယူခြင်း။
သင်ယူခြင်းသည် Looping ဖြစ်သည်။
ML မော်ဒယ် ကို ဒေတာကို အကြိမ်များစွာ လှည့် ပတ် ခြင်းဖြင့် လေ့ကျင့်သင်ကြား ပေးသည်။
တစ်ကြိမ်စီတိုင်းအတွက် အလေးချိန်တန်ဖိုးများ ကို ချိန်ညှိထားသည်။
ကုန်ကျစရိတ်ကို လျှော့ချရန် ထပ်ခါတလဲလဲ ပျက်ကွက်ပါက သင်တန်းပြီးမြောက် ပါသည်။
အသင့်တော်ဆုံးလိုင်းကို ရှာတွေ့အောင် လေ့ကျင့်ပေးပါ။
Gradient အဆင်း
Gradient Descent သည် AI ပြဿနာများကိုဖြေရှင်းရန်အတွက် ရေပန်းစားသော algorithm တစ်ခုဖြစ်သည်။
ရိုးရှင်းသော Linear Regression Model ကို gradient ဆင်းသက်မှုကို သရုပ်ပြရန် အသုံးပြုနိုင်သည်။
linear regression ၏ ပန်းတိုင်မှာ linear graph တစ်ခုအား (x၊y) အမှတ်များ အစုံအလင်နှင့် ကိုက်ညီရန် ဖြစ်သည်။ ဒါကို သင်္ချာပုံသေနည်းနဲ့ ဖြေရှင်းနိုင်ပါတယ်။ ဒါပေမယ့် Machine Learning Algorithm က ဒါကို ဖြေရှင်းပေးနိုင်ပါတယ်။
ဤသည်မှာ အထက်ဖော်ပြပါ ဥပမာအတိုင်း လုပ်ဆောင်သည်။
၎င်းသည် scatter plot နှင့် linear model (y = wx + b) ဖြင့် စတင်သည်။
ထို့နောက် ဇာတ်ညွှန်းနှင့်ကိုက်ညီသော လိုင်းကိုရှာဖွေရန် မော်ဒယ်ကို လေ့ကျင့်ပေးသည်။ ၎င်းကို မျဉ်းကြောင်း၏ အလေးချိန် (လျှောစောက်) နှင့် ဘက်လိုက်မှု (ကြားဖြတ်) ကို ပြောင်းလဲခြင်းဖြင့် လုပ်ဆောင်သည်။
အောက်တွင် ဤပြဿနာ (နှင့် အခြားသော ပြဿနာများစွာ) ကို ဖြေရှင်းနိုင် သော Trainer Object အတွက် ကုဒ် ဖြစ်ပါသည်။
Trainer Object တစ်ခု
array နှစ်ခုတွင် (xArr,yArr) အရေအတွက်မည်သည့် (x,y) တန်ဖိုးများကိုမဆို ယူနိုင်သော Trainer object တစ်ခုကို ဖန်တီးပါ။
အလေးချိန်နှင့် ဘက်လိုက်မှုကို သုညဟု သတ်မှတ်ပါ။
သင်ယူမှုကိန်းသေ (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 တန်ဖိုးနှင့် စာကြောင်းကြားရှိ စတုရန်းအကွာအဝေးများကို ပေါင်းစည်းရန်ဖြစ်သည်။
သမားရိုးကျဆုံးနည်းလမ်းမှာ အကွာအဝေးများကို နှစ်ထပ် (အပြုသဘောဆောင်သောတန်ဖိုးများသေချာစေရန်) နှင့် error 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;
}
Cost Function ၏ နောက်ထပ်အမည်မှာ Error Function ဖြစ်သည်။
Function မှာသုံးတဲ့ဖော်မြူလာက တကယ်တော့ ဒီလိုပါ။
- E သည် error (ကုန်ကျစရိတ်)၊
- N သည် လေ့လာတွေ့ရှိချက်စုစုပေါင်း (အမှတ်များ)၊
- y သည် ကြည့်ရှုမှုတစ်ခုစီ၏ တန်ဖိုး (တံဆိပ်) ဖြစ်သည်။
- x သည် လေ့လာမှုတစ်ခုစီ၏ တန်ဖိုး (feature) ဖြစ်သည်။
- m သည် slope (အလေးချိန်)၊
- 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>