神经网络C语言实现
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
firstHiddenCode = sigmoidFunction(addWeightSum);
addWeightSum = weightFromInputToHidden[0][1]*inputValue[0] + weightFromInputToHidden[1][1]*inputValue[1];
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];
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");
weightFromInputToHidden[0][0] = upDateWeightFunction(weightFromInputToHidden[0][0],weightChangeNum);
weightChangeNum = changeWeightFromInputToHidden(cost,outputCode,weightFromHiddenToOutput[1],secondHiddenCode,inputValue[0]);
weightFromHiddenToOutput[1] = upDateWeightFunction(weightFromHiddenToOutput[1],weightChangeNum);
//更新输入层到隐藏层的权值
weightChangeNum = changeWeightFromInputToHidden(cost,outputCode,weightFromHiddenToOutput[0],firstHiddenCode,inputValue[0]);
//输入节点2到隐藏层:0.8,0.6
//隐藏层到输出层初始权值为:0.3,0.9
//学习速率为1
double changeWeightFromHiddenToOutput(double cost,double output,double hiddenLayerCode)
{
double result=0;
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);
firstHiddenCode = sigmoidFunction(addWeightSum);
addWeightSum = weightFromInputToHidden[0][1]*inputValue[0] + weightFromInputToHidden[1][1]*inputValue[1];
{
double result=0;
result = cost*output*(1-output)*weightOfHiddenCodeToOutput*weightOfHiddenCode*(1-weightOfHiddenCode)*inputNum;
return result;
}
double sigmoidFunction(double x)
#include "stdio.h"
#include <math.h>
const double e = 2.7182818;
//设置一个神经网络
//有一个隐藏层(含有两个节点)
//输出层有一个节点
//输入数据是二维(两个节点)
//一个样本数据为:x = (0.35,0.9)标签为0.5
//初始权值输入节点1到隐藏层:0.1,0.4
weightFromInputToHidden[0][1] = upDateWeightFunction(weightFromInputToHidden[0][1],weightChangeNum);
weightChangeNum = changeWeightFromInputToHidden(cost,outputCode,weightFromHiddenToOutput[0],firstHiddenCode,inputValue[1]);
result = cost*output*(1-output)*hiddenLayerCode;
return result;
}
double changeWeightFromInputToHidden(double cost,double output,double weightOfHiddenCodeToOutput,double weightOfHiddenCode,double inputNum)
{
double updatedWeight=originalValue;
updatedWeight = updatedWeight - fabs(gradient);
return updatedWeight;
}
int main(void)
{
double weightFromInputToHidden[][2]={0.1,0.4,0.8,0.6};
secondHiddenCode = sigmoidFunction(addWeightSum);
//输出
addWeightSum = weightFromHiddenToOutput[0]*firstHiddenCode + weightFromHiddenToOutput[1]*secondHiddenCode;
double weightFromHiddenToOutput[]={0.3,0.9};
double firstHiddenCode,secondHiddenCode,outputCode;
double inputValue[] ={0.35,0.9};
double originalSignal = 0.5;
secondHiddenCode = sigmoidFunction(addWeightSum);
addWeightSum = weightFromHiddenToOutput[0]*firstHiddenCode + weightFromHiddenToOutput[1]*secondHiddenCode;
cost = (1/2.0)*(originalSignal-outputOfOurCalculation)*(originalSignal-outputOfOurCalculation);
return cost;
}
double upDateWeightFunction(double originalValue,double gradient)
weightFromHiddenToOutput[0] = upDateWeightFunction(weightFromHiddenToOutput[0],weightChangeNum);
weightChangeNum = changeWeightFromHiddenToOutput(cost,outputCode,secondHiddenCode);
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];
for(i=0;i<2;i++)
{
printf(" {%lf} ",weightFromHiddenToOutput[i]);
}
for(int iteration = 0;iteration<1;iteration++)
{
//更新隐藏层到输出层权值
//weightChangeNum为相应权值的梯度
weightChangeNum = changeWeightFromHiddenToOutput(cost,outputCode,firstHiddenCode);
outputCode = sigmoidFunction(addWeightSum);
//计算误差
cost = costFunction(originalSignal,outputCode);
printf("\n\n(0)firNode:[%f] secNode:[%f] outNode:[%f] cost:%f",firstHiddenCode,secondHiddenCode,outputCode,cost);
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");
}
{
double result=0;
result = 1/(1+pow(e,-x));
return result;
}
double costFunction(double originalSignal,double outputOfOurCalculation)
{
//此处采取的损失函数是最小二乘法
double cost=0;
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");
addWeightSum = weightFromInputToHidden[0][1]*inputValue[0] + weightFromInputToHidden[1][1]*inputValue[1];
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];
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");
weightFromInputToHidden[0][0] = upDateWeightFunction(weightFromInputToHidden[0][0],weightChangeNum);
weightChangeNum = changeWeightFromInputToHidden(cost,outputCode,weightFromHiddenToOutput[1],secondHiddenCode,inputValue[0]);
weightFromHiddenToOutput[1] = upDateWeightFunction(weightFromHiddenToOutput[1],weightChangeNum);
//更新输入层到隐藏层的权值
weightChangeNum = changeWeightFromInputToHidden(cost,outputCode,weightFromHiddenToOutput[0],firstHiddenCode,inputValue[0]);
//输入节点2到隐藏层:0.8,0.6
//隐藏层到输出层初始权值为:0.3,0.9
//学习速率为1
double changeWeightFromHiddenToOutput(double cost,double output,double hiddenLayerCode)
{
double result=0;
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);
firstHiddenCode = sigmoidFunction(addWeightSum);
addWeightSum = weightFromInputToHidden[0][1]*inputValue[0] + weightFromInputToHidden[1][1]*inputValue[1];
{
double result=0;
result = cost*output*(1-output)*weightOfHiddenCodeToOutput*weightOfHiddenCode*(1-weightOfHiddenCode)*inputNum;
return result;
}
double sigmoidFunction(double x)
#include "stdio.h"
#include <math.h>
const double e = 2.7182818;
//设置一个神经网络
//有一个隐藏层(含有两个节点)
//输出层有一个节点
//输入数据是二维(两个节点)
//一个样本数据为:x = (0.35,0.9)标签为0.5
//初始权值输入节点1到隐藏层:0.1,0.4
weightFromInputToHidden[0][1] = upDateWeightFunction(weightFromInputToHidden[0][1],weightChangeNum);
weightChangeNum = changeWeightFromInputToHidden(cost,outputCode,weightFromHiddenToOutput[0],firstHiddenCode,inputValue[1]);
result = cost*output*(1-output)*hiddenLayerCode;
return result;
}
double changeWeightFromInputToHidden(double cost,double output,double weightOfHiddenCodeToOutput,double weightOfHiddenCode,double inputNum)
{
double updatedWeight=originalValue;
updatedWeight = updatedWeight - fabs(gradient);
return updatedWeight;
}
int main(void)
{
double weightFromInputToHidden[][2]={0.1,0.4,0.8,0.6};
secondHiddenCode = sigmoidFunction(addWeightSum);
//输出
addWeightSum = weightFromHiddenToOutput[0]*firstHiddenCode + weightFromHiddenToOutput[1]*secondHiddenCode;
double weightFromHiddenToOutput[]={0.3,0.9};
double firstHiddenCode,secondHiddenCode,outputCode;
double inputValue[] ={0.35,0.9};
double originalSignal = 0.5;
secondHiddenCode = sigmoidFunction(addWeightSum);
addWeightSum = weightFromHiddenToOutput[0]*firstHiddenCode + weightFromHiddenToOutput[1]*secondHiddenCode;
cost = (1/2.0)*(originalSignal-outputOfOurCalculation)*(originalSignal-outputOfOurCalculation);
return cost;
}
double upDateWeightFunction(double originalValue,double gradient)
weightFromHiddenToOutput[0] = upDateWeightFunction(weightFromHiddenToOutput[0],weightChangeNum);
weightChangeNum = changeWeightFromHiddenToOutput(cost,outputCode,secondHiddenCode);
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];
for(i=0;i<2;i++)
{
printf(" {%lf} ",weightFromHiddenToOutput[i]);
}
for(int iteration = 0;iteration<1;iteration++)
{
//更新隐藏层到输出层权值
//weightChangeNum为相应权值的梯度
weightChangeNum = changeWeightFromHiddenToOutput(cost,outputCode,firstHiddenCode);
outputCode = sigmoidFunction(addWeightSum);
//计算误差
cost = costFunction(originalSignal,outputCode);
printf("\n\n(0)firNode:[%f] secNode:[%f] outNode:[%f] cost:%f",firstHiddenCode,secondHiddenCode,outputCode,cost);
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");
}
{
double result=0;
result = 1/(1+pow(e,-x));
return result;
}
double costFunction(double originalSignal,double outputOfOurCalculation)
{
//此处采取的损失函数是最小二乘法
double cost=0;
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");