实验四 BP神经网络模拟sin函数.doc_第1页
实验四 BP神经网络模拟sin函数.doc_第2页
实验四 BP神经网络模拟sin函数.doc_第3页
实验四 BP神经网络模拟sin函数.doc_第4页
实验四 BP神经网络模拟sin函数.doc_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

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

文档简介

实验四 BP神经网络模拟sin函数一、 问题描述BP神经网络模拟sin函数根据人工神经网络学习算法,实现正弦曲线的拟合。要求能随机自动生成数量为n的正弦值作为训练集,并根据训练集拟合曲线,最后能通过输入x值测试理论与拟合sinx的拟合度。二、 设计思想思维学普遍认为,人类大脑的思维分为抽象(逻辑)思维、形象(直观)思维和灵感(顿悟)思维三种基本方式。逻辑性的思维是指根据逻辑规则进行推理的过程;它先将信息化成概念,并用符号表示,然后,根据符号运算按串行模式进行逻辑推理;这一过程可以写成串行的指令,让计算机执行。然而,直观性的思维是将分布式存储的信息综合起来,结果是忽然间产生想法或解决问题的办法。这种思维方式的根本之点在于以下两点:1.信息是通过神经元上的兴奋模式分布储在网络上;2.信息处理是通过神经元之间同时相互作用的动态过程来完成的。人工神经网络就是模拟人思维的第二种方式。这是一个非线性动力学系统,其特色在于信息的分布式存储和并行协同处理。虽然单个神经元的结构极其简单,功能有限,但大量神经元构成的网络系统所能实现的行为却是极其丰富多彩的。人工神经网络由大量的神经元构成。这些神经元一般分为三种:输入层神经元,隐层神经元和输出层神经元。输入层:单元i的输入:单元数量:d单元i的输出:单元i的激活函数:线性函数隐 层:单元j的输入:单元数量: 单元j的输出:单元j的激活函数:非线性函数输出层:单元k的输入:单元数量:c 单元k的输出:单元k的激活函数:非线性函数3、 程序设计1、 编译环境使用C#语言编写,编译器为VS2008,运行环境为Windows+.NetFramework3.5 2、 程序运行流程运行程序后,单击“训练”按钮,开始训练,训练完成后可以看到函数图像与各权值,输入x的值可以查看预测值与准确值。3、 类设计神经网络类的定义:class NeuralNetwork #region Instance Fields /private fields private int num_in; private int num_hid; private int num_out; private double, i_to_h_wts; private double, h_to_o_wts; private double inputs; private double hidden; private double outputs; private double learningRate = 0.3; private Random gen = new Random(); #endregion #region Constructor / / Creates a new NeuralNetwork, using the parameters / provided / / Number of inputs nodes / Number of hidden nodes / Number of output nodes public NeuralNetwork(int num_in, int num_hid, int num_out) this.num_in = num_in; this.num_hid = num_hid; this.num_out = num_out; i_to_h_wts = new doublenum_in + 1, num_hid; h_to_o_wts = new doublenum_hid + 1, num_out; inputs = new doublenum_in + 1; hidden = new doublenum_hid + 1; outputs = new doublenum_out; #endregion #region Initialization of random weights / / Randomly initialise all the network weights. / Need to start with some weights. / This method sets up the input to hidden nodes and / hidden nodes to output nodes with random values / public void initialiseNetwork() / Set the input value for bias node inputsnum_in = 1.0; hiddennum_hid = 1.0; / Set weights between input & hidden nodes. for (int i = 0; i num_in + 1; i+) for (int j = 0; j num_hid; j+) / Set random weights between -2 & 2 i_to_h_wtsi, j = (gen.NextDouble() * 4) - 2; / Set weights between hidden & output nodes. for (int i = 0; i num_hid + 1; i+) for (int j = 0; j num_out; j+) / Set random weights between -2 & 2 h_to_o_wtsi, j = (gen.NextDouble() * 4) - 2; #endregion #region Pass forward / / Does a complete pass through within the network, using the / applied_inputs parameters. The pass thorugh is done to the / input to hidden, and hidden to ouput layers / / An double array which holds input values, which / are then preseted to the networks input layer public void pass_forward(double applied_inputs) / Load a set of inputs into our current inputs for (int i = 0; i num_in; i+) inputsi = applied_inputsi; / Forward to hidden nodes, and calculate activations in hidden layer for (int i = 0; i num_hid; i+) double sum = 0.0; for (int j = 0; j num_in + 1; j+) sum += inputsj * i_to_h_wtsj, i; hiddeni = ActivationFunction.Sigmoid(sum); / Forward to output nodes, and calculate activations in output layer for (int i = 0; i num_out; i+) double sum = 0.0; for (int j = 0; j num_hid + 1; j+) sum += hiddenj * h_to_o_wtsj, i; /pass the sum, through the activation function, Sigmoid in this case /which allows for backward differentation outputsi = ActivationFunction.Sigmoid(sum); #endregion #region Public Properties / / gets / sets the number of input nodes for the Neural Network / public int NumberOfInputs get return num_in; set num_in = value; / / gets / sets the number of hidden nodes for the Neural Network / public int NumberOfHidden get return num_hid; set num_hid = value; / / gets / sets the number of output nodes for the Neural Network / public int NumberOfOutputs get return num_out; set num_out = value; / / gets / sets the input to hidden weights for the Neural Network / public double, InputToHiddenWeights get return i_to_h_wts; set i_to_h_wts = value; / / gets / sets the hidden to output weights for the Neural Network / public double, HiddenToOutputWeights get return h_to_o_wts; set h_to_o_wts = value; / / gets / sets the input values for the Neural Network / public double Inputs get return inputs; set inputs = value; / / gets / sets the hidden values for the Neural Network / public double Hidden get return hidden; set hidden = value; / / gets / sets the outputs values for the Neural Network / public double Outputs get return outputs; set outputs = value; / / gets / sets the LearningRate (eta) value for the Neural Network / public double LearningRate get return learningRate; set learningRate = value; #endregion 训练过程:public void Train() _nn.initialiseNetwork(); for (int i = 0; i training_times; i+) foreach (TrainSet trainSet in _trainSets) double inputs = new double trainSet.Input ; double outputs = new double trainSet.Output ; _nn.pass_forward(inputs); train_network(outputs); 权值变更函数:private void train_network(double outputs) /get momentum values (delta values from last pass) double delta_hidden = new double_nn.NumberOfHidden + 1; double delta_outputs = new double_nn.NumberOfOutputs; / Get the delta value for the output layer for (int i = 0; i _nn.NumberOfOutputs; i+) delta_outputsi = _nn.Outputsi * (1.0 - _nn.Outputsi) * (outputsi - _nn.Outputsi); / Get the delta value for the hidden layer for (int i = 0; i _nn.NumberOfHidden + 1; i+) double error = 0.0; for (int j = 0; j _nn.NumberOfOutputs; j+) error += _nn.HiddenToOutputWeightsi, j * delta_outputsj; delta_hiddeni = _nn.Hiddeni * (1.0 - _nn.Hiddeni) * error; / Now update the weights between hidden & output layer for (int i = 0; i _nn.NumberOfOutputs; i+) for (int j = 0; j _nn.NumberOfHidden + 1; j+) /use momentum (delta values from last pass), /to ensure moved in correct direction _nn.HiddenToOutputWeightsj, i += _nn.LearningRate * delta_outputsi * _nn.Hiddenj; / Now update the weights between input & hidden layer for (int i = 0; i _nn.NumberOfHidden; i+) for (int j = 0; j _nn.NumberOfInputs + 1; j+) /use momentum (delta values from last pass), /to ensure moved in correct direction _nn.InputToHiddenWeightsj, i += _nn.LearningRate * delta_hiddeni * _nn.Inputsj; 激活函数(sigmoid函数):public static double Sigmoid(double x) return 1.0 / (1.0 + Math.Pow(Math.E, -x); 建立训练集和输入层,隐层,输出层分别为1,4,1的神经网络实例: Tra

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论