Apprendimento automatico

L'apprendimento è in loop

Un modello ML viene addestrato eseguendo più volte il ciclo sui dati.

Per ogni iterazione, i valori di peso vengono modificati.

La formazione è completa quando le iterazioni non riescono a ridurre il costo .

Allenami per trovare la linea più adatta:


Discesa graduale

Gradient Descent è un popolare algoritmo per la risoluzione di problemi di intelligenza artificiale.

Un semplice modello di regressione lineare può essere utilizzato per dimostrare una discesa del gradiente.

L'obiettivo di una regressione lineare è di adattare un grafo lineare a un insieme di (x,y) punti. Questo può essere risolto con una formula matematica. Ma anche un algoritmo di apprendimento automatico può risolvere questo problema.

Questo è ciò che fa l'esempio sopra.

Inizia con un grafico a dispersione e un modello lineare (y = wx + b).

Quindi addestra il modello per trovare una linea che si adatta alla trama. Questo viene fatto alterando il peso (pendenza) e la deviazione (intercetta) della linea.

Di seguito è riportato il codice per un oggetto Trainer in grado di risolvere questo problema (e molti altri problemi).


Un oggetto trainer

Crea un oggetto Trainer che può accettare un numero qualsiasi di valori (x,y) in due array (xArr,yArr).

Imposta sia il peso che la deviazione su zero.

È necessario impostare una costante di apprendimento (learnc) e definire una variabile di costo:

Esempio

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;

Funzione di costo

Un modo standard per risolvere un problema di regressione è con una "funzione di costo" che misura quanto è buona la soluzione.

La funzione utilizza il peso e la deviazione dal modello (y = wx + b) e restituisce un errore, basato su quanto bene la linea si adatta a un grafico.

Il modo per calcolare questo errore è di scorrere tutti i punti (x,y) nel grafico e sommare le distanze quadrate tra il valore y di ciascun punto e la linea.

Il modo più convenzionale è quadrare le distanze (per garantire valori positivi) e rendere differenziabile la funzione di errore.

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

Un altro nome per la funzione di costo è Funzione di errore .

La formula utilizzata nella funzione è in realtà questa:

Formula
  • E è l'errore (costo)
  • N è il numero totale di osservazioni (punti)
  • y è il valore (etichetta) di ciascuna osservazione
  • x è il valore (caratteristica) di ciascuna osservazione
  • m è la pendenza (peso)
  • b è intercettazione (bias)
  • mx + b è la previsione
  • 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>