1、 单元数量:d单元i的输出:单元i的激活函数:线性函数隐 层:单元j的输入:单元j的输出:单元j的激活函数:非线性函数输出层:单元k的输入:c 单元k的输出:单元k的激活函数:3、程序设计1、编译环境使用C#语言编写,编译器为VS2008,运行环境为Windows+.NetFramework3.52、 程序运行流程运行程序后,单击“训练”按钮,开始训练,训练完成后可以看到函数图像与各权值,输入x的值可以查看预测值与准确值。3、 类设计神经网络类的定义:class NeuralNetwork #region Instance Fields /private fields private int
2、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 Neura
3、lNetwork, using the parameters / provided/summaryparam name=num_inNumber of inputs nodesnum_hidNumber of hidden nodesnum_outNumber 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 doublen
4、um_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; #region Initialization of random weights / Randomly initialise all the network weights. / Need to start with some weights. / This method sets u
5、p 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+)
6、/ Set random weights between -2 & 2 i_to_h_wtsi, j = (gen.NextDouble() * 4) - 2; / Set weights between hidden & output nodes. num_hid + 1; num_out; h_to_o_wtsi, j = (gen.NextDouble() * 4) - 2; #region Pass forward / Does a complete pass through within the network, using the / applied_inputs paramete
7、rs. The pass thorugh is done to the / input to hidden, and hidden to ouput layersapplied_inputsAn 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 num_in; inputs
8、i = applied_inputsi; / Forward to hidden nodes, and calculate activations in hidden layer double sum = 0.0; sum += inputsj * i_to_h_wtsj, i; hiddeni = ActivationFunction.Sigmoid(sum); / Forward to output nodes, and calculate activations in output layer sum += hiddenj * h_to_o_wtsj, i; /pass the sum,
9、 through the activation function, Sigmoid in this case /which allows for backward differentation outputsi = ActivationFunction.Sigmoid(sum); #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; / get
10、s / 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
11、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 Neura
12、l 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 = val
13、ue; / gets / sets the LearningRate (eta) value for the Neural Network public double LearningRate get return learningRate; set learningRate = value;训练过程:public void Train() _nn.initialiseNetwork(); training_times; foreach (TrainSet trainSet in _trainSets) double inputs = new double trainSet.Input ; d
14、ouble 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.NumberOfOutp
15、uts; / Get the delta value for the output layer _nn.NumberOfOutputs; delta_outputsi = _nn.Outputsi * (1.0 - _nn.Outputsi) * (outputsi - _nn.Outputsi); / Get the delta value for the hidden layer _nn.NumberOfHidden + 1; double error = 0.0; error += _nn.HiddenToOutputWeightsi, j * delta_outputsj; delta
16、_hiddeni = _nn.Hiddeni * (1.0 - _nn.Hiddeni) * error; / Now update the weights between hidden & output layer /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 b
17、etween input & hidden layer _nn.NumberOfHidden; _nn.NumberOfInputs + 1; _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的神经网络实例: TrainSet set = new T
18、rainSetsetSize; setSize; double n = Math.PI * 2 / setSize; seti = new TrainSet(n * (i + 1), Math.Sin(n * (i + 1) / 2 + 0.5); NeuralNetwork network = new NeuralNetwork(1, 4, 1);绘制拟合后的图形: Graphics g; g = panel1.CreateGraphics(); g.Clear(panel1.BackColor); double pi=Math.PI*2/setSize; g.FillRectangle(b
19、rush,(float)(pi*(i+1)*40,100-(float)(_nntrainer.GetOutPut(new doublepi*(i+1)0)*100,1,1);输出任意角度的拟合值和正弦值: double _result = _nntrainer.GetOutPut(new double Double.Parse(x_value.Text) )0; result.Text = (_result - 0.5) * 2).ToString(); sinx.Text = Math.Sin(Double.Parse(x_value.Text).ToString();4、程序运行截图1、 程序界面2、 训练结果3、 对比预测值与准确值
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1