神经网络C语言实现(共4页).docx
《神经网络C语言实现(共4页).docx》由会员分享,可在线阅读,更多相关《神经网络C语言实现(共4页).docx(4页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精选优质文档-倾情为你奉上#include stdio.h#include const double e = 2.;/设置一个神经网络/有一个隐藏层(含有两个节点)/输出层有一个节点/输入数据是二维(两个节点)/一个样本数据为:x = (0.35,0.9) 标签为0.5/初始权值输入节点1到隐藏层:0.1,0.4/输入节点2到隐藏层:0.8,0.6/隐藏层到输出层初始权值为:0.3,0.9/学习速率为1double changeWeightFromHiddenToOutput(double cost,double output,double hiddenLayerCode)double res
2、ult=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
3、*(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)*(originalS
4、ignal-outputOfOurCalculation);return cost;double upDateWeightFunction(double originalValue,double gradient)double updatedWeight=originalValue;updatedWeight = updatedWeight - fabs(gradient);return updatedWeight;int main(void)double weightFromInputToHidden2=0.1,0.4,0.8,0.6;double weightFromHiddenToOut
5、put=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 = weightFromInputToHidden00*inputValue0 + we
6、ightFromInputToHidden10*inputValue1; firstHiddenCode = sigmoidFunction(addWeightSum);addWeightSum = weightFromInputToHidden01*inputValue0 + weightFromInputToHidden11*inputValue1;secondHiddenCode = sigmoidFunction(addWeightSum);addWeightSum = weightFromHiddenToOutput0*firstHiddenCode + weightFromHidd
7、enToOutput1*secondHiddenCode; outputCode = sigmoidFunction(addWeightSum);/计算误差cost = costFunction(originalSignal,outputCode);printf(nn(0)firNode:%f secNode:%f outNode:%f cost:%f,firstHiddenCode,secondHiddenCode,outputCode,cost);printf(nntt输入到隐藏层的权值:tt);for(int i=0;i2;i+)printf(ntt);for(int j=0;j2;j+
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 神经网络 语言 实现
限制150内