



下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 马鞍山城市道路施工方案
- 简单的护理课件
- 巴彦淖尔节能地坪施工方案
- 惠州食品无尘车间施工方案
- 湖北经济学院法商学院《液压与气压传动》2023-2024学年第二学期期末试卷
- 四川信息职业技术学院《中华礼乐文明》2023-2024学年第二学期期末试卷
- 淮北师范大学《人居环境学》2023-2024学年第二学期期末试卷
- 江苏工程职业技术学院《工程制图(上)》2023-2024学年第二学期期末试卷
- 兰州石化职业技术大学《市场营销与策划》2023-2024学年第二学期期末试卷
- 浙江农林大学《三维电脑动画》2023-2024学年第二学期期末试卷
- ESAP法律英语教程全册配套优质教学课件
- 水资源保护知识竞赛试题及答案
- 道路清扫保洁-组织机构框架图、内部分工
- PCB制程涨缩系数操作指引
- 标准 DB37T 3690.1-2019 液体菌种制备技术规程 第1部分:香菇规范
- 2021五年级道德与法治培优辅差计划3篇
- 静脉药物配置中心课件
- 最新2022年减肥食品市场现状与发展趋势预测
- 发展汉语初级综合1:第30课PPT课件[通用]
- 马工程西方经济学(第二版)教学课件-(4)
- 医疗废物管理组织机构架构图
评论
0/150
提交评论