A Single Layered Neural Network (SLNN), also called a perceptron, is the simplest form of a neural network where all inputs connect directly to the output through a single set of weights. It is used for tasks like basic pattern recognition, binary classification and simple prediction problems.
A Single Layered Neural Network has:
- One input layer
- One output layer
- No hidden layers
- Straightforward weight updates
- Easy implementation and fast training
It works well when the data is linearly separable.
Architecture of a Single Layered Neural Network
- Let input vector be x = (x1, x2, …, xn)
- Let weights be w = (w1, w2, …, wn)
- Network computes:y = f( w.x+ b) where f is the activation function.
Common Activation Functions
- Step Function: for Perceptron
- Sigmoid: for probability outputs
- Linear Activation: for regression tasks
Step-by-Step Implementation of Single Layered Neural Network in R
Below is the complete step-by-step implementation from scratch
Step 1: Create Dataset
# Simple OR dataset
x1 <- c(0, 0, 1, 1)
x2 <- c(0, 1, 0, 1)
y <- c(0, 1, 1, 1) # OR output
Step 2: Initialize Weights and Bias
print("Hello World!")set.seed(10)
w1 <- runif(1)
w2 <- runif(1)
b <- runif(1)
lr <- 0.1 # learning rate
Step 3: Activation Function (Step Function)
activation <- function(net) {
if (net >= 0) return(1)
else return(0)
}
Step 4: Train the Network (Perceptron Learning Rule)
for (epoch in 1:20) {
for (i in 1:length(y)) {
# Forward pass
net <- w1 * x1[i] + w2 * x2[i] + b
y_pred <- activation(net)
# Error
error <- y[i] - y_pred
# Weight update
w1 <- w1 + lr * error * x1[i]
w2 <- w2 + lr * error * x2[i]
b <- b + lr * error
}
}
Step 5: Make Predictions
for (i in 1:4) {
net <- w1 * x1[i] + w2 * x2[i] + b
print(activation(net))
}
Output:
[1] 0
[1] 1
[1] 1
[1] 1
Limitations of Single Layered Neural Networks
- Cannot solve non-linear problems like XOR
- No hidden layers, limited learning ability
- Works only for linearly separable data
- Simple decision boundaries