Single Layer Perceptron in TensorFlow

Last updated on Oct 25 2021
Ashutosh Wakiroo

Table of Contents

Single Layer Perceptron in TensorFlow

The perceptron is a single processing unit of any neural network. Frank Rosenblatt first proposed in 1958 is a simple neuron which is used to classify its input into one or two categories. Perceptron is a linear classifier, and is used in supervised learning. It helps to organize the given input data.
A perceptron is a neural network unit that does a precise computation to detect features in the input data. Perceptron is mainly used to classify the data into two parts. Therefore, it is also known as Linear Binary Classifier.

tensorFlow 16
tensorFlow

Perceptron uses the step function that returns +1 if the weighted sum of its input 0 and -1.
The activation function is used to map the input between the required value like (0, 1) or (-1, 1).
A regular neural network looks like this:

tensorFlow 17
tensorFlow

The perceptron consists of 4 parts.
Input value or One input layer: The input layer of the perceptron is made of artificial input neurons and takes the initial data into the system for further processing.
Weights and Bias:
Weight: It represents the dimension or strength of the connection between units. If the weight to node 1 to node 2 has a higher quantity, then neuron 1 has a more considerable influence on the neuron.
Bias: It is the same as the intercept added in a linear equation. It is an additional parameter which task is to modify the output along with the weighted sum of the input to the other neuron.
Net sum: It calculates the total sum.
Activation Function: A neuron can be activated or not, is determined by an activation function. The activation function calculates a weighted sum and further adding bias with it to give the result.

tensorFlow 18
tensorFlow

A standard neural network looks like the below diagram.

tensorFlow 19
tensorFlow

How does it work?
The perceptron works on these simple steps which are given below:
a. In the first step, all the inputs x is multiplied with their weights w.

tensorFlow 20
tensorFlow

b. In this step, add all the increased values and call them the Weighted sum.

tensorFlow 21
tensorFlow

c. In our last step, apply the weighted sum to a correct Activation Function.
For Example:
A Unit Step Activation Function

tensorFlow 22
tensorFlow

There are two types of architecture. These types focus on the functionality of artificial neural networks as follows-
• Single Layer Perceptron
• Multi-Layer Perceptron

Single Layer Perceptron

The single-layer perceptron was the first neural network model, proposed in 1958 by Frank Rosenbluth. It is one of the earliest models for learning. Our goal is to find a linear decision function measured by the weight vector w and the bias parameter b.
To understand the perceptron layer, it is necessary to comprehend artificial neural networks (ANNs).
The artificial neural network (ANN) is an information processing system, whose mechanism is inspired by the functionality of biological neural circuits. An artificial neural network consists of several processing units that are interconnected.
This is the first proposal when the neural model is built. The content of the neuron’s local memory contains a vector of weight.
The single vector perceptron is calculated by calculating the sum of the input vector multiplied by the corresponding element of the vector, with each increasing the amount of the corresponding component of the vector by weight. The value that is displayed in the output is the input of an activation function.
Let us focus on the implementation of a single-layer perceptron for an image classification problem using TensorFlow. The best example of drawing a single-layer perceptron is through the representation of “logistic regression.”

tensorFlow 23
tensorFlow

Now, We have to do the following necessary steps of training logistic regression-
• The weights are initialized with the random values at the origination of each training.
• For each element of the training set, the error is calculated with the difference between the desired output and the actual output. The calculated error is used to adjust the weight.
• The process is repeated until the fault made on the entire training set is less than the specified limit until the maximum number of iterations has been reached.

Complete code of Single layer perceptron
1. # Import the MINST dataset
2. from tensorflow.examples.tutorials.mnist import input_data
3. mnist = input_data.read_data_ ("/tmp/data/", one_hot=True)
4.
5. import tensorflow as tf
6. import matplotlib.pyplot as plt
7. # Parameters
8. learning_rate = 0.01
9. training_epochs = 25
10. batch_size = 100
11. display_step = 1
12.
13. # tf Graph Input
14. x = tf.placeholder("float", [none, 784]) # MNIST data image of shape 28*28 = 784
15. y = tf.placeholder("float", [none, 10]) # 0-9 digits recognition => 10 classes
16. # Create model
17. # Set model weights
18. W = tf.Variable(tf.zeros([784, 10]))
19. b = tf.Variable(tf.zeros([10]))
20. # Constructing the model
21. activation=tf.nn.softmaxx(tf.matmul (x, W)+b) # Softmax
22. of function
23. # Minimizing error using cross entropy
24. cross_entropy = y*tf.log(activation)
25. cost = tf.reduce_mean\ (-tf.reduce_sum\ (cross_entropy, reduction_indice = 1))
26. optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
27. #Plot settings
28. avg_set = []
29. epoch_set = []
30. # Initializing the variables where init = tf.initialize_all_variables()
31. # Launching the graph
32. with tf.Session() as sess:
33. sess.run(init)
34.
35. # Training of the cycle in the dataset
36. for epoch in range(training_epochs):
37. avg_cost = 0.
38. total_batch = int(mnist.train.num_example/batch_size)
39.
40. # Creating loops at all the batches in the code
41. for i in range(total_batch):
42. batch_xs, batch_ys = mnist.train.next_batch(batch_size)
43. # Fitting the training by the batch data sess.run(optimizr, feed_dict = {
44. x: batch_xs, y: batch_ys})
45. # Compute all the average of loss avg_cost += sess.run(cost, \ feed_dict = {
46. x: batch_xs, \ y: batch_ys}) //total batch
47. # Display the logs at each epoch steps
48. if epoch % display_step==0:
49. print("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format (avg_cost))
50. avg_set.append(avg_cost) epoch_set.append(epoch+1)
51. print ("Training phase finished")
52.
53. plt.plot(epoch_set,avg_set, 'o', label = 'Logistics Regression Training')
54. plt.ylabel('cost')
55. plt.xlabel('epoch')
56. plt.legend()
57. plt.show()
58.
59. # Test the model
60. correct_prediction = tf.equal (tf.argmax (activation, 1),tf.argmax(y,1))
61.
62. # Calculating the accuracy of dataset
63. accuracy = tf.reduce_mean(tf.cast (correct_prediction, "float")) print
64. ("Model accuracy:", accuracy.eval({x:mnist.test.images, y: mnist.test.labels}))

The output of the Code:

tensorFlow 24
tensorFlow

The logistic regression is considered as predictive analysis. Logistic regression is mainly used to describe data and use to explain the relationship between the dependent binary variable and one or many nominal or independent variables.

tensorFlow 25
tensorFlow

Note: Weight shows the strength of the particular node.

So, this brings us to the end of blog. This Tecklearn ‘Single Layer Perceptron in Tensor Flow’ blog helps you with commonly asked questions if you are looking out for a job in Artificial Intelligence. If you wish to learn Artificial Intelligence and build a career in AI or Machine Learning domain, then check out our interactive, Artificial Intelligence and Deep Learning with TensorFlow Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/artificial-intelligence-and-deep-learning-with-tensorflow/

Artificial Intelligence and Deep Learning with TensorFlow Training

About the Course

Tecklearn’s Artificial Intelligence and Deep Learning with Tensor Flow course is curated by industry professionals as per the industry requirements & demands and aligned with the latest best practices. You’ll master convolutional neural networks (CNN), TensorFlow, TensorFlow code, transfer learning, graph visualization, recurrent neural networks (RNN), Deep Learning libraries, GPU in Deep Learning, Keras and TFLearn APIs, backpropagation, and hyperparameters via hands-on projects. The trainee will learn AI by mastering natural language processing, deep neural networks, predictive analytics, reinforcement learning, and more programming languages needed to shine in this field.

Why Should you take Artificial Intelligence and Deep Learning with Tensor Flow Training?

• According to Paysa.com, an Artificial Intelligence Engineer earns an average of $171,715, ranging from $124,542 at the 25th percentile to $201,853 at the 75th percentile, with top earners earning more than $257,530.
• Worldwide Spending on Artificial Intelligence Systems Will Be Nearly $98 Billion in 2023, According to New IDC Spending Guide at a GACR of 28.5%.
• IBM, Amazon, Apple, Google, Facebook, Microsoft, Oracle and almost all the leading companies are working on Artificial Intelligence to innovate future technologies.

What you will Learn in this Course?

Introduction to Deep Learning and AI
• What is Deep Learning?
• Advantage of Deep Learning over Machine learning
• Real-Life use cases of Deep Learning
• Review of Machine Learning: Regression, Classification, Clustering, Reinforcement Learning, Underfitting and Overfitting, Optimization
• Pre-requisites for AI & DL
• Python Programming Language
• Installation & IDE
Environment Set Up and Essentials
• Installation
• Python – NumPy
• Python for Data Science and AI
• Python Language Essentials
• Python Libraries – Numpy and Pandas
• Numpy for Mathematical Computing
More Prerequisites for Deep Learning and AI
• Pandas for Data Analysis
• Machine Learning Basic Concepts
• Normalization
• Data Set
• Machine Learning Concepts
• Regression
• Logistic Regression
• SVM – Support Vector Machines
• Decision Trees
• Python Libraries for Data Science and AI
Introduction to Neural Networks
• Creating Module
• Neural Network Equation
• Sigmoid Function
• Multi-layered perception
• Weights, Biases
• Activation Functions
• Gradient Decent or Error function
• Epoch, Forward & backword propagation
• What is TensorFlow?
• TensorFlow code-basics
• Graph Visualization
• Constants, Placeholders, Variables
Multi-layered Neural Networks
• Error Back propagation issues
• Drop outs
Regularization techniques in Deep Learning
Deep Learning Libraries
• Tensorflow
• Keras
• OpenCV
• SkImage
• PIL
Building of Simple Neural Network from Scratch from Simple Equation
• Training the model
Dual Equation Neural Network
• TensorFlow
• Predicting Algorithm
Introduction to Keras API
• Define Keras
• How to compose Models in Keras
• Sequential Composition
• Functional Composition
• Predefined Neural Network Layers
• What is Batch Normalization
• Saving and Loading a model with Keras
• Customizing the Training Process
• Using TensorBoard with Keras
• Use-Case Implementation with Keras
GPU in Deep Learning
• Introduction to GPUs and how they differ from CPUs
• Importance of GPUs in training Deep Learning Networks
• The GPU constituent with simpler core and concurrent hardware
• Keras Model Saving and Reusing
• Deploying Keras with TensorBoard
Keras Cat Vs Dog Modelling
• Activation Functions in Neural Network
Optimization Techniques
• Some Examples for Neural Network
Convolutional Neural Networks (CNN)
• Introduction to CNNs
• CNNs Application
• Architecture of a CNN
• Convolution and Pooling layers in a CNN
• Understanding and Visualizing a CNN
RNN: Recurrent Neural Networks
• Introduction to RNN Model
• Application use cases of RNN
• Modelling sequences
• Training RNNs with Backpropagation
• Long Short-Term memory (LSTM)
• Recursive Neural Tensor Network Theory
• Recurrent Neural Network Model
Application of Deep Learning in image recognition, NLP and more
Real world projects in recommender systems and others
Got a question for us? Please mention it in the comments section and we will get back to you.

0 responses on "Single Layer Perceptron in TensorFlow"

Leave a Message

Your email address will not be published. Required fields are marked *