Skip to content

atta3554/Perceptron-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Simple Perceptron (From Scratch in JavaScript)

A minimal implementation of a single-layer Perceptron written from scratch in pure JavaScript.

This implementation demonstrates how a basic linear binary classifier works using randomly initialized weights and bias, trained via the Perceptron learning rule.


📌 Overview

This project implements a:

  • Single neuron (Perceptron)
  • Binary classification model (2 classes: 0 and 1)
  • Step activation function
  • Supervised learning using labeled training samples

The model initializes weights and bias randomly and iteratively updates them to minimize classification errors.


🧠 How It Works

1️⃣ Initialization

  • Random weights
  • Random bias
  • Defined number of input features

2️⃣ Forward Pass

The output is computed as: output = step( w · x + b )

Where:

  • w = weights
  • x = input features
  • b = bias
  • step() = activation function

3️⃣ Activation Function

Binary step function: if (sum > 0) return 1; else return 0;

This makes the model suitable only for binary classification problems.


🔁 Training Algorithm

The model is trained using the Perceptron Learning Rule: w = w + learning_rate * error * input b = b + learning_rate * error Where: error = target - output

Training continues for a fixed number of iterations or until no weight updates occur.


⚠️ Limitations

  • Works only for linearly separable data
  • Supports binary classification only
  • Not suitable for regression problems
  • Uses non-differentiable step activation

🚀 Usage Example

const p = new Perceptron(2);

p.train(trainingInputs, targets, 0.01, 1000);

const result = p.calculateOutput([x1, x2]);

📚 Educational Purpose

This implementation is designed for learning and understanding:

Linear classifiers

Decision boundaries

Weight updates

Fundamental neural network concepts

About

writing single perceptron(neuron) with pure js from scratch

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors