神经网络C语言实现(共4页)_第1页
神经网络C语言实现(共4页)_第2页
神经网络C语言实现(共4页)_第3页
神经网络C语言实现(共4页)_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论