Intro to Neural Networks
You've probably seen images like the ones below that represent machine learning models, but what do they actually mean? The goal of this lesson is to begin to build some intuition about these models.
Neural networks are a sub-category of machine learning models.
The diagrams below all represent the simplest category of neural networks called a Feed Forward Networks (FFNN). You will also encounter the related names Fully Connected Neural Network (FCNN) and Multilayer Perceptron (MLP).
Neural Networks come in many shapes and sizes.
In practice, networks can have many millions of nodes.
"Feed Forward" and "Fully Connected"
Can you explain what makes these networks fully connected? What makes them feed forward?
Inputs and Outputs
In machine learning, we need to represent our input and output data with numbers. There are lots of different ways to do that. Any of the methods below are valid targets for the inputs and outputs of a neural network.
- MIDI Note Number
- Frequency (Hz)
- Spectrograms
- Audio waveforms (Wavenet did this, but there are now better ways to generate waveforms)
- ABC notation (via tokens)
- What are some others?
Some formats work better than others!
Weights
How did we calculate the 24 in the first hidden layer?
Where do these orange values come from?
A more practical example
Let's discuss how we could create a neural network that generates chord sequences.
Points so far
- A neural network is a function that takes multiple inputs and produces multiple outputs.
- There are two kinds of values that we need to think about: data and parameters
- Data is the information flowing through the network (inputs, activations, and outputs).
- Parameters are the internal values of the network (weights and biases) that are updated during training.
Neurons with bias
Remember I said that we skipped over some of the details of the neurons? Let us add it for completeness.
Neurons with bias and "activation"
The activation function is applied to the output of hidden layers.
Softmax
Take a look at the output of the matrix above. Data flowing through a neural network is typically represented as a column vector.
What we want is an array of probabilities for each possible output. That is, we want a vector that sums to . A naive way to do this would be to divide each value by the sum of the vector.
Here is how this would be written in machine learning resources:
In practice, we use a slightly different approach called the softmax function. For an input vector, , the softmax function is defined as:
This evaluates to a probability distribution, which is a vector of probabilities that sum to 1.
Notice how the softmax exaggerates the difference between the values? This is one of the reasons it is preferred over the naive approach.
Loss
Now that we have a probability distribution, there is one more function we need to complete our model. The loss function measures how "wrong" our model weights are. Classification models and generative models like the Transformer often use the cross-entropy loss function.
For a single example with a true one-hot label vector and predicted probabilities , the cross-entropy loss is:
With a one-hot label, only the correct class has (all others are 0), so this reduces to the negative log-likelihood of the model’s probability for the correct class:
Softmax Output
Cross-Entropy Loss
Resources
- But what is a neural network? A Great YouTube video by 3Blue1Brown that covers this material and more.