您的位置:首页 > 理论基础 > 计算机网络

文章标题

2015-08-13 09:54 996 查看

Nural Network (Representation)

@(Machine Learning)[neural network|representation]

This is a learning note mainly from coursera wiki for A.ng’s machine learning lesson.

Representation of a single neuron















Non-linear Hypotheses

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:

g(θ0+θ1x21+θ2x1x2+θ3x1x3+θ4x22+θ5x2x3+θ6x23)

That gives us 6 features. The exact way to calculate how many features for all polynomial terms is the combination function with repetition : (n+r−1)!r!(n−1)!. In this case we are taking all two-element combinations of three features: (3+2−1)!(2!⋅(3−1)!)=4!4=6. (Note: you do not have to know these formulas, I just found it helpful for understanding).

For 100 features, if we wanted to make them quadratic we would get (100+2−1)!(2⋅(100−1)!)=5050 resulting new features.

We can approximate the growth of the number of new features we get with all quadratic terms with O(n2/2). And if you wanted to include all cubic terms in your hypothesis, the features would grow asymptotically at O(n3). These are very steep growths, so as the number of our features increase, the number of quadratic or cubic features increase very rapidly and becomes quickly impractical.

Example: let our training set be a collection of 50x50 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 O(n2/2). So our total features will be about 25002/2=3125000, which is very impractical.

Neural networks offers an alternate way to perform machine learning when we have complex hypotheses with many features.

Neurons and the Brain

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.

Model Representation I

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 (x1⋯xn), and the output is the result of our hypothesis function:

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: 11+e−θTx. In neural networks however we sometimes call it a sigmoid (logistic) activation function.

Our “theta” parameters are sometimes instead called “weights” in the neural networks model.

Visually, a simplistic representation looks like:

⎡⎣⎢x0x1x2⎤⎦⎥→[ ]→hθ(x)

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 a20⋯a2n and call them “activation units.”

a(j)i="activation" of unit i in layer jΘ(j)=matrix of weights controlling function mapping from layer j to layer j+1

If we had one hidden layer, it would look visually something like:

⎡⎣⎢⎢⎢x0x1x2x3⎤⎦⎥⎥⎥→⎡⎣⎢⎢⎢a(2)1a(2)2a(2)3⎤⎦⎥⎥⎥→hθ(x)

The values for each of the “activation” nodes is obtained as follows:

a(2)1=g(Θ(1)10x0+Θ(1)11x1+Θ(1)12x2+Θ(1)13x3)a(2)2=g(Θ(1)20x0+Θ(1)21x1+Θ(1)22x2+Θ(1)23x3)a(2)3=g(Θ(1)30x0+Θ(1)31x1+Θ(1)32x2+Θ(1)33x3)hΘ(x)=a(3)1=g(Θ(2)10a(2)0+Θ(2)11a(2)1+Θ(2)12a(2)2+Θ(2)13a(2)3)

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 Θ(2) containing the weights for our second layer of nodes.

Each layer gets its own matrix of weights, Θ(j).

The dimensions of these matrices of weights is determined as follows:

If network has sj units in layer j and sj+1 units in layer j+1, then Θ(j) will be of dimension sj+1×(sj+1).

The +1 comes from the addition in Θ(j) of the “bias nodes,” x0 and Θ(j)0. In other words the output nodes will not include the bias nodes while the inputs will.

Example: layer 1 has 2 input nodes and layer 2 has 4 activation nodes. Dimension of Θ(1) is going to be 4×3 where sj=2 and sj+1=4, so sj+1×(sj+1)=4×3.

Model Representation II

In this section we’ll do a vectorized implementation of the above functions. We’re going to define a new variable z(j)k that encompasses the parameters inside our g function. In our previous example if we replaced the variable z for all the parameters we would get:

a(2)1=g(z(2)1)a(2)2=g(z(2)2)a(2)3=g(z(2)3)

In other words, for a given layer j and node k, the variable z will be:

z(j)k=Θ(j−1)k,0x0+Θ(j−1)k,1x1+⋯+Θ(j−1)k,nxn

The vector representation of x and z(j) is:

x=⎡⎣⎢⎢⎢x0x1⋯xn⎤⎦⎥⎥⎥z(j)=⎡⎣⎢⎢⎢⎢⎢z(j)1z(j)2⋯z(j)n⎤⎦⎥⎥⎥⎥⎥

We can compute z(j) with:

z(j)=Θ(j−1)x

Setting x=a(1), we can rewrite the equation as:

z(j)=Θ(j−1)a(1)

We are multiplying our matrix Θ(j−1) with dimensions sj×(n+1) (where sj is the number of our activation nodes) by our vector x with height (n+1). This gives us our vector z(j) with height sj.

Now we can get a vector of our activation nodes for layer j as follows:

a(j)=g(z(j))

Where our function g can be applied element-wise to our vector z(j).

We can then add a bias unit (equal to 1) to layer j after we have computed a(j). This will be element a(j)0 and will be equal to 1.

To compute our final hypothesis, let’s first compute another z vector:

z(j+1)=Θ(j)a(j)

We get this final z vector by multiplying the next theta matrix after Θ(j−1) with the values of all the activation nodes we just got.

This last theta matrix (Θ(j)) will have only one row so that our result is a single number.

We then get our final result with:

hΘ(x)=a(j+1)=g(z(j+1))

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.

Examples and Intuitions I

A simple example of applying neural networks is by predicting x1 AND x2, which is the logical ‘and’ operator and is only true if both x1 and x2 are 1.

The graph of our functions will look like:

⎡⎣⎢x0x1x2⎤⎦⎥→[g(z(2))]→hΘ(x)

Remember that x0 is our bias variable and is always 1.

Let’s set our first Θ matrix as:

Θ(1)=[−302020]

This will cause the output of our hypothesis to only be positive if both x1 and x2 are 1. In other words:

hΘ(x)=g(−30+20x1+20x2)x1=0 and x2=0 then g(−30)≈0x1=0 and x2=1 then g(−10)≈0x1=1 and x2=0 then g(−10)≈0x1=1 and x2=1 then g(10)≈1

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.

Examples and Intuitions II

The Θ(1) matrices for AND, NOR, and OR are:

AND:Θ(1)=[−302020]NOR:Θ(1)=[10−20−20]OR:Θ(1)=[−102020]

We can combine these to get the XNOR logical operator (which gives 1 if x1 and x2 are both 0 or both 1).

⎡⎣⎢x0x1x2⎤⎦⎥→⎡⎣a(2)1a(2)2⎤⎦→[a(3)]→hΘ(x)

For the transition between the first and second layer, we’ll use a Θ(1) matrix that combines the values for AND and NOR:

Θ(1)=[−301020−2020−20]

For the transition between the second and third layer, we’ll use a Θ(2) matrix that uses the value for OR:

Θ(2)=[−102020]

Let’s write out the values for all our nodes:

a(2)=g(Θ(1)⋅x)a(3)=g(Θ(2)⋅a(2))hΘ(x)=a(3)

And there we have the XNOR operator using one hidden layer!

Multiclass Classification

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:

⎡⎣⎢⎢⎢⎢⎢⎢x0x1x2⋯xn⎤⎦⎥⎥⎥⎥⎥⎥→⎡⎣⎢⎢⎢⎢⎢a(2)0a(2)1a(2)2⋯⎤⎦⎥⎥⎥⎥⎥→⎡⎣⎢⎢⎢⎢⎢a(3)0a(3)1a(3)2⋯⎤⎦⎥⎥⎥⎥⎥→⋯→⎡⎣⎢⎢⎢⎢hΘ(x)1hΘ(x)2hΘ(x)3hΘ(x)4⎤⎦⎥⎥⎥⎥→

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:

hΘ(x)=⎡⎣⎢⎢⎢0010⎤⎦⎥⎥⎥

In which case our resulting class is the third one down, or hΘ(x)3.

We can define our set of resulting classes as y:

y(i)=⎡⎣⎢⎢⎢1000⎤⎦⎥⎥⎥, ⎡⎣⎢⎢⎢0100⎤⎦⎥⎥⎥, ⎡⎣⎢⎢⎢0010⎤⎦⎥⎥⎥, ⎡⎣⎢⎢⎢0001⎤⎦⎥⎥⎥,

Our final value of our hypothesis for a set of inputs will be one of the elements in y.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息