下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、精选优质文档-倾情为你奉上#include "stdio.h"#include <math.h>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
2、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)*weightOfHiddenCodeT
3、oOutput*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-outputOf
4、OurCalculation)*(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;do
5、uble 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 = weightFromInputT
6、oHidden00*inputValue0 + weightFromInputToHidden10*inputValue1; firstHiddenCode = sigmoidFunction(addWeightSum);addWeightSum = weightFromInputToHidden01*inputValue0 + weightFromInputToHidden11*inputValue1;secondHiddenCode = sigmoidFunction(addWeightSum);addWeightSum = weightFromHiddenToOutput0*firstH
7、iddenCode + 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"
8、;);for(int i=0;i<2;i+)printf("ntt");for(int j=0;j<2;j+)printf(" %lf ",weightFromInputToHiddenij);printf("nntt隐藏层到输出的权值:ntt");for(i=0;i<2;i+)printf(" %lf ",weightFromHiddenToOutputi);for(int iteration = 0;iteration<1;iteration+)/更新隐藏层到输出层权值/weightCh
9、angeNum为相应权值的梯度weightChangeNum = changeWeightFromHiddenToOutput(cost,outputCode,firstHiddenCode);weightFromHiddenToOutput0 = upDateWeightFunction(weightFromHiddenToOutput0,weightChangeNum);weightChangeNum = changeWeightFromHiddenToOutput(cost,outputCode,secondHiddenCode);weightFromHiddenToOutput1 =
10、upDateWeightFunction(weightFromHiddenToOutput1,weightChangeNum);/更新输入层到隐藏层的权值weightChangeNum = changeWeightFromInputToHidden(cost,outputCode,weightFromHiddenToOutput0,firstHiddenCode,inputValue0);weightFromInputToHidden00 = upDateWeightFunction(weightFromInputToHidden00,weightChangeNum);weightChange
11、Num = changeWeightFromInputToHidden(cost,outputCode,weightFromHiddenToOutput1,secondHiddenCode,inputValue0);weightFromInputToHidden01 = upDateWeightFunction(weightFromInputToHidden01,weightChangeNum);weightChangeNum = changeWeightFromInputToHidden(cost,outputCode,weightFromHiddenToOutput0,firstHidde
12、nCode,inputValue1);weightFromInputToHidden10 = upDateWeightFunction(weightFromInputToHidden10,weightChangeNum);weightChangeNum = changeWeightFromInputToHidden(cost,outputCode,weightFromHiddenToOutput1,secondHiddenCode,inputValue1);weightFromInputToHidden11 = upDateWeightFunction(weightFromInputToHid
13、den11,weightChangeNum);/再次进行前向传播addWeightSum = weightFromInputToHidden00*inputValue0 + weightFromInputToHidden10*inputValue1; firstHiddenCode = sigmoidFunction(addWeightSum);addWeightSum = weightFromInputToHidden01*inputValue0 + weightFromInputToHidden11*inputValue1;secondHiddenCode = sigmoidFunctio
14、n(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 i=0;i<2;i+)printf("ntt");for(int j=0;j<2;j+)printf(" %lf ",
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 投资顾问面试考核题及答案详解
- 特殊群体急救资源可及性提升方案
- 深度解析(2026)《GBT 18932.10-2002蜂蜜中溴螨酯、44-二溴二苯甲酮残留量的测定方法 气相色谱质谱法》
- 生产项目管理经理的招聘面试题集
- 劳务输出项目可行性分析报告范文(总投资13000万元)
- 教育顾问面试题集及应对策略
- 深度解析(2026)《GBT 9002-2017音频、视频和视听设备及系统词汇》
- 京东物流策划部面试题及策略性答案
- 会计事务所审计师面试问题及答案
- 关于华能集团对副总经理的考核制度分析
- JT-T-961-2020交通运输行业反恐怖防范基本要求
- MOOC 物理与艺术-南京航空航天大学 中国大学慕课答案
- 银行案件复盘分析报告
- 分析方法转移方案课件
- 无创呼吸机面部压疮预防措施
- 全国高校黄大年式教师团队推荐汇总表
- 员工管理规章制度实施细则
- 社会心理学(西安交通大学)知到章节答案智慧树2023年
- 《安井食品价值链成本控制研究案例(论文)9000字》
- GB/T 4135-2016银锭
- GB/T 33084-2016大型合金结构钢锻件技术条件
评论
0/150
提交评论