AI Learning
Introduction to Machine Learning (ML)
Machine Learning is all about recognizing patterns through data. An ML system iteratively loops over data to enhance its accuracy.
During every cycle, it tweaks its parameters—mainly the weight and bias—to minimize prediction errors. The goal? Reduce the cost (a measure of error) with each pass. Training halts when this error stops decreasing.
Gradient Descent — The Learning Engine
Gradient Descent is a foundational optimization algorithm in ML. It drives the model to find the optimal solution by gradually moving in the direction that minimizes error.
In a simple case like linear regression, the aim is to find a straight line (y = wx + b) that best represents the relationship between two variables.
The approach?
- Plot (x, y) data.
- Start with a guess line.
- Adjust w (slope) and b (intercept) with each iteration using gradients (slopes of error).
The Trainer Object
This object simulates training using gradient descent.
Example:
function Trainer(xArray, yArray) {
this.xArr = xArray;
this.yArr = yArray;
this.points = this.xArr.length;
this.learnc = 0.00001; // learning rate
this.weight = 0; // slope (w)
this.bias = 1; // intercept (b)
this.cost;
// Error Metric (Cost Function)
this.costError = function() {
let total = 0;
for (let i = 0; i < this.points; i++) {
let predicted = this.weight * this.xArr[i] + this.bias;
let diff = this.yArr[i] - predicted;
total += diff ** 2;
}
return total / this.points;
};
// Training Function
this.train = function(iterations) {
for (let i = 0; i < iterations; i++) {
this.updateWeights();
} this.cost = this.costError(); };
// Gradient Calculation and Parameter Update
this.updateWeights = function() {
let w_grad = 0;
let b_grad = 0;
for (let i = 0; i < this.points; i++) {
let guess = this.weight * this.xArr[i] + this.bias;
let error = this.yArr[i] - guess;
w_grad += -2 * this.xArr[i] * error;
b_grad += -2 * error;
}
this.weight -= (w_grad / this.points) * this.learnc;
this.bias -= (b_grad / this.points) * this.learnc;
};
}
Understanding the Math
- E (Error): Total cost
- N: Number of samples
- y: Actual output
- x: Input feature
- w (m): Weight (slope)
- b: Bias (intercept)
- Formula:
𝐸𝑟𝑟𝑜𝑟 = (1/N) ∑ (𝑦ᵢ - (𝑤𝑥ᵢ + 𝑏))²
How to Use This
Save the above Trainer code in myailib.js and include it in your HTML:
<script src="myailib.js"></script>
Then, initialize and train:
const xData = [1, 2, 3, 4, 5];
const yData = [2, 4, 6, 8, 10];
const model = new Trainer(xData, yData);
model.train(500);
console.log("Weight:", model.weight);
console.log("Bias:", model.bias);
Console.log("Cost:", model.cost);
Let me know if you'd like a visual chart, graph, or even to convert this into a learning module.
Previous NextPrefer Learning by Watching?
Watch these YouTube tutorials to understand AWS Tutorial visually:
What You'll Learn:
- 📌 How I'd Learn AI (if I could start over)
- 📌 Artificial Intelligence | What is AI | Introduction to Artificial Intelligence | Edureka