Single Layer Perceptron in TensorFlow

Last Updated : 09 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In this article, we will be understanding the single-layer perceptron and its implementation in Python using the TensorFlow library. Neural Networks work in the same way that our biological neuron works.

Structure of a biological neuron

Structure of a biological neuron

Biological neuron has three basic functionality 

  • Receive signal from outside.
  • Process the signal and enhance whether we need to send information or not.
  • Communicate the signal to the target cell which can be another neuron or gland.

In the same way, neural networks also work.

Neural Network in Machine Learning

Neural Network in Machine Learning

What is 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 us now implement a single-layer perceptron using the “MNIST” dataset using the TensorFlow library.

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.

Python3




import numpy as np
import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
%matplotlib inline


Step 2: Now load the dataset using “Keras” from the imported version of tensor flow.

Python3




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


Step 3: 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.

Python3




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 4: Now normalize the dataset in order to compute the calculations in a fast and accurate manner.

Python3




# Normalizing the dataset
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 5: Building a 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.  

Python3




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 6: Output the accuracy of the model on the testing data.

Python3




model.evaluate(x_test_flatten, y_test)


Output:

Models performance on the testing data

Models performance on the testing data



Previous Article
Next Article

Similar Reads

Hidden Layer Perceptron in TensorFlow
In this article, we will learn about hidden layer perceptron. A hidden layer perceptron is nothing but a hi-fi terminology for a neural network with one or more hidden layers. The purpose which is being served by these hidden layers is that they help to learn complex and non-linear functions for a task. The above image is the simplest representatio
5 min read
Multi-Layer Perceptron Learning in Tensorflow
In this article, we will understand the concept of a multi-layer perceptron and its implementation in Python using the TensorFlow library. Multi-layer Perceptron Multi-layer perception is also known as MLP. It is fully connected dense layers, which transform any input dimension to the desired dimension. A multi-layer perception is a neural network
5 min read
Classification Using Sklearn Multi-layer Perceptron
A key machine learning method that belongs to the class of artificial neural networks is classification using Multi-Layer Perceptrons (MLP). It is a flexible and effective method for tackling a variety of classification problems, including text classification and picture recognition. Traditional linear classifiers might not be up to the challenge,
7 min read
Multi-layer Perceptron a Supervised Neural Network Model using Sklearn
An artificial neural network (ANN), often known as a neural network or simply a neural net, is a machine learning model that takes its cues from the structure and operation of the human brain. It is a key element in machine learning's branch known as deep learning. Interconnected nodes, also referred to as artificial neurons or perceptrons, are arr
11 min read
Fully Connected Layer vs Convolutional Layer
Confusion between Fully Connected Layers (FC) and Convolutional Layers is common due to terminology overlap. In CNNs, convolutional layers are used for feature extraction followed by FC layers for classification that makes it difficult for beginners to distinguish there roles. This article compares Fully Connected Layers (FC) and Convolutional Laye
4 min read
Perceptron Algorithm for Logic Gate with 3-bit Binary Input
In the field of Machine Learning, the Perceptron is a Supervised Learning Algorithm for binary classifiers. The Perceptron Model implements the following function: [Tex]\[\begin{array}{c}\hat{y}=\Theta\left(w_{1} x_{1}+w_{2} x_{2}+\ldots+w_{n} x_{n}+b\right) \\ =\Theta(\mathbf{w} \cdot \mathbf{x}+b) \\ \text { where } \Theta(v)=\left\{\begin{array}
4 min read
Difference between Multilayer Perceptron and Linear Regression
Multi-layer perceptrons (MLP) is an artificial neural network that has 3 or more layers of perceptrons. These layers are- a single input layer, 1 or more hidden layers, and a single output layer of perceptrons. The data flows in a single direction, that is forward, from the input layers-> hidden layer(s) -> output layer. Backpropagation is a
2 min read
Perceptron Algorithm for Classification using Sklearn
Assigning a label or category to an input based on its features is the fundamental task of classification in machine learning. One of the earliest and most straightforward machine learning techniques for binary classification is the perceptron. It serves as the framework for more sophisticated neural networks. This post will examine how to use Scik
11 min read
Perceptron class in Sklearn
Machine learning is a prominent technology in this modern world and as years go by it is growing immensely. There are several components involved in Machine Learning that make it evolve and solve various problems and one such crucial component that exists is the Perceptron. In this article, we will be learning about what a perceptron is, the histor
11 min read
What is Perceptron | The Simplest Artificial neural network
A single-layer feedforward neural network was introduced in the late 1950s by Frank Rosenblatt. It was the starting phase of Deep Learning and Artificial neural networks. During that time for prediction, Statistical machine learning, or Traditional code Programming is used. Perceptron is one of the first and most straightforward models of artificia
11 min read
What is the difference between Perceptron and ADALINE?
Answer: Perceptron is a binary classifier using a step function, while ADALINE is a continuous-valued linear classifier employing a linear activation function.Here's a detailed comparison between Perceptron and ADALINE in a table: FeaturePerceptronADALINE (Adaptive Linear Neuron)Activation FunctionStep functionLinear activation functionOutputBinary
2 min read
Perceptron Convergence Theorem in Neural Networks
The Perceptron Convergence Theorem is a fundamental concept in machine learning, showing how a simple algorithm, the perceptron, can learn to classify items accurately. It's like a basic building block for understanding how computers make decisions, much like our brains handle simple choices. In this article, we will delve into the details of the p
13 min read
Main Loopholes in TensorFlow - Tensorflow Security
TensorFlow is an open-source machine-learning framework widely used for building, training, and deploying machine-learning models. Despite its popularity and versatility, TensorFlow is not immune to security vulnerabilities and loopholes. Some of the common security loopholes in TensorFlow are related to data privacy, session hijacking, and lack of
6 min read
Why TensorFlow is So Popular - Tensorflow Features
In this article, we will see Why TensorFlow Is So Popular, and then explore Tensorflow Features. TensorFlow is an open-source software library. It was originally developed by researchers and engineers working on the Google Brain Team within Google’s Machine Intelligence research organization for the purposes of conducting machine learning and deep
3 min read
Tensorflow 1.xvs. Tensorflow 2.x: What's the Difference?
TensorFlow is an end-to-end open-source machine learning platform that contains comprehensive tools, libraries and community resources. It is meant for developers, data scientists and researchers to build and deploy applications powered by machine learning. TensorFlow was essentially built to scale, developed by Google Brain team, TensorFlow accele
6 min read
How to migrate from TensorFlow 1.x to TensorFlow 2.x
The introduction of TensorFlow 2. x marks a significant advance in the strong open-source machine learning toolkit TensorFlow. TensorFlow 2.0 introduces significant API changes, making manual code upgrades tedious and error prone. TensorFlow 2. x places an emphasis on user-friendliness and optimizes the development process, whereas TensorFlow 1. x
7 min read
Types of padding in convolution layer
Let's discuss padding and its types in convolution layers. In convolution layer we have kernels and to make the final filter more informative we use padding in image matrix or any kind of input array. We have three types of padding that are as follows. Padding Full : Let's assume a kernel as a sliding window. We have to come with the solution of pa
5 min read
CNN | Introduction to Pooling Layer
The pooling operation involves sliding a two-dimensional filter over each channel of feature map and summarising the features lying within the region covered by the filter. For a feature map having dimensions nh x nw x nc, the dimensions of output obtained after a pooling layer is (nh - f + 1) / s x (nw - f + 1)/s x nc where, -> nh - height of f
6 min read
Understanding Multi-Layer Feed Forward Networks
Let's understand how errors are calculated and weights are updated in backpropagation networks(BPNs). Consider the following network in the below figure. The network in the above figure is a simple multi-layer feed-forward network or backpropagation network. It contains three layers, the input layer with two neurons x1 and x2, the hidden layer with
7 min read
What is Transposed Convolutional Layer?
A transposed convolutional layer is an upsampling layer that generates the output feature map greater than the input feature map. It is similar to a deconvolutional layer. A deconvolutional layer reverses the layer to a standard convolutional layer. If the output of the standard convolution layer is deconvolved with the deconvolutional layer then t
6 min read
FNet: A Transformer Without Attention Layer
This article delves into FNet, a transformative architecture that reimagines the traditional transformer by discarding attention mechanisms entirely. Let's begin the journey to explore FNet, but first, let's look at the limitations of transformers. What is FNet?In contrast to conventional transformer architectures, like the popular Transformer mode
7 min read
What is fractionally-strided convolution layer?
Answer: Fractionally-strided convolution (deconvolution) layer upsamples by inserting zeros between inputs and applying regular convolution.To understand fractionally-strided convolution, it's essential to first grasp the concept of regular (or "valid") convolution. In a standard convolution operation, a filter (also called a kernel) is applied to
2 min read
How to Decide the Window Size on a Pooling Layer?
Answer: The window size on a pooling layer is often determined based on the desired balance between feature preservation and spatial reduction in the input data.Determining the window size on a pooling layer involves considering several factors related to the specific task and characteristics of the input data. Pooling layers are commonly used in c
3 min read
What is a Neural Network Flatten Layer?
Answer: A neural network flatten layer is used to convert the multi-dimensional output from the previous layer into a one-dimensional array, typically before feeding it into a fully connected layer for further processing.A neural network flatten layer is a type of layer commonly used in deep learning architectures to transform multi-dimensional inp
2 min read
What is a 1D Convolutional Layer in Deep Learning?
Answer: A 1D Convolutional Layer in Deep Learning applies a convolution operation over one-dimensional sequence data, commonly used for analyzing temporal signals or text.A 1D Convolutional Layer (Conv1D) in deep learning is specifically designed for processing one-dimensional (1D) sequence data. This type of layer is particularly useful for tasks
2 min read
What is the Difference between a "Cell" and a "Layer" within Neural Networks?
Answer: In neural networks, a "cell" refers to the basic processing unit within a recurrent neural network (RNN), such as a long short-term memory (LSTM) cell, while a "layer" is a structural component comprising interconnected neurons in the network architecture, including convolutional layers, dense layers, etc.In neural networks, both "cell" and
1 min read
Keras Input Layer
When deep learning models are built, the foundation step of the model preparation starts with the input layer. Keras Input Layer is essential for defining the shape and size of the input data the model with receive. In this article, we are going to learn more on Keras Input Layer, its purpose, usage and it's role in model architecture. Table of Con
4 min read
What is Fully Connected Layer in Deep Learning?
Fully Connected (FC) layers, also known as dense layers, are a crucial component of neural networks, especially in the realms of deep learning. These layers are termed "fully connected" because each neuron in one layer is connected to every neuron in the preceding layer, creating a highly interconnected network. This article explores the structure,
7 min read
Multi-Hidden layer neural network with the mlp method in the caret package
Neural networks are a foundational element of deep learning, capable of modeling complex patterns in data. A neural network with multiple hidden layers, also known as a deep neural network, can capture intricate relationships in the data more effectively than a single-layer network. This article will cover the theory behind multi-hidden layer neura
4 min read
Multi-Layer Perceptrons (MLP) in R
In the world of machine learning, Multi-Layer Perceptrons (MLP) are a popular type of artificial neural network used for various tasks such as classification and regression. MLPs are versatile and can model complex patterns in data. Here, we’ll break down what MLPs are and how you can implement them using R for data analysis. What is a Multi-Layer
4 min read
three90RightbarBannerImg