Getting Started with Neural Networks
Neural Networks, sometimes abbreviated "NN," are an important tool in Machine Learning that are used in a variety of applications. These include: automatically recognizing handwritten letters and digits on letters, generating recommendations on social media, playing chess, and even predicting the stock market!
Different variations of Neural Networks exist, and they can also be used as a component in larger, more complex Machine Learning systems, such as in the Transformer architecture that is used in popular applications such as OpenAI's ChatGPT! In fact, GPT is an acronym for generative pre-trained transformer, a specific type of large language model that uses Neural Networks as part of the overall model architecture. Other types of Neural Networks include convolutional neural networks and recurrent neural networks.
Learning about the different components of Neural Networks and developing an intuition of the concepts, structure, and math that underpin it is an important step in advancing machine learning skills. Interactive NN seeks to introduce these concepts and ideas visually and intereactively so that students can build this intuition and background knowledge naturally.
What is a Neural Network?
Neural Networks, as their name suggests, were originally inspired by biological neural networks in neuroscience, although their modern interpretation and application have diverged since these original ideas, even if they share many vocabulary terms.
Each neuron in a Neural Network is connected with others, and each serves a different function in the overall network. This helps the network accomplish its task. To better understand the overall function of a Neural Network, these different components will be broken down and studied individually. They include:
- Artificial Neurons
- Inputs, which are real numbers
- Activation Functions
- Hidden Layers
- Interpreting Outputs
If a neural network contains three or more hidden layers, it is often called a "deep" Neural Network, which gives rise to the term deep learning!
Perceptrons
A simple model of an artificial neuron is called the perceptron. The idea for the perceptron dates back to 1943, and it was succesfully simulated with early computers in 1957. Therefore, the perceptron is not a new idea, but it still forms the basis for other, more modern methods!
The perceptron has three components:
- One or more binary inputs. Binary inputs are O or 1.
- Real number weights for each input, which describe how important each input is.
- A threshold value to determine the output
These components are combined as follows:
- The weighted sum of the inputs and weights produces a single value.
- That value is compared to the threshold value. If it is bigger than the threshold value, the ouput is 1. If it is less than the threshold, the ouput is 0.
That's it! Visually, a Peceptron with three inputs can be represented as:
Perceptron Example
Let's walk through a practical example using the perceptron structure.
Inputs
The inputs to a perceptron are always binary - either or . For this example, let's set the following values:Weights
Each input has a corresponding weight that controls how important it is to the output. The bigger the weight, the more important it is! Weights, unlike inputs, are real numbers. Real numbers can be thought of as any number on a number line, such as 0, 1, -0.5, and even ! Real numbers are commonly denoted as .
Let's use the following weights:
Weighted Sum
With the inputs and weights, the next step is to calculate their weighted sum. This means that every input is multiplied with its weight. Then, all of those results are added together.
And summed up together:
Threshold and Step Function
With the weighted sum, the last step is to compare it to the threshold value. The threshold value converts the raw output into a binary value - 0 or 1. The function that performs this conversion is called a Step Function.
If we set the , the step function is defined as follows, where is the weighted sum:
Binary Outputs
There is a limit on what binary outputs -- 0 or 1 -- can tell us. A binary output is True or False, Yes or No, On or Off. There is only two possible outcomes.
Binary outputs can still be useful, though. A practical example is creating a perceptron model to predict whether it will rain today. We could have seven inputs for each of the seven previous days. The input will be 1 if it rained, and 0 otherwise. The threshold is 1, and if the output is greater than 1, the model predicts rain. We can adjust the weights so that rain yesterday is weighted higher than rain seven days ago. In practical terms, this suggests that if rain yesterday is a better indicator of rain today than than rain from several days ago.
Here is our example:
Let's walk through the steps:
- Calculate the weighted sum of the inputs and their weights.
- Compare the weighted sum to the threshold value of the step function.
- Interpret the output! This perceptron's output is 0, so this predicts no rain today!
The details of the math are left to you as an exercise!
Even though it rained last Monday, Tuesday, and Wednesday, they are weighted less than more recent days, like Saturday or Sunday. So, the weighted sum does not meet the threshold, and our perceptron model predicts no rain.
This is cool! But we have some questions:
- How do we find the weights? Are they random, or guesses?
- Can we change the weights if needed, such as for different seasons or locations?
- How do we choose the threshold value? What if a threshold of 0 or 2 is better at predicting rain in our model?
- What if we want to predict something other than rain, like if it will be rainy, cloudy, sunny? That is three choices, so it doesn't fit into the binary model where we only have two options!
Building on Perceptrons
Perceptrons are just the building block for Neural Networks. The power and innovation behind Neural Networks uses many groups of perceptron and different variations of the math. There is a lot of creativity in developing Neural Networks! Some innovations on this simple perceptron model include:
- Using real number inputs, instead of only binary inputs.
- Organizing groups of perceptrons into layers.
- Using the output of one layer of neurons as the input to the next layer.
- New activation functions besides the simple step function.
- Being able to predict the most likely output from a group, instead of answering simple binary questions.
Here is a visual of a Neural Network, building on the perceptron model:
This may look complicated, but we already have all of the skills needed to build and interpret a Neural Network like this. Continue with Interactive NN to learn about and experiment with these different components!