Open In App

Single Layer Perceptron in TensorFlow

Last Updated : 08 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Single Layer Perceptron is inspired by biological neurons and their ability to process information. To understand the SLP we first need to break down the workings of a single artificial neuron which is the fundamental building block of neural networks. An artificial neuron is a simplified computational model that mimics the behavior of a biological neuron. It takes inputs, processes them and produces an output. Here's how it works step by step:

  • Receive signal from outside.
  • Process the signal and decide whether we need to send information or not.
  • Communicate the signal to the target cell, which can be another neuron or gland.
Structure of a biological neuron
Structure of a biological neuron

In the same way neural networks also work.

Neural Network in Machine Learning
Neural Network in Machine Learning

Single Layer Perceptron

It is one of the oldest and first introduced neural networks. It was proposed by Frank Rosenblatt in 1958. Perceptron is also known as an artificial neural network. Perceptron is mainly used to compute the logical gate like AND, OR and NOR which has binary input and binary output.

The main functionality of the perceptron is:-

  • Takes input from the input layer
  • Weight them up and sum it up.
  • Pass the sum to the nonlinear function to produce the output.
Single-layer neural network
Single-layer neural network

Here activation functions can be anything like sigmoid, tanh, relu based on the requirement we will be choosing the most appropriate nonlinear activation function to produce the better result. Now let us implement a single-layer perceptron.

Implementation of Single-layer Perceptron

Let’s build a simple single-layer perceptron to recognize handwritten digits from the MNIST dataset using TensorFlow. This model will help you understand how neural networks work at the most basic level.

Step1: Import necessary libraries

  • Numpy – Numpy arrays are very fast and can perform large computations in a very short time.
  • Matplotlib – This library is used to draw visualizations.
  • TensorFlow – This is an open-source library that is used for Machine Learning and Artificial intelligence and provides a range of functions to achieve complex functionalities with single lines of code.
Python
import numpy as np
import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt

Step 2: Load the Dataset

Now we load the dataset using "Keras" from the imported version of tensor flow.

Python
(x_train, y_train),\
    (x_test, y_test) = keras.datasets.mnist.load_data()

Now display the shape and image of the single image in the dataset. The image size contains a 28*28 matrix and length of the training set is 60,000 and the testing set is 10,000. Each image is 28x28 pixels.

  • The pixel values range from 0 (black) to 255 (white).
  • matshow() displays the image in a grid format.
Python
len(x_train)
len(x_test)
x_train[0].shape
plt.matshow(x_train[0])

Output:

Sample image from the training dataset
Sample image from the training dataset

Step 3: Normalize the Dataset

Now normalize the dataset in order to compute the calculations in a fast and accurate manner. Normalization helps the model train faster and improves accuracy. Neural networks expect input in a 1D format so we flatten the 2D 28x28 images into 784-length vectors.

Python
x_train = x_train/255
x_test = x_test/255

# Flatting the dataset in order
# to compute for model building
x_train_flatten = x_train.reshape(len(x_train), 28*28)
x_test_flatten = x_test.reshape(len(x_test), 28*28)

Step 4: Building a neural network

Now we will build neural network with single-layer perception. Here we can observe as the model is a single-layer perceptron that only contains one input layer and one output layer there is no presence of the hidden layers.

  • 10 output neurons (one for each digit from 0 to 9).
  • input_shape=(784) matches the flattened image shape.
  • We use sigmoid activation which outputs probabilities between 0 and 1 for each digit class.


Python
model = keras.Sequential([
    keras.layers.Dense(10, input_shape=(784,),
                       activation='sigmoid')
])
model.compile(
    optimizer='adam',
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy'])

model.fit(x_train_flatten, y_train, epochs=5)

Output:

Training progress per epoch
Training progress per epoch

Step 5: Model Evaluation

After training we test the model's performance on unseen data. This gives the loss and accuracy on the test dataset.

Python
model.evaluate(x_test_flatten, y_test)

Output:

Models performance on the testing data
Models performance on the testing data

Even with such a simple model we achieved over 92% accuracy. That’s quite impressive for a neural network with just one layer. However for even better results we could add hidden layers or use more complex architectures like CNNs (Convolutional Neural Networks).


Next Article

Similar Reads