التعلم الالي

التعلم هو التكرار

يتم تدريب نموذج ML عن طريق تكرار البيانات عدة مرات.

لكل تكرار ، يتم تعديل قيم الوزن .

يكتمل التدريب عندما تفشل التكرارات في تقليل التكلفة .

دربني على العثور على الخط الأنسب:


نزول متدرج

نزول التدرج هو خوارزمية شائعة لحل مشاكل الذكاء الاصطناعي.

يمكن استخدام نموذج الانحدار الخطي البسيط لإثبات الانحدار المتدرج.

الهدف من الانحدار الخطي هو ملاءمة الرسم البياني الخطي لمجموعة من النقاط (س ، ص). يمكن حل ذلك باستخدام صيغة رياضية. لكن يمكن لخوارزمية التعلم الآلي أيضًا حل هذه المشكلة.

هذا ما يفعله المثال أعلاه.

يبدأ بمخطط مبعثر ونموذج خطي (y = wx + b).

ثم يقوم بتدريب النموذج لإيجاد خط يناسب الحبكة. يتم ذلك عن طريق تغيير الوزن (الميل) والانحياز (التقاطع) للخط.

يوجد أدناه رمز كائن المدرب الذي يمكنه حل هذه المشكلة (والعديد من المشكلات الأخرى).


كائن المدرب

قم بإنشاء كائن المدرب يمكنه أخذ أي عدد من قيم (س ، ص) في صفيفين (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) وترجع خطأ ، بناءً على مدى ملاءمة الخط للرسم.

تتمثل طريقة حساب هذا الخطأ في إجراء حلقة عبر جميع النقاط (س ، ص) في الرسم ، وجمع مسافات المربعات بين قيمة 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 هي قيمة (ميزة) كل ملاحظة
  • م هو المنحدر (الوزن)
  • ب هو اعتراض (تحيز)
  • 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>