Introduction to Neural Network

2020-12-27

What Is a Neural Network?

Imagine your brain as a super-smart friend that can learn to recognize patterns, just like when you learn to recognize a person's face or your favorite song. A neural network is a computerized version that brain.

Think of it as a virtual network made up of tiny building blocks called neurons. These neurons work together to solve problems and make decisions, just like your brain does. Each neuron can take in information, process it, and pass it on to the next neuron.

In a neural network, we have layers of these neurons. The first layer might take in simple information, like the pixels of a picture. Each layer after that looks at more complex features, combining the information to understand things like shapes or colors.

Now, the cool part is that this network learns from examples. If we want it to recognize cats, we show it lots of pictures of cats. The network adjusts its connections between neurons to get better at recognizing cat-like patterns. It's like training a robot to identify things.


Example Problem

Suppose there's a gardener who loves measuring her beautiful red and blue flowers. She records the color, width, and length of each petal in a table. Most of the time, she's careful, but one day, she forgets to label the color for one measurement. Now, she wants to figure out the color of that mystery flower.

Neural Network Example Problem

Neural Network Example Problem

Solving Manually

The gardener decides to plot a graph with the petal's length on one axis and width on the other. She observes that most blue petals form one group, and red petals form another, which are slightly longer than the blue ones. When she plots the missing data point, she notices it falls within the group of red flowers. So, she can confidently conclude that the missing label is red.

Neural Network Solving Manually

Neural Network Solving Manually

She solved her problem but she had to do this all by herself. So she calls her friend, her computer which is much faster, to solve this.

Solving With a Computer

Instead of manually making graphs, the gardener now uses a computer brain called a neural network to predict a flower's color based on its size. This digital system looks at the petal's length and width and spits out a number as its guess. To keep things simple, the gardener decides that if the number is close to 1, the flower is likely red, and if it's close to 0, it's probably blue.

Neural Network Solving with a Computer


In the figure above, the computer was given measurements of a blue flower with length 2 and width 1, but it guessed red. Why did it make that mistake? Well, when it started, the computer had no idea and kind of made random guesses (using random numbers for weights and bias). But the more flower examples we give it, the smarter it becomes. It learns from its mistakes and fixes those initial guesses, making its predictions way better. Now, let's explore what these tricky things called 'weights' and 'bias' really mean.

Weights and Bias

Weights and bias are like the gears and levers in the brain of a computer, guiding it to make decisions and predictions. Every connection between neurons has a level of importance. So, for the measurements of length and width of a petal (let's call them m1 and m2), we have special numbers- weights (w1 and w2) showing how much value we give to each measurement.

Imagine crafting a unique formula. Each measurement is multiplied by its respective weight (w1 x m1 and w2 x m2), creating a "weighted sum" that reflects the calculated importance of each ingredient in the overall mixture.

But wait, there's another element in this formula called 'bias' (let's call it b). It's like adding a little something extra to the mix, even if all the measurements are zero. It helps the computer deal with situations where everything starts from scratch.

Neural Network Weights and Bias


Finally, the mix we created is squeezed to values between 0 and 1 for the prediction. A common way to do this is to use the 'Sigmoid' function.

Here's an example code to initialize random values of weights and bias in Python language.



Sigmoid Function

Sigmoid Function


Figure above shows the equation for sigmoid function. It is used to squash our number and transform it into a new one that's between 0 and 1. Let's see some example of this.

Sigmoid Function Examples


As you can see, it transformed (-5) to 0.0067, (0) to 0.5, and (+5) to 0.9933, all of which are between 0 and 1. The exponent (e) is just a mathematical symbol for the irrational number 2.71828...

Remember how we discussed that the Neural Network(NN) applies this sigmoid function to get its final output? This is known as the Activation Function of the neural network.

Neural Network Sigmoid Function


Defining a sigmoid function would look something like this in Python



Prediction using Activtion Function

Initially, the neural network uses random values for weights and bias. Let's say it assumes w1=0.5, w2=0.2, and b=0.3. For the data inputs of blue petal with length 2 and width 1, its calculation is shown in the figure below.

Neural Network Activation Function


Neural Network Prediction with Activation Function


Since the prediction didn't quite hit the mark, we ask our neural network to reconsider its connections; basically, we want it to tweak the values of those weights and the bias. So, we bring in another measurement of another flower. This new flower helps our neural network learn from its mistake. We tell the network the correct color, and it adjusts its internal settings – those weights and biases – to get a little closer to making accurate predictions.

The neural network is fine-tuned until the error is low –error is the difference between what it predicted and what's actually true. It's a bit like practicing a dance routine. At first, the steps might be a bit off, but with each practice round, you adjust and refine until the dance is flawless.

We can code our simple Neural Network function as such

With all of our code combined, we can define our Sigmoid function, Neural Network function, assign random values for weights and bias, and finally make prediction by calling the Neural Network function.



Build Your Own Neural Network

Now it's time for you to build your own Neural Network! Come join us in learning how to code and create your very own digital brain. Together, we'll explore the fascinating world of artificial intelligence, understand the magic behind neural networks, and unleash the power of coding to make predictions and solve problems.