Link Search Menu Expand Document

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:

  1. Take the source data
  2. Perform calculations on it
  3. Predict the solution
  4. Compare the solution with the actual solution
  5. 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

  1. Import the required packages.
  2. Declare and initialize the input and outputs of the data in the form of NumPy arrays.
  3. 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.
  4. Set the number of nodes or neurons for each layer.
  5. Set the activation function, which in this case is sigmoid function. In mathematical terms, sigmoid ( x ) = 1 / ( 1 + exp ( -x ) ).
  6. 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.
  7. Print the model's summary using the summary method.

Other useful articles:


Back to top

© , Learn Python 101 — All Rights Reserved - Terms of Use - Privacy Policy