📄 neural.htm
字号:
<P>void Print_Net(void)</P>
<P>{</P>
<P>// this function prints out the current weight matrix and biases along with the specifics</P>
<P>// about the net</P>
<P> </P>
<P>printf("\nThe Hebb Net has %d inputs and %d outputs",num_inputs, num_outputs);</P>
<P>printf("\nThe weight matrix is %dX%d",num_inputs, num_outputs);</P>
<P>printf("\nThe W[i,j]th element refers to the weight from the ith to jth neurode\n");</P>
<P> </P>
<P>for (int index_i = 0; index_i<num_inputs;index_i++)</P>
<P>	{</P>
<P>	printf("\n|");</P>
<P>	for (int index_j=0; index_j<num_outputs; index_j++)</P>
<P>		{</P>
<P>		// data is in row major form</P>
<P>		printf(" %2.2f ",MAT(weight_matrix,num_outputs,index_i,index_j));</P>
<P> </P>
<P>		} // end for index_j</P>
<P> </P>
<P>	printf("|");</P>
<P>	</P>
<P>} // end for index_row</P>
<P> </P>
<P>printf("\n\nBias weights for the net are:\n[");</P>
<P> </P>
<P>for (int index_j=0; index_j<num_outputs; index_j++)</P>
<P>	printf("%2.2f, ",bias_bi[index_j]);</P>
<P> </P>
<P>printf("]\n\n");</P>
<P> </P>
<P>} // end Print_Net</P>
<P> </P>
<P>//////////////////////////////////////////////////////////////////////////////////</P>
<P> </P>
<P>void Reset_Net(void)</P>
<P>{</P>
<P>// clear out all the matrices</P>
<P>memset(weight_matrix,0,num_inputs*num_outputs*sizeof(float));</P>
<P>memset(bias_bi,0,MAX_OUTPUTS*sizeof(float));</P>
<P> </P>
<P>} // end Reset_Net</P>
<P> </P>
<P>// MAIN //////////////////////////////////////////////////////////////////////////</P>
<P> </P>
<P>void main(void)</P>
<P>{</P>
<P>float FORCE_FP_LINK=(float)1.0; // needed for bug in VC++ fp lib link</P>
<P> </P>
<P>printf("\nHebbian Neural Network Simulator.\n");</P>
<P> </P>
<P>// querry user for parmaters of network</P>
<P> </P>
<P>printf("\nEnter number of inputs?");</P>
<P>scanf("%d",&num_inputs);</P>
<P> </P>
<P>printf("\nEnter number of Neurons (outputs)?");</P>
<P>scanf("%d",&num_outputs);</P>
<P> </P>
<P>printf("\nSelect Activation Function (Hebbian usually uses Step)\n0=Step, 1=Linear, 2=Exponential?");</P>
<P>scanf("%d",&activation_func);</P>
<P> </P>
<P>// test for exponential, get alpha is needed</P>
<P>if (activation_func == ACTF_EXP)</P>
<P>	{</P>
<P>	printf("\nEnter value for alpha (decimals allowed)?");</P>
<P>	scanf("%f",&alpha);</P>
<P>	} // end if</P>
<P> </P>
<P>// allocate weight matrix it is mxn where m is the number of inputs and n is the</P>
<P>// number of outputs</P>
<P>weight_matrix = new float[num_inputs*num_outputs];</P>
<P> </P>
<P>// clear out matrices</P>
<P>Reset_Net();</P>
<P> </P>
<P>// enter main event loop</P>
<P> </P>
<P>int	sel=0,</P>
<P>	done=0;</P>
<P> </P>
<P>while(!done)</P>
<P>	{</P>
<P>	printf("\nHebb Net Main Menu\n");</P>
<P>	printf("\n1. Input Training Vectors into Neural Net.");</P>
<P>	printf("\n2. Run Neural Net.");</P>
<P>	printf("\n3. Print Out Weight Matrix and Biases.");</P>
<P>	printf("\n4. Reset Weight Matrix and Biases.");</P>
<P>	printf("\n5. Exit Simulator.");</P>
<P>	printf("\n\nSelect One Please?");</P>
<P>	scanf("%d",&sel);</P>
<P> </P>
<P>	// what was the selection</P>
<P>	switch(sel)</P>
<P>		{</P>
<P>	</P>
<P>		case 1: // Input Training Vectors into Neural Net</P>
<P>			{</P>
<P>			Train_Net();</P>
<P>				</P>
<P>			} break;</P>
<P>	</P>
<P>		case 2: // Run Neural Net</P>
<P>			{</P>
<P>			Run_Net();</P>
<P>			} break;</P>
<P>	</P>
<P>		case 3: // Print Out Weight Matrix and Biases</P>
<P>			{</P>
<P>			Print_Net();</P>
<P>			} break;</P>
<P>	</P>
<P>		case 4: // Reset Weight Matrix and Biases</P>
<P>			{</P>
<P>			Reset_Net();</P>
<P>			} break;</P>
<P>	</P>
<P>		case 5: // Exit Simulator</P>
<P>			{</P>
<P>			// set exit flag</P>
<P>			done=1;</P>
<P> </P>
<P>			} break;</P>
<P>	</P>
<P>		default:break;</P>
<P> </P>
<P>		} // end swtich</P>
<P> </P>
<P>	} // end while</P>
<P> </P>
<P>// free up resources</P>
<P> </P>
<P>delete [] weight_matrix;</P>
<P> </P>
<P>} // end main</P>
</B></FONT><FONT SIZE=1><P> </P>
<P> </P>
</FONT><B><FONT SIZE=4><P>Playing the Hopfield </P>
</B></FONT><FONT SIZE=2><P> </P>
<B><P>Figure 10.0 - A 4 Node Hopfield Autoassociative Neural Net. </P>
</B><P><IMG SRC="Image13.gif" WIDTH=496 HEIGHT=350></P>
<P> </P>
<P>John Hopfield is a physicist that likes to play with neural nets (which is good for us). He came up with a simple (in structure at least), but effective neural network called the <B>Hopfield Net.</B> It is used for autoassociation, you input a vector <B>x</B> and you get <B>x</B> back (hopefully). A Hopfield net is shown in Figure 10.0. It is a single layer network with a number of neurodes equal to the number of inputs <B>X</B><SUB>i</SUB>. The network is fully connected meaning that every neurode is connected to every other neurode and the inputs are also the outputs. This should strike you as weird since there is <B>feedback</B>. Feedback is one of the key features of the Hopfield net and this feedback is the basis for the convergence to the correct result. </P>
<P> </P>
<P>The Hopfield network is an <B>iterative autoassociative memory.</B> This means that is may take one or more cycles to return the correct result (if at all). Let me clarify; the Hopfield network takes an input and then feeds it back, the resulting output may or may not be the desired input. This feedback cycle may occur a number of times before the input vector is returned. Hence, a Hopfield network functional sequence is: first we determine the weights based on our input vectors that we want to autoassociate, then we input a vector and see what comes out of the activations. If the result is the same as our original input then we are done, if not, then we take the result vector and feed it back through the network. Now let's take a look at the weight matrix and learning algorithm used for Hopfield nets. </P>
<P> </P>
<P>The learning algorithm for Hopfield nets is based on the Hebbian rule and is simply a summation of products. However, since the Hopfield network has a number of input neurons the weights are no longer a single array or vector, but a collection of vectors which are most compactly contained in a single matrix. Thus the weight matrix <B>W</B> for a Hopfield net is created based on this equation:</P>
<P> </P>
<B><P>Given:</P>
</B><P> </P>
<UL>
<LI>Inputs vectors are in bipolar form <B>I</B> = (-1,1,,...-1,1) and contain <B>k</B> 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>.</LI>
<LI>Outputs will be referred to as <B>y</B><SUB>j</SUB> and there are <B>k</B> of them, one for each input <B>I</B><SUB>j</SUB>.</LI>
<LI>The weight matrix <B>W</B> is square and has dimension <B>k</B>x<B>k</B> since there are <B>k</B> inputs.</LI></UL>
<P> </P>
<P> Eq. 8.0 	 <B>k</B>	</P>
<B><P>W</B> <SUB>(kxk)</SUB> = <FONT FACE="Symbol">å</FONT>
<B>I</B><SUB>i</SUB><SUP>t</SUP> x <B>I</B><SUB>i</SUB> </P>
<P> <B>i</B> = 1</P>
<P>note: each outer product will have dimension <B>k</B> x <B>k</B>, since we are multiplying a column vector and a row vector.</
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -