Skip to content

Repository files navigation

Perceptron Demo

This project contains a simple perceptron implementation and two example scripts that train it on basic logic gates.

Files

  • perceptron.py defines the Perceptron class, including parameter initialization, training, and cost tracking.
  • and_gate.py trains the perceptron on the AND gate.
  • or_gate.py trains the perceptron on the OR gate.
  • visualising_utils.py provides helpers for plotting the input data, decision boundary, and cost curve.
  • pyproject.toml lists the project metadata and Python dependencies.

Requirements

  • Python 3.14 or newer
  • numpy
  • matplotlib

Install the dependencies with:

uv sync

How It Works

The perceptron expects input features in X and labels in Y.

  • Inputs are arranged as a 2D NumPy array with shape (samples, features).
  • Labels use the -1 and 1 convention.
  • Training uses a simple sign activation and updates weights over a fixed number of epochs.

The example scripts use the following training set:

X = np.array([[0, 0],
              [0, 1],
              [1, 0],
              [1, 1]])

For the AND gate:

Y = np.array([[-1],
              [-1],
              [-1],
              [1]])

For the OR gate:

Y = np.array([[-1],
              [1],
              [1],
              [1]])

Running The Examples

Run either example from the project directory:

uv run python and_gate.py
uv run python or_gate.py

Each script prints the initial and trained parameters, then displays:

  • the input scatter plot
  • the learned decision boundary
  • the cost history

Notes

  • The current implementation is intentionally minimal and focused on binary classification.
  • The decision boundary helper assumes two input features.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages