📄 neural.htm
字号:
<P>		output_yi[MAX_OUTPUTS],	// holds the output values</P>
<P>		bias_bi[MAX_OUTPUTS],	// holds the bias weights bi</P>
<P>		alpha = (float)1.0,		// needed for exponential activation function</P>
<P>		*weight_matrix = NULL;	// dynamically allocated weight matrix</P>
<P>		</P>
<P>int		num_inputs,		// number of inputs in heb net</P>
<P>		num_outputs,		// number of outputs in heb net</P>
<P>		activation_func = ACTF_STEP;	// type of activation function to use</P>
<P> </P>
<P>// FUNCTIONS /////////////////////////////////////////////////////////////////////</P>
<P> </P>
<P>void Train_Net(void)</P>
<P>{</P>
<P>// this function is resposible for training the neural net using hebbian learning</P>
<P> </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> </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> </P>
<P>printf("\n\nInput vectors have %d components each and outputs have %d\n",num_inputs, num_outputs);</P>
<P> </P>
<P>while(1)</P>
<P>	{</P>
<P>	// get the input vector</P>
<P>	printf("\nEnter input vector elements\n");</P>
<P> </P>
<P>	for (int index=0; index<num_inputs; index++)</P>
<P>		{</P>
<P>		printf("Input Vector Element[%d]=?",index);</P>
<P>		scanf("%f",&input_i[index]);</P>
<P>		} // end for</P>
<P> </P>
<P>	printf("\nNow enter associated output vector elements\n");</P>
<P> </P>
<P>	// now get the output vector (note there might only be one neuron in this net</P>
<P> </P>
<P>	for (index=0; index<num_outputs; index++)</P>
<P>		{</P>
<P>		printf("Output Vector Element[%d]=?",index);</P>
<P>		scanf("%f",&output_i[index]);</P>
<P>		} // end for</P>
<P> </P>
<P>	// train the net with new vector, note we process one neuron at a time</P>
<P> </P>
<P>	for (int index_j=0; index_j<num_outputs; index_j++)</P>
<P>		{</P>
<P>		for (int index_i=0; index_i<num_inputs; index_i++)</P>
<P>			{</P>
<P>			// hebb learning alg. wi=wi+input*ouput, b=b+output</P>
<P>			</P>
<P>			MAT(weight_matrix,num_outputs,index_i, index_j) += 					(input_i[index_i]*output_i[index_j]);</P>
<P>			bias_bi[index_j] += output_i[index_i];</P>
<P> </P>
<P>			} // end for index_i</P>
<P>		} // end for index_j</P>
<P> </P>
<P>	printf("\nDo you wish to enter another input/output pair Y or N?");</P>
<P>	char ans[8];</P>
<P>	scanf("%s",ans);</P>
<P>	if (toupper(ans[0])!='Y')</P>
<P>		break;</P>
<P> </P>
<P>	} // end while</P>
<P> </P>
<P>} // end Train_Net</P>
<P> </P>
<P>//////////////////////////////////////////////////////////////////////////////////</P>
<P> </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> </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> </P>
<P>while(1)</P>
<P>	{</P>
<P>	// get the input vector</P>
<P>	printf("\nEnter input vector elements\n");</P>
<P> </P>
<P>	for (int index=0; index<num_inputs; index++)</P>
<P>		{</P>
<P>		printf("Input Vector Element[%d]=?",index);</P>
<P>		scanf("%f",&input_i[index]);</P>
<P>		} // end for</P>
<P> </P>
<P>	// now process the input by performing a matrix mutiply</P>
<P>	// each weight vector is stored as a column in the weight matrix, so to process</P>
<P>	// the input for each neurode, we simply must perform a dot product, and then input</P>
<P>	// the result to the activation function, this is the basis of the parallel </P>
<P>	// processing a neural net performs, all outputs are independent of the others</P>
<P> </P>
<P>	// loop thru the columns (outputs, neurodes)</P>
<P>	for (int index_j=0; index_j<num_outputs; index_j++)</P>
<P>		{</P>
<P>		// now compute a dot product with the input vector and the column</P>
<P> </P>
<P>		input_act[index_j] = (float)0.0; // reset activation</P>
<P> </P>
<P>		for (int index_i=0; index_i<num_inputs; index_i++)</P>
<P>			{</P>
<P>			input_act[index_j] = input_act[index_j] + </P>
<P>				(MAT(weight_matrix,num_outputs,index_i, index_j) * input_i[index_i]);</P>
<P>			} // end for index_i</P>
<P>		</P>
<P>		// add in bias term</P>
<P>		input_act[index_j] = input_act[index_j] + bias_bi[index_j];</P>
<P> </P>
<P>		// now compute output based on activation function</P>
<P> </P>
<P>		if (activation_func==ACTF_STEP)</P>
<P>			{</P>
<P>			// perform step activation</P>
<P>			if (input_act[index_j]>=(float)0.0)</P>
<P>				output_yi[index_j] = (float)1.0;</P>
<P>			else</P>
<P>				output_yi[index_j] = (float)-1.0;</P>
<P> </P>
<P>			} // end if</P>
<P>		else</P>
<P>		if (activation_func==ACTF_LINEAR)</P>
<P>			{</P>
<P>			// perform linear activation</P>
<P>			output_yi[index_j] = input_act[index_j];</P>
<P>			}</P>
<P>		else</P>
<P>			{</P>
<P>			// must be exponential activation</P>
<P>			output_yi[index_j] =(float)(1/(1+exp(-input_act[index_j]*alpha)));</P>
<P>	</P>
<P>			} // end else exp</P>
<P> </P>
<P>		} // end for index_j</P>
<P> </P>
<P>	// now that ouputs have been computed print everything out</P>
<P> </P>
<P>	printf("\nNet inputs were:\n[");</P>
<P>	for (index_j=0; index_j<num_outputs; index_j++)</P>
<P>		printf("%2.2f, ",input_act[index_j]);</P>
<P>	printf("]\n");</P>
<P>	</P>
<P>	printf("\nFinal Outputs after activation functions are:\n[");</P>
<P>	for (index_j=0; index_j<num_outputs; index_j++)</P>
<P>		printf("%2.2f, ",output_yi[index_j]);</P>
<P>	printf("]\n");</P>
<P> </P>
<P>	printf("\nDo you wish to enter another test input Y or N?");</P>
<P>	char ans[8];</P>
<P>	scanf("%s",ans);</P>
<P>	if (toupper(ans[0])!='Y')</P>
<P>		break;</P>
<P> </P>
<P>	} // end while</P>
<P> </P>
<P>} // end Run_Net</P>
<P> </P>
<P>//////////////////////////////////////////////////////////////////////////////////</P>
<P> </P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -