기계 학습
학습은 반복
ML 모델은 데이터를 여러 번 반복 하여 학습됩니다 .
각 반복에 대해 가중치 값 이 조정됩니다.
반복이 비용 줄이기에 실패하면 훈련이 완료된 것 입니다.
가장 적합한 라인을 찾도록 교육:
경사하강법
경사하강법 은 AI 문제를 해결하기 위해 널리 사용되는 알고리즘입니다.
간단한 선형 회귀 모델 을 사용하여 경사하강법을 설명할 수 있습니다.
선형 회귀의 목표는 선형 그래프를 (x,y) 점 집합에 맞추는 것입니다. 이것은 수학 공식으로 풀 수 있습니다. 그러나 머신 러닝 알고리즘 도 이 문제를 해결할 수 있습니다.
이것이 위의 예가 하는 일입니다.
산점도와 선형 모델(y = wx + b)로 시작합니다.
그런 다음 플롯에 맞는 선을 찾기 위해 모델을 훈련합니다. 이것은 선의 가중치(기울기)와 편향(절편)을 변경하여 수행됩니다.
아래는 이 문제(및 기타 많은 문제)를 해결할 수 있는 Trainer Object 의 코드입니다.
트레이너 개체
두 개의 배열(xArr,yArr)에서 원하는 수의 (x,y) 값을 사용할 수 있는 Trainer 개체를 만듭니다.
가중치와 편향을 모두 0으로 설정합니다.
학습 상수(learnnc)를 설정하고 비용 변수를 정의해야 합니다.
예시
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;
}
비용 함수 의 또 다른 이름 은 오류 함수 입니다.
함수에 사용된 공식은 실제로 다음과 같습니다.
- E 는 오류(비용)입니다.
- N 은 총 관측치(점)입니다.
- y 는 각 관측값의 값(레이블)입니다.
- x 는 각 관측치의 값(특성)입니다.
- m 은 기울기(무게)
- 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>