Performing linear regression with a complex set of data with many features is very unwieldy. Say you wanted to create a hypothesis from three (3) features that included all the quadratic terms:
That gives us 6 features. The exact way to calculate how many features for all polynomial terms is the combination function with repetition: http://www.mathsisfun.com/combinatorics/combinations-permutations.html
For 100 features, if we wanted to make them quadratic we would get
We can approximate the growth of the number of new features we get with all quadratic terms with
Example: let our training set be a collection of 50 x 50 pixel black-and-white photographs, and our goal will be to classify which ones are photos of cars. Our feature set size is then n = 2500 if we compare every pair of pixels.
Now let's say we need to make a quadratic hypothesis function. With quadratic features, our growth is
Neural networks offers an alternate way to perform machine learning when we have complex hypotheses with many features.
Neural networks are limited imitations of how our own brains work. They've had a big recent resurgence because of advances in computer hardware.
There is evidence that the brain uses only one "learning algorithm" for all its different functions. Scientists have tried cutting (in an animal brain) the connection between the ears and the auditory cortex and rewiring the optical nerve with the auditory cortex to find that the auditory cortex literally learns to see.
This principle is called "neuroplasticity" and has many examples and experimental evidence.
Let's examine how we will represent a hypothesis function using neural networks.
At a very simple level, neurons are basically computational units that take input (dendrites) as electrical input (called "spikes") that are channeled to outputs (axons).
In our model, our dendrites are like the input features
In this model our x0 input node is sometimes called the "bias unit." It is always equal to 1.
In neural networks, we use the same logistic function as in classification:
Our "theta" parameters are sometimes instead called "weights" in the neural networks model.
Visually, a simplistic representation looks like:
Our input nodes (layer 1) go into another node (layer 2), and are output as the hypothesis function.
The first layer is called the "input layer" and the final layer the "output layer," which gives the final value computed on the hypothesis.
We can have intermediate layers of nodes between the input and output layers called the "hidden layer."
We label these intermediate or "hidden" layer nodes
If we had one hidden layer, it would look visually something like:
The values for each of the "activation" nodes is obtained as follows:
This is saying that we compute our activation nodes by using a 3×4 matrix of parameters. We apply each row of the parameters to our inputs to obtain the value for one activation node. Our hypothesis output is the logistic function applied to the sum of the values of our activation nodes, which have been multiplied by yet another parameter matrix
Each layer gets its own matrix of weights,
The dimensions of these matrices of weights is determined as follows:
The +1 comes from the addition in
Example: layer 1 has 2 input nodes and layer 2 has 4 activation nodes. Dimension of
In this section we'll do a vectorized implementation of the above functions. We're going to define a new variable
In other words, for layer j=2 and node k, the variable z will be:
The vector representation of x and
Setting
We are multiplying our matrix
Now we can get a vector of our activation nodes for layer j as follows:
Where our function g can be applied element-wise to our vector
We can then add a bias unit (equal to 1) to layer j after we have computed
To compute our final hypothesis, let's first compute another z vector:
We get this final z vector by multiplying the next theta matrix after
This last theta matrix
We then get our final result with:
Notice that in this last step, between layer j and layer j+1, we are doing exactly the same thing as we did in logistic regression.
Adding all these intermediate layers in neural networks allows us to more elegantly produce interesting and more complex non-linear hypotheses.
A simple example of applying neural networks is by predicting
The graph of our functions will look like:
Remember that
Let's set our first theta matrix as:
This will cause the output of our hypothesis to only be positive if both
So we have constructed one of the fundamental operations in computers by using a small neural network rather than using an actual AND gate. Neural networks can also be used to simulate all the other logical gates.
The
We can combine these to get the XNOR logical operator (which gives 1 if
For the transition between the first and second layer, we'll use a
For the transition between the second and third layer, we'll use a
Let's write out the values for all our nodes:
And there we have the XNOR operator using two hidden layers!
To classify data into multiple classes, we let our hypothesis function return a vector of values. Say we wanted to classify our data into one of four final resulting classes:
Our final layer of nodes, when multiplied by its theta matrix, will result in another vector, on which we will apply the g() logistic function to get a vector of hypothesis values.
Our resulting hypothesis for one set of inputs may look like:
In which case our resulting class is the third one down, or .
We can define our set of resulting classes as y:
Our final value of our hypothesis for a set of inputs will be one of the elements in y.