Demystifying mathematics behind neural nets
Mathematics behind neural networks and how they work
‘Machine learning’ and ‘artificial intelligence’ are fancy ways of saying statistical mathematics. One of the most popular machine learning models is neural nets or neural network which is class of machine learning algorithms used to identify complex patterns in a dataset.
Neural nets are models that mimic neurons in a biological brain. Our brains have neurons (nodes) that are interconnected to one another through synapse (weights), similarly neural net then applies an activation function to the “sum of weighted inputs” from each incoming synapse and passes the result on to all the neurons (nodes) in the next layer.
To better understand let us break it down,
Let’s take the simplest neural network, The Perceptron.

It consists of one or more inputs, a processor, and a single output. A perceptron follows the feed-forward model, meaning inputs are sent into the nodes are processed and result is an output.
So, what does it even mean?
Let’s assume we have two inputs; input is the information or data that we have and we will call them x1 and x2.
Input 0: x1 = 12Input 1: x2 = 4
Now, in order to send the input into node, it has to travel through a road, i.e., weights. So when we pass the inputs through weight to neuron (node), they are multiplied to a value (often number between -1 and 1).
We typically assign random values,
Weight 0: 0.5Weight 1: -1
Let’s now mathematically pass the input through weight.
Input 0 * Weight 0 => 12 * 0.5 = 6Input 1 * Weight 1 => 4 * -1 = -4
Then we sum the weighted inputs, Input 0 + Input 1,
Sum = 6 + (-4) = 2
The output of a perceptron is generated by passing that sum through an activation function. In the case of a simple binary output, the activation function tells the perceptron whether to “fire” or not.
Activation functions can get a little bit doozy and difficult. If you start reading one of those artificial intelligence textbooks looking for more info about activation functions, you may soon find yourself reaching for a calculus textbook. However, with our friend the simple perceptron, we’re going to do something really easy. Let’s make the activation function the sign of the sum. In other words, if the sum is a positive number, the output is 1; if it is negative, the output is -1.
The Perceptron Algorithm:
- For every input, multiply that input by its weight.
- Sum all of the weighted inputs.
- Compute the output of the perceptron based on that sum passed through an activation function (the sign of the sum).
Let’s assume we have two arrays of numbers, the inputs, and the weights. For example:
float[] inputs = {12 , 4};float[] weights = {0.5,-1};
“For every input” implies a loop that multiplies each input by its corresponding weight. Since we need the sum, we can add up the results in that very loop.
Steps 1 and 2: Add up all the weighted inputs.
float sum = 0;for (int i = 0; i < inputs.length; i++) {
sum += inputs[i]*weights[i];
}
But how do we get output out of the neuron (node)?
We use a function that helps us decide if the neuron should be activated or not, which basically means whether neurons input to the network is important or not.
So, let’s try to summarize what we have learnt before we dive in deeper,
A single artificial neuron can be seen as:
Inputs -> weights -> sum -> activation function -> output
We use activation function to make sure ‘learning’ is possible for machines. As from the above example of perceptron, we can say with confidence that it is a linear function, that’s a simple number (d/dx f(x) = 1 in this case), never changing, not even remotely reliant on the input given. So, it gives no useful information, the input weights can’t be adjusted in a meaningful way, and so it prevents learning.
So, we apply activation function to get a derivative that depends on the input, so learning is possible, and your layers don’t collapse into a single neuron, activation function determines if/how the neuron fires based on the sum we have from Input 0 + Input 1.
So now let’s understand the activation function,
Neural network fig.2

A neural network without activation function is essentially a linear regression model, if you have noticed in the above diagram we have hidden layer, which are just layers of mathematical functions designed to produce specific result like XOR operation, but this is when we apply activation function, as you can see we have only one layer, which means we can only apply one activation function, in-order to apply more, we would need more hidden layers.
There are many activation function to name a few, Linear Activation Function, Non-Linear Activation Function, Sigmoid Activation Function, Tanh Activation Function, ReLU Activation Function, Maxout, ELU and Softmax Activation Function. For this article, we will only look into ReLU as it is most popular, but the activation function usage will vary depending on your use case.
Relu (Rectifier Linear Unit), the mathematical formula is f(x) = max(0,x),


As you can see from graph, the ReLU function looks a lot like ramp function, because it is ramp function, which is unary function where f(x) is always 0 if input is negative and it will result in same number as input if the integer is positive.
ReLU function f(x)=max(0,x). It means if x<=0 then f(x)=0, else f(x)=x. In the first case, when x<0 so the derivative of f(x) with respect to x gives result f'(x)=0. In the second case, it's clear to compute f'(x)=1.
If the function receives any negative input, it returns 0; however, if the function receives any positive value x, it will return that value. Both ReLU function and its derivative are monotonic, so output ReLU generates has range from 0 to infinite. Because there is no difficult arithmetic, the ReLU deep learning function is simple and does not require any heavy processing. As a result, the model can train or operate in less time. Sparsity is another significant quality that we consider to be an advantage of utilizing the ReLU activation function. For instance, in a model that detects human faces in photos, there may be a neuron that can identify eyes, which should obviously not be activated if the image is not of a face and is a three or bridge.
Conclusion
We have looked into the basics of neural nets, as neural nets is a vast subject, many data scientists solely focus on neural network techniques and neural network particularly works well on class of problems like image recognition. Currently, there is a lot of exciting research going on, around neural networks. After gaining sufficient knowledge in this basic session, you may want to explore reinforced learning, deep learning etc.
Citation & further read
Neural Networks: A Systematic Introduction” by Raúl Rojas
Deep Learning: A Practitioner’s Approach by Adam Gibson and Josh Patterson
Fundamentals of Deep Learning: Designing Next-Generation Artificial Intelligence Algorithms by Nikhil Buduma