Artificial Neural Network (ANN) – Python

An artificial neural network (ANN) is a computer model that can work together and solve problems. It can identify complex relationships in datasets, data mining, pattern recognition, etc. It’s an interconnected group of artificial neurons working together in proper sync. It’s inspired by the human brain the working of neurons in our brain is tried to replicate here artificially.

Simon Haykin stated the easiest and most informative definition of ANN.

Definition: “An artificial neural network is a system made up of many simple units working together in parallel. It’s great at learning from experience and using that knowledge when needed.”

It’s one of the well-regarded definitions of Artificial Neural Networks (ANNs).

 

Architecture

ANN architecture consists of 3 main components, Input Layer, Hidden Layer, and Output Layer.
Input Layer: It’s the initial layer of ANN, layer receives the input data (raw). It performs no computation on it and instead passes it to the next layer.
Hidden Layer: It receives the data from the input layer and processes it, and once completed with the processing part, passes the data to the next layer. There can be more than one hidden layer. They are very important for learning the complex patterns and relations.
Output Layer: These are the last step, they produce the final output/final result or the prediction of the entire network and present it in a representable format.

There are many interconnected neurons in each layer, which are further connected to neurons in the following layer. Now multiple terms like input, hidden, output, and node/neurons can be confusing. As mentioned above, they are derived from the human brain to replicate the human brain. So let’s see the similarities between a biological neuron and an artificial neuron.

 

The image above is a biological neuron, found in the human brain. From these, the artificial neurons are derived. The Nucleus is the node/neurons, dendrites are the inputs and axon are the output.  This is how an artificial neuron is built and works.

Applications of ANN

There are many applications of ANN in many fields few of which are given below.

1] Medical Diagnosis: It can analyze the X-rays, and reports, and detect the disease.

2] Image, Speech Recognition: Facial recognition systems use ANN to identify the person in photos or videos.

3] Finance: It can be used to predict stock prices based on historical data.

4] Autonomous Driving: Tesla cars are the best example of this, they use CNN, ANN, and  RNN to process real-time video and make decisions.

5]Recommendation Systems: Streaming services use ANNs to recommend movies based on your watching preferences.

 

Python Code: ANN

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import OneHotEncoder

These are the libraries we are going to use for this program. TensorFlow and Keras are Used to create and train the ANN. Meanwhile scikit-learn helps us with data preprocessing.

 

iris = load_iris()
X = iris.data  
y = iris.target

The Iris dataset is the most commonly used, it includes the measurements of iris flowers from three different species.

  1. X: Contains the feature data (sepal length, sepal width, petal length, petal width).
  2. y: Contains the labels (target species).

 

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

This step is self-explanatory, it Splits the data into training and testing sets (80% training, 20% testing).

 

model = Sequential()
model.add(Dense(20, input_dim=4, activation='relu')) 
model.add(Dense(15, activation='relu'))  
model.add(Dense(3, activation='softmax'))  

Here we have created a sequential model with input, hidden, and output layers.

  1. Dense(20): It is used to add a dense (fully connected) layer with 0 neurons or 6 neurons
  2. input_dim=8: used to Specify the input dimensions.
  3. activation=’relu’ or activation=’softmax’: These are activation functions used for classification and learning.

 

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(X_train, y_train, epochs=50,)

Here we have compiled and trained the model

  1. optimizer: We have used the Adam optimizer.
  2. loss: To calculate the loss function, categorical crossentropy.
  3. metrics: It’s used to evaluate the model.
  4. fit(): Trains the model on the training data.
  5. epochs=50: Trains for 50 epochs.

 

loss, accuracy = model.evaluate(X_test, y_test)
print(f'Test Accuracy: {accuracy:.4f}')

Here we have represented or printed the output of our model. In the output image, we can see loss and accuracy.

  1. loss=0.0683 Lower values of loss show that the model is accurate,
  2. An accuracy of 1.0000 (or 100%) means that the model has correctly classified all test instances.

 

Output

 

Leave a Reply

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