



全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
#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/学习速率为1double 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 weightFromInputToHidden2=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 = weightFromInputToHidden00*inputValue0 + weightFromInputToHidden10*inputValue1; firstHiddenCode = sigmoidFunction(addWeightSum);addWeightSum = weightFromInputToHidden01*inputValue0 + weightFromInputToHidden11*inputValue1;secondHiddenCode = sigmoidFunction(addWeightSum);addWeightSum = weightFromHiddenToOutput0*firstHiddenCode + weightFromHiddenToOutput1*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+)printf( %lf ,weightFromInputToHiddenij);printf(nntt隐藏层到输出的权值:ntt);for(i=0;i2;i+)printf( %lf ,weightFromHiddenToOutputi);for(int iteration = 0;iteration1;iteration+)/更新隐藏层到输出层权值/weightChangeNum为相应权值的梯度weightChangeNum = changeWeightFromHiddenToOutput(cost,outputCode,firstHiddenCode);weightFromHiddenToOutput0 = upDateWeightFunction(weightFromHiddenToOutput0,weightChangeNum);weightChangeNum = changeWeightFromHiddenToOutput(cost,outputCode,secondHiddenCode);weightFromHiddenToOutput1 = upDateWeightFunction(weightFromHiddenToOutput1,weightChangeNum);/更新输入层到隐藏层的权值weightChangeNum = changeWeightFromInputToHidden(cost,outputCode,weightFromHiddenToOutput0,firstHiddenCode,inputValue0);weightFromInputToHidden00 = upDateWeightFunction(weightFromInputToHidden00,weightChangeNum);weightChangeNum = changeWeightFromInputToHidden(cost,outputCode,weightFromHiddenToOutput1,secondHiddenCode,inputValue0);weightFromInputToHidden01 = upDateWeightFunction(weightFromInputToHidden01,weightChangeNum);weightChangeNum = changeWeightFromInputToHidden(cost,outputCode,weightFromHiddenToOutput0,firstHiddenCode,inputValue1);weightFromInputToHidden10 = upDateWeightFunction(weightFromInputToHidden10,weightChangeNum);weightChangeNum = changeWeightFromInputToHidden(cost,outputCode,weightFromHiddenToOutput1,secondHiddenCode,inputValue1);weightFromInputToHidden11 = upDateWeightFunction(weightFromInputToHidden11,weightChangeNum);/再次进行前向传播addWeightSum = weightFromInputToHidden00*inputValue0 + weightFromInputToHidden10*inputValue1; firstHiddenCode = sigmoidFunction(addWeightSum);addWeightSum = weightFromInputToHidden01*inputValue0 + weightFromInputToHidden11*inputValue1;secondHiddenCode = sigmoidFunction(addWeightSum);/输出addWeightSum = weightFromHiddenToOutput0*firstHiddenCode + weightFromHiddenToOutput1*secondHiddenCode; outputCode = sigmoidFunction(addWeightSum);/计算误差cost = costFunction(originalSignal,outputCode);printf(nn(%d)firNode:%f secNode:%f outNode:%f cost:%f,iteration+1,firstHiddenCode,secondHiddenCode,outputCode,cost);printf(nntt输入到隐藏层的权值:tt);for(int
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 廊坊市人民医院治疗质量控制考核
- 2025儿童医院运动平板试验资格认证
- 中国高纯氢项目创业计划书
- 2025年垃圾处理项目申请报告范文
- 2025年中国水性蜡乳液项目创业投资方案
- 中国动力软包电池项目经营分析报告
- 2025年中国凝胶剂项目商业计划书
- 2025年石油钻探、开采专用设备项目可行性研究方案
- 中国脱叶灵项目经营分析报告
- 2025年中国餐桌行业市场规模及投资前景预测分析报告
- 职业红蓝百家玩家深度剖析五局八星
- CJ/T 123-2004给水用钢骨架聚乙烯塑料复合管
- 正规超市劳务合同协议书
- T/IAC 19.2-2018汽车后市场用配件合车规范第2部分:车辆外部照明和光信号装置
- T/CECS 10210-2022给水用胶圈电熔双密封聚乙烯复合管材及管件
- T/CCASC 2003-2023环氧氯丙烷副产氯化钙应用技术要求
- 极端天气下的安全管理-洞察阐释
- 中药产业园可行性研究报告
- 工贸企业重大事故隐患判定标准解读
- 国家能源集团陆上风电项目通 用造价指标(2024年)
- 考18个文言虚词用法
评论
0/150
提交评论