神经网络C语言实现

#include \#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-weightOfHiddenCode)*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(\secNode:[%f] outNode:[%f] cost:%f\ printf(\输入到隐藏层的权值:\\t\\t\ for(int i=0;i<2;i++) { printf(\ for(int j=0;j<2;j++) printf(\

}

printf(\隐藏层到输出的权值:\\n\\t\\t\for(i=0;i<2;i++) { printf(\}

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],firstHiddenCode,inputValue[0]); weightFromInputToHidden[0][0] = upDateWeightFunction(weightFromInputToHidden[0][0],weightChangeNum); weightChangeNum = changeWeightFromInputToHidden(cost,outputCode,weightFromHiddenToOutput[1],secondHiddenCode,inputValue[0]); weightFromInputToHidden[0][1] = upDateWeightFunction(weightFromInputToHidden[0][1],weightChangeNum); weightChangeNum = changeWeightFromInputToHidden(cost,outputCode,weightFromHiddenToOutput[0],firstHiddenCode,inputValue[1]); weightFromInputToHidden[1][0] = upDateWeightFunction(weightFromInputToHidden[1][0],weightChangeNum); weightChangeNum = changeWeightFromInputToHidden(cost,outputCode,weightFromHiddenToOutput[1],secondHiddenCode,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(\secNode:[%f]

cost:%f\ printf(\输入到隐藏层的权值:\\t\\t\ for(int i=0;i<2;i++) { printf(\ for(int j=0;j<2;j++) printf(\ } printf(\隐藏层到输出的权值:\\n\\t\\t\ for(i=0;i<2;i++) { printf(\ } if(cost<0.01) { printf(\误差已经小于0.01了!停止^_^\\n\ break; } } printf(\}

+

+

+

outNode:[%f]

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4