neural.htm
来自「关于windows游戏编程的一些文章还有相关图形」· HTM 代码 · 共 814 行 · 第 1/5 页
HTM
814 行
</B><P> </P>
<B><P>X1	X2	Output</P>
</B><P>-1	-1	-1</P>
<P>-1	1	-1</P>
<P>1	-1	-1</P>
<P>1	1	1</P>
<P> </P>
<P>And here is the activation function <B>f</B><SUB>c</SUB><B>(x)</B> that we will use:</P>
<P> </P>
<P>Eq. 6.0</P>
<B><P>f</B><SUB>c</SUB><B>(x)</B> <B>= 	</B>1, if <B>x</B> <FONT FACE="Symbol">³</FONT>
<FONT FACE="Symbol">q</FONT>
</P><DIR>
<DIR>
<P>-1, if <B>x</B> < <FONT FACE="Symbol">q</FONT>
	</P>
<P> </P></DIR>
</DIR>
<P>Notice that the function is step with bipolar outputs. Before we continue, let me place a seed in your mind; the bias and threshold end up doing the same thing, they give us another degree of freedom in our neurons that make the neurons respond in ways that can't be achieved without them. You will see this shortly.</P>
<P> </P>
<P> </P>
<P> </P>
<P>	The single neurode net in Figure 8.0 is going to perform a classification for us. It is going to tell us if our input is in one class or another. For example, is this image a tree or <B><I>not</B></I> a tree. Or in our case is this input (which just happens to be the logic for an <B>AND</B>) in the +1 or -1 class? This is the basis of most neural nets and the reason I was belaboring linear separability. We need to come up with a linear partitioning of space that maps our inputs and outputs so that there is a solid delineation of space that separates them. Thus, we need to come up with the correct weights and a bias that will do this for us. But how do we do this? Do we just use trial and error or is there a methodology? The answer is that there are a number of training methods to teach a neural net. These training methods work on various mathematical premises and can be proven, but for now, we're just going to pull some values out of the hat that work. These exercises will lead us into the learning algorithms and more complex nets that follow.</P>
<P> </P>
<P>	All right, we are trying to finds weights <B>w</B><SUB>i</SUB> and bias <B>b</B> that give use the correct result when the various inputs are feed to our network with the given activation function <B>f</B><SUB>c</SUB><B>(x).</B> Let's write down the activation summation of our neurode and see if we can infer any relationship between the weights and the inputs that might help us. Given the inputs <B>X</B><SUB>1</SUB> and <B>X</B><SUB>2</SUB> with weights <B>w</B><SUB>1</SUB> and <B>w</B><SUB>2</SUB> along with <B>B</B>=1 and bias <B>b</B>, we have the following formula:</P>
<P> </P>
<P>Eq. 7.0</P>
<B><P>X</B><SUB>1</SUB>*<B>w</B><SUB>1</SUB> + <B>X</B><SUB>2</SUB>*<B>w</B><SUB>2</SUB> + <B>B</B>*<B>b</B>=<FONT FACE="Symbol">q</FONT>
</P>
<P> </P>
<P>Since <B>B</B> is always equal to 1.0 the equation simplifies to:</P>
<P> </P>
<B><P>X</B><SUB>1</SUB>*<B>w</B><SUB>1</SUB> + <B>X</B><SUB>2</SUB>*<B>w</B><SUB>2</SUB> + <B>b</B>=<FONT FACE="Symbol">q</FONT>
</P>
<P>.</P>
<P>.</P>
<B><P>X</B><SUB>2</SUB> = -<B>X</B><SUB>1</SUB>*<B>w</B><SUB>1</SUB>/<B>w</B><SUB>2</SUB> + (<FONT FACE="Symbol">q</FONT>
-<B>b</B>)/<B>w</B><SUB>2 </SUB>(solving in terms of <B>X</B><SUB>2</SUB>)</P>
<P> </P>
<B><P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P>Figure 9.0 - Mathematical Decision Boundaries Generated by Weights, Bias, and <FONT FACE="Symbol">q</FONT>
. </P>
<P> </P>
</B><P><IMG SRC="Image12.gif" WIDTH=576 HEIGHT=370></P>
<P>What is this entity? It's a line! And if the left hand side is greater than or equal to <FONT FACE="Symbol">q</FONT>
, that is, (<B>X</B><SUB>1</SUB>*<B>w</B><SUB>1</SUB> + <B>X</B><SUB>2</SUB>*<B>w</B><SUB>2</SUB> + <B>b</B>) then the neurode will fire and output 1, otherwise the neurode will output -1. So the line is a decision boundary. Figure 9.0a illustrates this. Referring to the figure, you can see that the slope of the line is <B>-w<SUB>1</SUB>/w<SUB>2</B></SUB> and the <B>X<SUB>2</B></SUB> intercept is (<FONT FACE="Symbol">q</FONT>
-<B>b</B>)/<B>w</B><SUB>2</SUB>. Now can you see why we can get rid of <FONT FACE="Symbol">q</FONT>
? It is part of a constant and we can always scale b to take up any loss, so we will assume that <FONT FACE="Symbol">q</FONT>
<B> </B>= 0, and the resulting equation is:</P>
<P> </P>
<B><P>X</B><SUB>2</SUB> = -<B>X</B><SUB>1</SUB>*<B>w</B><SUB>1</SUB><B>/w</B><SUB>2</SUB> - <B>b/w</B><SUB>2</P>
</SUB><P> </P>
<P>What we want to find are weights <B>w<SUB>1</B></SUB> and <B>w</B><SUB>2</SUB> and bias <B>b</B> so that it separates our outputs or classifies them into singular partitions without overlap. This is the key to linear separability. Figure 9.0b shows a number of decision boundaries that will suffice, so we can pick any of them. Let's pick the simplest values which would be:</P>
<P> </P>
<B><P>w</B><SUB>1</SUB> = <B>w</B><SUB>2</SUB> = 1</P>
<B><P>b</B> = -1</P>
<P>With these values our decision boundary becomes:</P>
<P> </P>
<B><P>X</B><SUB>2</SUB> = -<B>X</B><SUB>1</SUB>*<B>w</B><SUB>1</SUB><B>/w</B><SUB>2</SUB> - <B>b/w</B><SUB>2</SUB> -> <B>X</B><SUB>2</SUB> = -1*<B>X</B><SUB>1</SUB> + 1</P>
<P> </P>
<P>The slope is -1 and the <B>X</B><SUB>2</SUB> intercept is 1. If we plug the input vectors for the logical <B>AND</B> into this equation and use the <B>f</B><SUB>c</SUB><B>(x)</B> activation function then we will get the correct outputs. For example if, <B>X</B><SUB>2</SUB> + <B>X</B><SUB>1</SUB>-1 >0 then fire the neurode, else output -1. Let's try it with our <B>AND</B> inputs and see what we come up with:</P>
<P> </P>
<P> </P>
<P> </P>
<B><P>Table 4.0 - Truth Table for Bipolar AND with decision boundary.</P>
</B><P> </P>
<B><P>Input	X1	X2	Output (X2+X1-1)</P>
</B><P>	-1	-1	(-1) +( -1) -1 = 3 < 0 	don't fire, output -1</P>
<P>-1	1	(-1) + (1) -1 = -1< 0 	don't fire, output -1</P>
<P>1	-1	(1) + (-1) -1 = -2 < 0	don't fire, output -1</P>
<P>1	1	(1) + (1)-1 = 1 > 0	fire, output 1</P>
<P> </P>
<P>As you can see, the neural network with the proper weights and bias solves the problem perfectly. Moreover, there are a whole family of weights that will do just as well (sliding the decision boundary in a direction perpendicular to itself). However, there is an important point here. Without the bias or threshold, only lines through the origin would be possible since the <B>X</B><SUB>2</SUB> intercept would have to be 0. This is very important and the basis for using a bias or threshold, so this example has proven to be an important one since it has flushed this fact out. So, are we closer to seeing how to algorithmically find weights? Yes, we now have a geometrical analogy and this is the beginning of finding an algorithm. </P>
<P> </P>
</FONT><B><FONT SIZE=4><P>The Ebb of Hebbian </P>
</B></FONT><FONT SIZE=2><P> </P>
<P>Now we are ready to see the first learning algorithm and its application to a neural net. One of the simplest learning algorithms was invented by <B>Donald Hebb</B> and it is based on using the input vectors to modify the weights in a way so that the weight create the best possible linear separation of the inputs and outputs. Alas, the algorithm works just OK. Actually, for inputs that are orthogonal it is perfect, but for non-orthogonal inputs, the algorithm falls apart. Even though, the algorithm doesn't result in correct weight for all inputs, it is the basis of most learning algorithms, so we will start here.</P>
<P> </P>
<P>Before we see the algorithm, remember that it is for a single neurode, single layer neural net. You can of course, place a number of neurodes in the layer, but they will all work in parallel and can be taught in parallel. Are you starting to see the massive parallization that neural nets exhibit? Instead of using a single weight vector, a multi-neurode net uses a weight matrix. Anyway, the algorithm is simple, it goes something like this:</P>
<P> </P>
<B><P>Given:</P>
</B><P> </P>
<UL>
<LI>Inputs vectors are in bipolar form <B>I</B> = (-1,1,0,...-1,1) and contain k elements. </LI>
<LI>There are <B>n</B> input vectors and we will refer to the set as <B>I</B> and the <B>j</B>th element as <B>I</B><SUB>j</SUB><B>.</LI>
</B><LI>Outputs will be referred to as <B>y</B><SUB>j</SUB> and there are k of them, one for each input <B>I</B><SUB>j</SUB>.</LI>
<LI>The weights <B>w</B><SUB>1</SUB><B>-w</B><SUB>k</SUB> are contained in a single vector <B>w</B> = (<B>w</B><SUB>1</SUB>, <B>w</B><SUB>2</SUB>, ... <B>w</B><SUB>k</SUB>).</LI></UL>
<P> </P>
<B><P>Step 1.</B> Initialize all your weights to 0, and let them be contained in a vector <B>w</B> that has <B>n</B> entries. Also initialize the bias b to 0.</P>
<P> </P>
<B><P>Step 2.</B> For <B>j</B> = 1 to <B>n</B> do</P>
<P> <B>b</B> = <B>b</B> + <B>y<SUB>j</B></SUB> 	(where y is the desired output)</P>
<P> <B>w</B> = <B>w</B> + <B>I<SUB>j</B></SUB> * <B>y<SUB>j</B></SUB> 	(remember this is a vector operation)</P>
<P>end do</P>
<P> </P>
<P>The algorithm is nothing more than an <B>"accumulator"</B> of sorts. Shifting, the decision boundary based on the changes in the input and output. The only problem is that it sometimes can't move the boundary fast enough (or at all) and <B>"learning"</B> doesn't take place.</P>
<P> </P>
<P>So how do we use <B>Hebbian</B> learning? The answer is, the same as the previous network except that now we have an algorithmic method teach the net with, thus we refer to the net as a <B>Hebb </B>or <B>Hebbian Net</B>. As an example, let's take our trusty logical <B>AND</B> function and see if the algorithm can find the proper weights and bias to solve the problem. The following summation is equivalent to running the algorithm:</P>
<P> </P>
<B><P>w</B> = [<B>I</B><SUB>1</SUB>*<B>y</B><SUB>1</SUB>] + [<B>I</B><SUB>2</SUB>*<B>y</B><SUB>2</SUB>] + [<B>I</B><SUB>3</SUB>*<B>y</B><SUB>3</SUB>] + [<B>I</B><SUB>4</SUB>*<B>y</B><SUB>4</SUB>] = [(-1, -1)*(-1)] + [(-1, 1)*(-1)] + [( 1, -1)*(-1)] + [(1, 1)*(1)] = (2,2) </P>
<B><P> </P>
<P>b</B> = <B>y</B><SUB>1</SUB> + <B>y</B><SUB>2</SUB> + <B>y</B><SUB>3</SUB> + <B>y</B><SUB>4</SUB> = (-1) + (-1) + (-1) + (1) = -2</P>
<P> </P>
<P>Therefore, <B>w</B><SUB>1</SUB>=2, <B>w</B><SUB>2</SUB>=2, and <B>b</B>=-2. These are simply scaled versions of the values <B>w</B><SUB>1</SUB>=1, <B>w</B><SUB>2</SUB>=1, <B>b</B>=-1 that we derived geometrically in the previous section. Killer huh! With this simple learning algorithm we can train a neural net (consisting of a single neurode) to respond to a set of inputs and either classify the input as true or false, 1 or -1. Now if we were to array these neurodes together to create a network of neurodes then instead of simple classifying the inputs as on or off, we can associate patterns with the inputs. This is one of the foundations for the next network neural net structure; the <B>Hopfield</B> net. One more thing, the activation function used for a Hebb Net is a step with a threshold of 0.0 and bipolar outputs 1 and -1.</P>
<P> </P>
<P>	To get a feel for Hebbian learning and how to implement an actual Hebb Net, Listing 2.0 contains a complete Hebbian Neural Net Simulator. You can create networks with up to 16 inputs and 16 neurodes (outputs). The program is self explanatory, but there are a couple of interesting properties: you can select 1 of 3 activation functions, and you can input any kind of data you wish. Normally, we would stick to the Step activation function and inputs/outputs would be binary or bipolar. However, in the light of discovery, maybe you will find something interesting with these added degrees of freedom. However, I suggest that you begin with the step function and all bipolar inputs and outputs.</P>
</FONT><FONT SIZE=1><P> </P>
<P> </P>
</FONT><B><FONT SIZE=2><P>Listing 2.0 - A Hebb Net Simulator</P>
</B><P> </P>
</FONT><B><FONT FACE="Arial" SIZE=1><P>// HEBBIAN NET SIMULATOR /////////////////////////////////////////////////////////</P>
<P> </P>
<P>// INCLUDES //////////////////////////////////////////////////////////////////////</P>
<P> </P>
<P>#include <conio.h></P>
<P>#include <stdlib.h></P>
<P>#include <malloc.h></P>
<P>#include <memory.h></P>
<P>#include <string.h></P>
<P>#include <stdarg.h></P>
<P>#include <stdio.h></P>
<P>#include <math.h></P>
<P>#include <io.h></P>
<P>#include <fcntl.h></P>
<P> </P>
<P>// DEFINES ///////////////////////////////////////////////////////////////////////</P>
<P> </P>
<P>#define MAX_INPUTS		16	// maximum number of inputs</P>
<P>#define MAX_OUTPUTS		16	// maximum number of outputs</P>
<P> </P>
<P>#define ACTF_STEP		0	// use a binary step activation function fs(x)</P>
<P>#define ACTF_LINEAR		1	// use a linear activation function fl(s)</P>
<P>#define ACTF_EXP		2	// use an inverse exponential activation function fe(x)</P>
<P> </P>
<P>// MACROS ////////////////////////////////////////////////////////////////////////</P>
<P> </P>
<P>// used to retrieve the i,jth element of a linear, row major, matrix</P>
<P> </P>
<P>#define MAT(mat,width,i,j) (mat[((width)*i)+(j)])</P>
<P> </P>
<P>// GLOBALS ///////////////////////////////////////////////////////////////////////</P>
<P> </P>
<P>float	input_xi[MAX_INPUTS],		// holds that input values</P>
<P>		input_i[MAX_INPUTS],	// holds a single input vector</P>
<P>		output_i[MAX_OUTPUTS],	// holds a single output vector </P>
<P>		input_act[MAX_OUTPUTS],	// holds the summed input activations </P>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?