当前位置:文档之家› 神经网络C语言实现

神经网络C语言实现

神经网络C语言实现
神经网络C语言实现

#include "stdio.h"

#include

const double e = 2.7182818;

//设置一个神经网络

//有一个隐藏层(含有两个节点)

//输出层有一个节点

//输入数据是二维(两个节点)

//一个样本数据为:x = (0.35,0.9) 标签为0.5

//初始权值输入节点1到隐藏层:0.1,0.4

//输入节点2到隐藏层:0.8,0.6

//隐藏层到输出层初始权值为:0.3,0.9

//学习速率为1

double changeWeightFromHiddenToOutput(double cost,double output,double hiddenLayerCode) {

double result=0;

result = cost*output*(1-output)*hiddenLayerCode;

return result;

}

double changeWeightFromInputToHidden(double cost,double output,double weightOfHiddenCodeToOutput,double weightOfHiddenCode,double inputNum)

{

double result=0;

result = cost*output*(1-output)*weightOfHiddenCodeToOutput*weightOfHiddenCode*(1-weightOfHidd enCode)*inputNum;

return result;

}

double sigmoidFunction(double x)

{

double result=0;

result = 1/(1+pow(e,-x));

return result;

}

double costFunction(double originalSignal,double outputOfOurCalculation)

{

//此处采取的损失函数是最小二乘法

double cost=0;

cost = (1/2.0)*(originalSignal-outputOfOurCalculation)*(originalSignal-outputOfOurCalculation);

return cost;

}

double upDateWeightFunction(double originalValue,double gradient)

{

double updatedWeight=originalValue;

updatedWeight = updatedWeight - fabs(gradient);

return updatedWeight;

}

int main(void)

{

double weightFromInputToHidden[][2]={0.1,0.4,0.8,0.6};

double weightFromHiddenToOutput[]={0.3,0.9};

double firstHiddenCode,secondHiddenCode,outputCode;

double inputValue[] ={0.35,0.9};

double originalSignal = 0.5;

double cost=0;

double weightChangeNum=0;

double addWeightSum = 0;

firstHiddenCode = 0;

secondHiddenCode = 0;

outputCode = 0;

//前向传播

addWeightSum = weightFromInputToHidden[0][0]*inputValue[0] + weightFromInputToHidden[1][0]*inputValue[1];

firstHiddenCode = sigmoidFunction(addWeightSum);

addWeightSum = weightFromInputToHidden[0][1]*inputValue[0] + weightFromInputToHidden[1][1]*inputValue[1];

secondHiddenCode = sigmoidFunction(addWeightSum);

addWeightSum = weightFromHiddenToOutput[0]*firstHiddenCode + weightFromHiddenToOutput[1]*secondHiddenCode;

outputCode = sigmoidFunction(addWeightSum);

//计算误差

cost = costFunction(originalSignal,outputCode);

printf("\n\n(0)firNode:[%f] secNode:[%f] outNode:[%f] cost:%f",firstHiddenCode,secondHiddenCode,outputCode,cost);

printf("\n\n\t\t输入到隐藏层的权值:\t\t");

for(int i=0;i<2;i++)

{

printf("\n\t\t");

for(int j=0;j<2;j++)

printf(" %lf ",weightFromInputToHidden[i][j]);

}

printf("\n\n\t\t隐藏层到输出的权值:\n\t\t");

for(i=0;i<2;i++)

{

printf(" {%lf} ",weightFromHiddenToOutput[i]);

}

for(int iteration = 0;iteration<1;iteration++)

{

//更新隐藏层到输出层权值

//weightChangeNum为相应权值的梯度

weightChangeNum = changeWeightFromHiddenToOutput(cost,outputCode,firstHiddenCode);

weightFromHiddenToOutput[0] = upDateWeightFunction(weightFromHiddenToOutput[0],weightChangeNum);

weightChangeNum = changeWeightFromHiddenToOutput(cost,outputCode,secondHiddenCode);

weightFromHiddenToOutput[1] = upDateWeightFunction(weightFromHiddenToOutput[1],weightChangeNum);

//更新输入层到隐藏层的权值

weightChangeNum = changeWeightFromInputToHidden(cost,outputCode,weightFromHiddenToOutput[0],firstHiddenC ode,inputValue[0]);

weightFromInputToHidden[0][0] = upDateWeightFunction(weightFromInputToHidden[0][0],weightChangeNum);

weightChangeNum = changeWeightFromInputToHidden(cost,outputCode,weightFromHiddenToOutput[1],secondHidde nCode,inputValue[0]);

weightFromInputToHidden[0][1] = upDateWeightFunction(weightFromInputToHidden[0][1],weightChangeNum);

weightChangeNum = changeWeightFromInputToHidden(cost,outputCode,weightFromHiddenToOutput[0],firstHiddenC ode,inputValue[1]);

weightFromInputToHidden[1][0] = upDateWeightFunction(weightFromInputToHidden[1][0],weightChangeNum);

weightChangeNum = changeWeightFromInputToHidden(cost,outputCode,weightFromHiddenToOutput[1],secondHidde nCode,inputValue[1]);

weightFromInputToHidden[1][1] =

upDateWeightFunction(weightFromInputToHidden[1][1],weightChangeNum);

//再次进行前向传播

addWeightSum = weightFromInputToHidden[0][0]*inputValue[0] + weightFromInputToHidden[1][0]*inputValue[1];

firstHiddenCode = sigmoidFunction(addWeightSum);

addWeightSum = weightFromInputToHidden[0][1]*inputValue[0] + weightFromInputToHidden[1][1]*inputValue[1];

secondHiddenCode = sigmoidFunction(addWeightSum);

//输出

addWeightSum = weightFromHiddenToOutput[0]*firstHiddenCode + weightFromHiddenToOutput[1]*secondHiddenCode;

outputCode = sigmoidFunction(addWeightSum);

//计算误差

cost = costFunction(originalSignal,outputCode);

printf("\n\n(%d)firNode:[%f] secNode:[%f] outNode:[%f] cost:%f",iteration+1,firstHiddenCode,secondHiddenCode,outputCode,cost);

printf("\n\n\t\t输入到隐藏层的权值:\t\t");

for(int i=0;i<2;i++)

{

printf("\n\t\t");

for(int j=0;j<2;j++)

printf(" %lf ",weightFromInputToHidden[i][j]);

}

printf("\n\n\t\t隐藏层到输出的权值:\n\t\t");

for(i=0;i<2;i++)

{

printf(" {%lf} ",weightFromHiddenToOutput[i]);

}

if(cost<0.01)

{

printf("\n\n误差已经小于0.01了!停止^_^\n");

break;

}

}

printf("\n\n\t\t");

}

相关主题
文本预览
相关文档 最新文档