Machine Learning Model Training


Definition

Training a machine learning model means teaching the model to recognize patterns using historical data so it can make predictions or decisions on new, unseen data.


Concept

Imagine a 2D space full of points. There’s an invisible line.

Your perceptron’s job is to learn to classify which side each point falls on (above/below the line).

See steps And after that learn:


1. Create Perceptron Object

This routine initializes a perceptron by assigning randomized synaptic strengths, defining an adaptation pace, and incorporating a constant bias trigger.

function Perceptron(inputsCount, learningRate = 0.00001) {
  this.lr = learningRate;
  this.bias = 1;
  this.weights = [];

  // Randomize weights between -1 and 1
  for (let i = 0; i <= inputsCount; i++) {
    this.weights[i] = Math.random() * 2 - 1;
  }

  // Activation Logic
  this.activate = function(inputs) {
    let total = 0;
    for (let i = 0; i < inputs.length; i++) {
      total += inputs[i] * this.weights[i];
    }
    return total > 0 ? 1 : 0;
  };

  // Learning Logic
  this.train = function(inputs, expected) {
    inputs.push(this.bias); // Add bias input
    const prediction = this.activate(inputs);
    const error = expected - prediction;
    if (error !== 0) {
      for (let i = 0; i < inputs.length; i++) {
        this.weights[i] += this.lr * error * inputs[i];
      }
    }
  };
}

2. Goal

Train this perceptron to guess which side of a line a point (x, y) lies.

We use a line:

function line(x) {   
     return x * 1.2 + 50;
} 

3. Setup the Training Data

const totalPoints = 500;
const xPoints = [];
const yPoints = [];
const targets = [];

// Generate random coordinates and compute labels
for (let i = 0; i < totalPoints; i++) {
  const x = Math.random() * 400;
  const y = Math.random() * 400;
  xPoints.push(x);
  yPoints.push(y);
  targets.push(y > line(x) ? 1 : 0); // Classify above or below
}

4. Initialize and Train the Perceptron

const myPerceptron = new Perceptron(2); // 2 inputs: x and y

// Train over 10,000 cycles
for (let epoch = 0; epoch < 10000; epoch++) {
  for (let i = 0; i < totalPoints; i++) {
    myPerceptron.train([xPoints[i], yPoints[i]], targets[i]);
  }
}
 

5. Test & Visualize Results

for (let i = 0; i < totalPoints; i++) {
  const x = xPoints[i];
  const y = yPoints[i];
  const output = myPerceptron.activate([x, y, myPerceptron.bias]);
  const color = output === 1 ? "red" : "blue";
  plotter.plotPoint(x, y, color); // Visual function from your plotting library
}
 

Key Concepts Recap

ConceptMeaning
WeightsLearned values for each input
BiasConstant input to avoid zero trap
Learning RateControls how fast model learns
ActivationOutput logic (1 or 0 decision)
TrainingAdjust weights based on mistakes
EpochOne full pass through all data

Prefer Learning by Watching?

Watch these YouTube tutorials to understand CYBERSECURITY Tutorial visually:

What You'll Learn:
  • 📌 All Machine Learning Models Explained in 5 Minutes | Types of ML Models Basics
  • 📌 What is a Perceptron Learning Algorithm - Step By Step Clearly Explained using Python
Previous Next