Makine öğrenme

Öğrenme Döngüseldir

Bir ML modeli, veriler üzerinde birden çok kez Döngü yapılarak Eğitilir .

Her yineleme için Ağırlık Değerleri ayarlanır.

Yinelemeler Maliyeti Azaltamadığında eğitim tamamlanır .

En uygun çizgiyi bulmak için beni eğit:


Dereceli alçalma

Gradient Descent , AI sorunlarını çözmek için popüler bir algoritmadır.

Gradyan inişini göstermek için basit bir Lineer Regresyon Modeli kullanılabilir.

Doğrusal bir regresyonun amacı, doğrusal bir grafiği bir dizi (x,y) noktasına sığdırmaktır. Bu bir matematik formülü ile çözülebilir. Ancak bir Makine Öğrenimi Algoritması da bunu çözebilir.

Yukarıdaki örneğin yaptığı şey budur.

Bir dağılım grafiği ve doğrusal bir model (y = wx + b) ile başlar.

Daha sonra modeli, arsaya uyan bir çizgi bulmak için eğitir. Bu, çizginin ağırlığını (eğim) ve eğimini (kesme noktası) değiştirerek yapılır.

Aşağıda, bu sorunu (ve diğer birçok sorunu) çözebilecek bir Eğitmen Nesnesinin kodu bulunmaktadır .


Bir Eğitmen Nesnesi

İki dizide (xArr,yArr) herhangi bir sayıda (x,y) değeri alabilen bir Trainer nesnesi oluşturun.

Hem ağırlığı hem de önyargıyı sıfıra ayarlayın.

Bir öğrenme sabiti (öğrenme) ayarlanmalı ve bir maliyet değişkeni tanımlanmalıdır:

Örnek

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;

Maliyet fonksiyonu

Bir regresyon problemini çözmenin standart bir yolu, çözümün ne kadar iyi olduğunu ölçen bir "Maliyet Fonksiyonu"dur.

İşlev, modelden (y = wx + b) ağırlık ve sapmayı kullanır ve çizginin bir grafiğe ne kadar iyi uyduğuna bağlı olarak bir hata döndürür.

Bu hatayı hesaplamanın yolu, çizimdeki tüm (x,y) noktaları arasında döngü yapmak ve her noktanın y değeri ile doğru arasındaki kare mesafeleri toplamaktır.

En geleneksel yol, mesafelerin karesini almak (pozitif değerleri sağlamak için) ve hata fonksiyonunu türevlenebilir hale getirmektir.

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;
}

Maliyet İşlevinin diğer bir adı Hata İşlevidir .

Fonksiyonda kullanılan formül aslında şudur:

formül
  • E hatadır (maliyet)
  • N , toplam gözlem sayısıdır (puan)
  • y , her gözlemin değeridir (etiket).
  • x , her bir gözlemin değeridir (özelliğidir).
  • m eğimdir (ağırlık)
  • b kesişmedir (önyargı)
  • mx + b tahmindir
  • 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>