What Is a Single Layer Perceptron? Definition, Formula, and Code

|
6 min read
|
49 views
Single Layer Perceptron

A single layer perceptron (SLP) is the most basic type of artificial neural network. It has one layer of adjustable weights connecting its inputs to its output and separates data into two classes using a linear boundary.

What Is a Single Layer Perceptron?

A single-layer perceptron (SLP) is a feed-forward neural network that lacks any hidden layer. This means the model multiplies each input by its respective weight, adds a bias value, and passes the result through the activation function to produce a binary output.

Frank Rosenblatt invented SLP in 1957 at the Cornell Aeronautical Laboratory under a grant by the U.S. Office of Naval Research. Rosenblatt presented his model in the 1958 paper entitled “The Perceptron: A Probabilistic Model for Information Storage and Organization in the Brain,” published in Psychological Review. He also built a physical version of it, the Mark I Perceptron, using custom hardware connected to a 20 ✕ 20 array of photocells.

We call SLP a single-layer perceptron, because it has a single layer of trainable weights. Note that the input layer does not count as a layer by definition, since it performs no computations. Novice learners often find this confusing when reading perceptron diagrams.

How a Single Layer Perceptron Works

A single layer perceptron processes data in four steps: it receives inputs, multiplies each by a weight, sums the weighted inputs with a bias, and applies an activation function to produce the output.

ComponentFunctionExample
Input (x)A numeric feature fed into the modelPixel intensity, sensor reading, or a binary flag
Weight (w)A value that scales the importance of each inputA higher weight increases an input’s influence on the output
Bias (b)A constant added to the weighted sumShifts the decision boundary away from the origin
Weighted sum (z)The total of all weighted inputs plus the biasz = w₁x₁ + w₂x₂ + b
Activation functionConverts z into the final outputStep function, sigmoid function, or ReLU function

The Single Layer Perceptron Formula

The output of a single layer perceptron is calculated with two equations:

z = w₁x₁ + w₂x₂ + ⋯ + wₙxₙ + b

y = f(z)

Here, x₁ to xₙ are the inputs, w₁ to wₙ are their respective weights, b is the bias, and f is the activation function. The function f is usually the Heaviside step function, which is based on the name of mathematician Oliver Heaviside, and outputs 1 if z is greater than 0, otherwise 0.

Worked Example: Classifying an AND Gate

The one-layer perceptron with weights w₁ = 1, w₂ = 1, and bias b = -1.5 perfectly identifies the logic function AND with step activation function. The table shows the calculations for all four possible input combinations.

x₁x₂z = w₁x₁ + w₂x₂ + bOutput (z > 0 → 1)Correct AND value
00-1.500
01-0.500
10-0.500
110.511

These weight values are not guessed. The perceptron learning rule finds them, as described in the next section.

The Perceptron Learning Rule

The perceptron learning rule updates each weight after every training example using the formula:

Δwⱼ = η(target − output) × xⱼ

η represents the learning rate, which is a constant with a value between 0.0 and 1.0. target refers to the correct value of the training data. output is the value generated by the perceptron. xⱼ represents the input that is connected to wⱼ.

Numeric example: With η = 0.1, an input of x₁ = 1, x₂ = 1, a target of 1, and a current output of 0, the weight update for w₁ is:

Δw₁ = 0.1 × (1 − 0) × 1 = 0.1

This value is then added to the existing weight. The perceptron repeats this process for each training instance, through many cycles called epochs, until it classifies the data correctly or reaches a set number of epochs.

According to the perceptron convergence theorem, this process of changing the weights guarantees convergence to a correct set of weights provided that the two sets in the training dataset are linearly separable. This means that if it is not possible to separate the two sets with a straight line, the process of finding the right weights will not converge.

The XOR Problem: Why a Single Layer Perceptron Cannot Solve It

One layer perceptron is unable to solve the XOR problem, since the XOR problem has four sets of inputs and outputs that are not linearly separable — they cannot be separated by one straight line into outputs of 1 and 0.

x₁x₂XOR output
000
011
101
110

When plotting the above points, it becomes clear that the points with output 1 and the points with output 0 are located at diagonally opposite corners of the square. There is no straight line that can separate these points into two categories.

Marvin Minsky and Seymour Papert mathematically proved this limitation in their book Perceptrons (1969). The fact that a single layer perceptron is limited to linear separable functions played an important role in decreasing the funding for neural networks research during the 1970s. The multi-layer perceptron (MLP) overcame this limitation.

Single Layer Perceptron vs. Multi-Layer Perceptron

A single layer perceptron and a multi-layer perceptron differ in the number of trainable layers, the type of problems each can solve, and the training method each uses.

AttributeSingle Layer PerceptronMulti-Layer Perceptron
Number of trainable layers12 or more (including one or more hidden layers)
Solvable problemsLinearly separable data onlyLinearly and non-linearly separable data
Training algorithmPerceptron learning ruleBackpropagation with gradient descent
Activation function usedStep function (non-differentiable)Differentiable functions, such as sigmoid, tanh, or ReLU
Example problem it can solveAND, OR logic gatesXOR logic gate, image classification

In a single layer perceptron, the step function is not differentiable at z=0, and its derivative equals 0 elsewhere. Backpropagation needs an activation function that can be differentiated to compute gradients; hence, a single layer perceptron cannot be trained using backpropagation.

Building a Single Layer Perceptron in Python

The following code builds a single layer perceptron from its underlying formula, using NumPy, to train it on the AND logic gate.

python
import numpy as np

# Training data for the AND logic gate
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([0, 0, 0, 1])

# Initialize weights, bias, and learning rate
weights = np.zeros(2)
bias = 0.0
learning_rate = 0.1
epochs = 10

def step_function(z):
    return 1 if z > 0 else 0

# Train using the perceptron learning rule
for epoch in range(epochs):
    for xi, target in zip(X, y):
        z = np.dot(xi, weights) + bias
        output = step_function(z)
        error = target - output
        weights += learning_rate * error * xi
        bias += learning_rate * error

# Test the trained perceptron
for xi in X:
    z = np.dot(xi, weights) + bias
    print(xi, "->", step_function(z))

Executing the code will give us the output [0 0] -> 0, [0 1] -> 0, [1 0] -> 0, and [1 1] -> 1, which proves that the perceptron has learned the AND function. 

You can also build a one-layer perceptron using a framework like the Keras API from TensorFlow, with a Dense layer and an activation function like sigmoid or step. The NumPy version above is great for learning the mechanics behind it, but the Keras version is quick and easy to implement for production. 

Advantages and Limitations of a Single Layer Perceptron

Advantages:

  • Requires less computational power, because it has only one layer of weights to train.
  • Trains quickly, typically converging in fewer epochs than a multi-layer network.
  • Is simple to implement, needing only a weighted sum and one activation function.
  • Provides guaranteed convergence on linearly separable data, per the perceptron convergence theorem.

Limitations:

  • Unable to solve problems that cannot be separated linearly (e.g., XOR).
  • Unable to learn using the backpropagation algorithm, because the step activation function is non-differentiable.
  • Unable to model complex relationships, because it lacks multiple layers.
  • It generates only binary output, because it makes use of the step activation function.

Frequently Asked Questions

Q1. Can a single layer perceptron solve XOR? 

Ans. No. The reason for this is that XOR is not linearly separable, while single layer perceptrons can only classify linearly separable classes.

Q2. Why is it called a “single layer” if there is also an input layer? 

Ans. A single layer perceptron most commonly uses the Heaviside step function, though a sigmoid function is used when a probability-style output is needed instead of a strict binary output.

Q3. What is the difference between a perceptron and logistic regression? 

Ans. The perceptron and logistic regression algorithm share the formula of the weighted sum but the former uses step function that outputs binary class labels while the latter uses sigmoid function which outputs probabilities.

Q4. Is a single layer perceptron still used today?

Ans. The application of the single layer perceptron nowadays is only for educational purposes and for doing simple linearly separable binary classifications; more complicated applications include multilayer perceptrons.

Q5. What activation function does a single layer perceptron use? 

Ans. We call it a “single layer perceptron” because the input layer performs no computations and therefore doesn’t count as a layer; only the weight layer counts as a layer. 

Gyansetu offers top professional training certification courses designed to enhance your skills and advance your career, providing industry-relevant knowledge and practical expertise.