Basic Artificial Neural Networks in Python
What are Neural Networks?
A human neural network is a biological representation of the neuron network in the human brain, which deals with transmitting information from the brain to any body part and vice versa through chemical reactions happening in the brain.
What are Artificial Neural Networks?
Artificial Neural networks are the networks inspired by the human neural network to classify and predict problems in the field of Deep Learning. It tends to develop humanly programmed algorithms to find the underlying patterns and relationships in the data set in a way that the human brain does.
Concept of Neural Networks in Deep Learning
Biological networks and much more complex in nature, and the computer scientists are working on achieving that level of accuracy and complexity but are far from achieving it yet. Therefore, the artificial neural network, also known as ANN, is a simple computational model to solve specific problems such as speech tasks. Neural Network in deep learning is a model or algorithm that predicts the solutions by following these steps:
- Take the source data
- Perform calculations on it
- Predict the solution
- Compare the solution with the actual solution
- Adjust the internal state or equation to predict this problem correctly in the future
It consists of neuron layers that manipulate the data and transfer it to the next layer, and the next layer further processes. It manipulates it before sending it to the next layer. In most cases, the manipulation is simply the dot product of the input with the node’s weight. A transformed data reaches the perceptron after going through the last node (which is a bias or summing junction in most cases), which makes it go through the activation function to get the final output. The model or user can compare the final output with the desired output and adjust the input weights of every node accordingly.
Artificial Neural Network Using Python
The users can choose to code the neural network from scratch using NumPy and scipy libraries. However, powerful library packages such as Keras and TensorFlow are available in python, making it easier for the users to design a primary artificial neural network to solve their problems. Below is an example of a simple artificial neural network built using Keras:
from keras.layers import Activation, Dense from keras.models import Sequential import numpy as np inputs = np.array ([ [1,1], [1,0], [0,1], [0,0] ]) outputs = np.array([ [0], [1], [1], [0] ]) ann_model = Sequential() ann_model.add(Dense (2, input_shape =(2,) ) ) ann_model.add( Activation (‘sigmoid’) ) ann_model.add( Dense(1) ) ann_model.add( Activation (‘sigmoid’) ) ann_model.compile( loss=’mean_squared_error’, optimizer=’sgd’, metrics=[‘accuracy’]) ann_model.summary()
Explanation
- Import the required packages.
- Declare and initialize the input and outputs of the data in the form of NumPy arrays.
- Define the network model, which in this case is a sequential model. The sequential model consists of a plain stack of layers with precisely one input tensor or node and one output tensor.
- Set the number of nodes or neurons for each layer.
- Set the activation function, which in this case is sigmoid function. In mathematical terms, sigmoid ( x ) = 1 / ( 1 + exp ( -x ) ).
- Compile the above model and calculate its accuracy. The loss function finds the mean squared error using the predicted and actual results. The optimizer function optimizes the weights for the next iteration based on the loss function and the model prediction. Finally, the metrics evaluate the performance of the model.
- Print the model's summary using the summary method.
Other useful articles:
- OOP in Python
- Python v2 vs Python v3
- Variables, Data Types, and Syntaxes in Python
- Operators, Booleans, and Tuples
- Loops and Statements in Python
- Python Functions and Modules
- Regular Expressions in Python
- Python Interfaces
- JSON Data and Python
- Pip and its Uses in Python
- File Handling in Python
- Searching and Sorting Algorithms in Python
- System Programming (Pipes &Threads etc.)
- Database Programming in Python
- Debugging with Assertion in Python
- Sockets in Python
- InterOp in Python
- Exception Handling in Python
- Environments in Python
- Foundation of Data Science
- Reinforcement Learning
- Python for AI
- Applied Text Mining in Python
- Python Iterations using Libraries
- NumPy vs SciPy
- Python Array Indexing and Slicing
- PyGame
- PyTorch
- Python & Libraries
- Python with MySQL
- Python with MongoDB
- Path Planning Algorithm in Python
- Image Processing with Python
- Python and Machine Learning
- Numerical Computation with Python
- Web Automation in Python
- Network Fundamentals and Socket Programming
- Basic Artificial Neural Networks in Python