Adaline Explained With Python Example
In this post, you will learn the concepts of Adaline (ADAptive LInear NEuron), a machine learning algorithm, along with a Python example.
Join the DZone community and get the full member experience.Join For Free
In this post, you will learn the concepts of Adaline (ADAptive LInear NEuron), a machine learning algorithm, along with a Python example. Like Perceptron, it is important to understand the concepts of Adaline as it forms the foundation of learning neural networks. The concept of Perceptron and Adaline could found to be useful in understanding how gradient descent can be used to learn the weights which when combined with input signals is used to make predictions based on unit step function output.
Here are the topics covered in this post in relation to Adaline algorithm and its Python implementation:
- What's Adaline?
- Adaline Python implementation
- Model trained using Adaline implementation
Adaline, like Perceptron, also mimics a neuron in the human brain. You may want to read one of my related posts on Perceptron — Perceptron explained using Python example. Adaline is also called as single-layer neural network. Here is the diagram of Adaline:
The following represents the working of Adaline machine learning algorithm based on the above diagram:
- Net Input function - Combination of Input signals of different strength (weights): Input signals of different strength (weights) get combined / added in order to be fed into the activation function. The combined input or sum of weighted inputs can also be called as net input. Pay attention to the Net-input function shown in the above diagram
- Net input is fed into activation function (Linear): Net input is fed into activation function. The activation function of adaline is an identity function. If Z is net input, the identity function would look like \(g(Z) = Z\). The activation function is linear activation function as the output of the function is linear combination of input signals and weights.
- Activation function output is used to learn weights: The output of activation function (same as net input owing to identity function) is used to calculate the change in weights related to different inputs which will be updated to learn new weights. Pay attention to feedback loop shown with text Error or cost. Recall that in Perceptron, the activation function is a unit step function and the output is binary (1 or 0) based on whether the net input value is greater than or equal to zero (0) or otherwise.
- Threshold function - Binary prediction (1 or 0) based on unit step function: The prediction made by Adaline neuron is done in the same manner as in case of Perceptron. The output of activation function, which is net input is compared with 0 and the output is 1 or 0 depending upon whether the net input is greater than or equal to 0. Pay attention in the above diagram as to how the output of activation function is fed into threshold function.
Adaline Python Implementation
The adaline algorithm explained in previous section with the help of diagram will be illustrated further with the help of Python code. Here are the algorithm steps and the related Python implementation:
- Weighted input signals combined as net input: First step is to combine the input signals with respective weights (strength of input signals) and come up with sum of weighted inputs. This is also termed as net input.
- Activation function invoked with net input: Net input is fed into activation function to calculate the output. The activation function is a linear activation function. It is an identity function. Thus, \(g(Z) = Z\). Note that the output or return value of activation function is same as input (identity function)
- Prediction based on unit step function: Prediction is made based on the unit step function which provides binary output as 1 or 0 based on whether the output of activation function is greater than or equal to zero. If the output of activation function is greater than or equal to zero, the prediction is 1 or else 0. Note how activation function is invoked with net input and the output of activation function is compared with 0.
- Weights learned using activation function output (continuous value): Unlike Perceptron where weights are learned based on the prediction value which is derived as out of unit step function, the weights in case of Adaline is learned by comparing the actual / expected value with the out of activation function which is a continuous value. Note that the weights are learned based on batch gradient descent algorithm which requires the weights to be updated after considering the weight updates related to all training examples. This is unlike stochastic gradient descent where weights are updated after each training example.
Here is the entire Python code of Adaline algorithm custom implementation:
Model Trained With Adaline Algorithm
Here is the Python code which represents the breast cancer classification model trained using Adaline implementation explained in the previous section:
The score of the model on the test data set turns out to be around 0.63.
Here is the summary of what you learned in this post in relation to Adaline algorithm and its Python implementation:
- Adaline algorithm mimics a neuron in the human brain
- Adaline is similar to the algorithm Perceptron. It can also be termed as a single-layer neural network.
- The difference between Adaline and Perceptron lies in the manner in which weights are learned based on the differences between the output label and the continuous value output of the activation function. In Perceptron, the difference between an actual label and a predicted label is used to learn the weights.
Published at DZone with permission of Ajitesh Kumar, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.