⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 neural.htm

📁 关于windows游戏编程的一些文章还有相关图形
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<P>&#9;&#9;output_yi[MAX_OUTPUTS],&#9;// holds the output values</P>
<P>&#9;&#9;bias_bi[MAX_OUTPUTS],&#9;// holds the bias weights bi</P>
<P>&#9;&#9;alpha = (float)1.0,&#9;&#9;// needed for exponential activation function</P>
<P>&#9;&#9;*weight_matrix = NULL;&#9;// dynamically allocated weight matrix</P>
<P>&#9;&#9;</P>
<P>int&#9;&#9;num_inputs,&#9;&#9;// number of inputs in heb net</P>
<P>&#9;&#9;num_outputs,&#9;&#9;// number of outputs in heb net</P>
<P>&#9;&#9;activation_func = ACTF_STEP;&#9;// type of activation function to use</P>
<P>&nbsp;</P>
<P>// FUNCTIONS /////////////////////////////////////////////////////////////////////</P>
<P>&nbsp;</P>
<P>void Train_Net(void)</P>
<P>{</P>
<P>// this function is resposible for training the neural net using hebbian learning</P>
<P>&nbsp;</P>
<P>// ask the user for another input/ouptput vector pair and then add the vectors contribution to</P>
<P>// the weight matrix and bias</P>
<P>&nbsp;</P>
<P>printf("\nHebbian Training System.");</P>
<P>printf("\nTo train neural net you will enter each input/output vector pair");</P>
<P>printf("\nan element at a time.");</P>
<P>&nbsp;</P>
<P>printf("\n\nInput vectors have %d components each and outputs have %d\n",num_inputs, num_outputs);</P>
<P>&nbsp;</P>
<P>while(1)</P>
<P>&#9;{</P>
<P>&#9;// get the input vector</P>
<P>&#9;printf("\nEnter input vector elements\n");</P>
<P>&nbsp;</P>
<P>&#9;for (int index=0; index&lt;num_inputs; index++)</P>
<P>&#9;&#9;{</P>
<P>&#9;&#9;printf("Input Vector Element[%d]=?",index);</P>
<P>&#9;&#9;scanf("%f",&amp;input_i[index]);</P>
<P>&#9;&#9;} // end for</P>
<P>&nbsp;</P>
<P>&#9;printf("\nNow enter associated output vector elements\n");</P>
<P>&nbsp;</P>
<P>&#9;// now get the output vector (note there might only be one neuron in this net</P>
<P>&nbsp;</P>
<P>&#9;for (index=0; index&lt;num_outputs; index++)</P>
<P>&#9;&#9;{</P>
<P>&#9;&#9;printf("Output Vector Element[%d]=?",index);</P>
<P>&#9;&#9;scanf("%f",&amp;output_i[index]);</P>
<P>&#9;&#9;} // end for</P>
<P>&nbsp;</P>
<P>&#9;// train the net with new vector, note we process one neuron at a time</P>
<P>&nbsp;</P>
<P>&#9;for (int index_j=0; index_j&lt;num_outputs; index_j++)</P>
<P>&#9;&#9;{</P>
<P>&#9;&#9;for (int index_i=0; index_i&lt;num_inputs; index_i++)</P>
<P>&#9;&#9;&#9;{</P>
<P>&#9;&#9;&#9;// hebb learning alg. wi=wi+input*ouput, b=b+output</P>
<P>&#9;&#9;&#9;</P>
<P>&#9;&#9;&#9;MAT(weight_matrix,num_outputs,index_i, index_j) += &#9;&#9;&#9;&#9;&#9;(input_i[index_i]*output_i[index_j]);</P>
<P>&#9;&#9;&#9;bias_bi[index_j] += output_i[index_i];</P>
<P>&nbsp;</P>
<P>&#9;&#9;&#9;} // end for index_i</P>
<P>&#9;&#9;} // end for index_j</P>
<P>&nbsp;</P>
<P>&#9;printf("\nDo you wish to enter another input/output pair Y or N?");</P>
<P>&#9;char ans[8];</P>
<P>&#9;scanf("%s",ans);</P>
<P>&#9;if (toupper(ans[0])!='Y')</P>
<P>&#9;&#9;break;</P>
<P>&nbsp;</P>
<P>&#9;} // end while</P>
<P>&nbsp;</P>
<P>} // end Train_Net</P>
<P>&nbsp;</P>
<P>//////////////////////////////////////////////////////////////////////////////////</P>
<P>&nbsp;</P>
<P>void Run_Net(void)</P>
<P>{</P>
<P>// this function is responsible for running the net, it allows the user to enter test</P>
<P>// vectors and then computes the response of the network</P>
<P>&nbsp;</P>
<P>printf("\nNetwork Simulation System.");</P>
<P>printf("\nYou will enter in test input vectors and the input will be processed by the net.");</P>
<P>printf("\nAll inputs must have %d elements\n",num_inputs);</P>
<P>&nbsp;</P>
<P>while(1)</P>
<P>&#9;{</P>
<P>&#9;// get the input vector</P>
<P>&#9;printf("\nEnter input vector elements\n");</P>
<P>&nbsp;</P>
<P>&#9;for (int index=0; index&lt;num_inputs; index++)</P>
<P>&#9;&#9;{</P>
<P>&#9;&#9;printf("Input Vector Element[%d]=?",index);</P>
<P>&#9;&#9;scanf("%f",&amp;input_i[index]);</P>
<P>&#9;&#9;} // end for</P>
<P>&nbsp;</P>
<P>&#9;// now process the input by performing a matrix mutiply</P>
<P>&#9;// each weight vector is stored as a column in the weight matrix, so to process</P>
<P>&#9;// the input for each neurode, we simply must perform a dot product, and then input</P>
<P>&#9;// the result to the activation function, this is the basis of the parallel </P>
<P>&#9;// processing a neural net performs, all outputs are independent of the others</P>
<P>&nbsp;</P>
<P>&#9;// loop thru the columns (outputs, neurodes)</P>
<P>&#9;for (int index_j=0; index_j&lt;num_outputs; index_j++)</P>
<P>&#9;&#9;{</P>
<P>&#9;&#9;// now compute a dot product with the input vector and the column</P>
<P>&nbsp;</P>
<P>&#9;&#9;input_act[index_j] = (float)0.0; // reset activation</P>
<P>&nbsp;</P>
<P>&#9;&#9;for (int index_i=0; index_i&lt;num_inputs; index_i++)</P>
<P>&#9;&#9;&#9;{</P>
<P>&#9;&#9;&#9;input_act[index_j] = input_act[index_j] + </P>
<P>&#9;&#9;&#9;&#9;(MAT(weight_matrix,num_outputs,index_i, index_j) * input_i[index_i]);</P>
<P>&#9;&#9;&#9;} // end for index_i</P>
<P>&#9;&#9;</P>
<P>&#9;&#9;// add in bias term</P>
<P>&#9;&#9;input_act[index_j] = input_act[index_j] + bias_bi[index_j];</P>
<P>&nbsp;</P>
<P>&#9;&#9;// now compute output based on activation function</P>
<P>&nbsp;</P>
<P>&#9;&#9;if (activation_func==ACTF_STEP)</P>
<P>&#9;&#9;&#9;{</P>
<P>&#9;&#9;&#9;// perform step activation</P>
<P>&#9;&#9;&#9;if (input_act[index_j]&gt;=(float)0.0)</P>
<P>&#9;&#9;&#9;&#9;output_yi[index_j] = (float)1.0;</P>
<P>&#9;&#9;&#9;else</P>
<P>&#9;&#9;&#9;&#9;output_yi[index_j] = (float)-1.0;</P>
<P>&nbsp;</P>
<P>&#9;&#9;&#9;} // end if</P>
<P>&#9;&#9;else</P>
<P>&#9;&#9;if (activation_func==ACTF_LINEAR)</P>
<P>&#9;&#9;&#9;{</P>
<P>&#9;&#9;&#9;// perform linear activation</P>
<P>&#9;&#9;&#9;output_yi[index_j] = input_act[index_j];</P>
<P>&#9;&#9;&#9;}</P>
<P>&#9;&#9;else</P>
<P>&#9;&#9;&#9;{</P>
<P>&#9;&#9;&#9;// must be exponential activation</P>
<P>&#9;&#9;&#9;output_yi[index_j] =(float)(1/(1+exp(-input_act[index_j]*alpha)));</P>
<P>&#9;</P>
<P>&#9;&#9;&#9;} // end else exp</P>
<P>&nbsp;</P>
<P>&#9;&#9;} // end for index_j</P>
<P>&nbsp;</P>
<P>&#9;// now that ouputs have been computed print everything out</P>
<P>&nbsp;</P>
<P>&#9;printf("\nNet inputs were:\n[");</P>
<P>&#9;for (index_j=0; index_j&lt;num_outputs; index_j++)</P>
<P>&#9;&#9;printf("%2.2f, ",input_act[index_j]);</P>
<P>&#9;printf("]\n");</P>
<P>&#9;</P>
<P>&#9;printf("\nFinal Outputs after activation functions are:\n[");</P>
<P>&#9;for (index_j=0; index_j&lt;num_outputs; index_j++)</P>
<P>&#9;&#9;printf("%2.2f, ",output_yi[index_j]);</P>
<P>&#9;printf("]\n");</P>
<P>&nbsp;</P>
<P>&#9;printf("\nDo you wish to enter another test input Y or N?");</P>
<P>&#9;char ans[8];</P>
<P>&#9;scanf("%s",ans);</P>
<P>&#9;if (toupper(ans[0])!='Y')</P>
<P>&#9;&#9;break;</P>
<P> </P>
<P>&#9;} // end while</P>
<P>&nbsp;</P>
<P>} // end Run_Net</P>
<P>&nbsp;</P>
<P>//////////////////////////////////////////////////////////////////////////////////</P>
<P>&nbsp;</P>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -