人工神经网络入门(3) —— 多层人工神经网络应用示例
范例程序下载:https://files.cnblogs.com/gpcuster/ANN2.rar
如果您有疑问,可以先参考 FAQ
如果您未找到满意的答案,可以在下面留言:)
1 介绍
求异或(XOR)操作是计算机中常用到的一种计算:
0 XOR 0 = 0
0 XOR 1 = 1
1 XOR 0 = 1
1 XOR 1 = 0
我们可以使用第一篇文章中的代码来计算这个结果https://files.cnblogs.com/gpcuster/ANN1.rar(需要修改其中的训练集),可以发现学习后的结果不能让我们满意,原因是单层神经网络学习能力有限,需要使用更加复杂的网络来学习。
在这一篇文章中,我们将使用一个新的多层神经网络来学习。
2 范例程序的使用和说明
该程序的使用说明和人工神经网络入门(1) —— 单层人工神经网络应用示例类似,可以参考http://www.cnblogs.com/gpcuster/archive/2008/05/22/1204456.html
但是多了一个计算操作(XOR),如图:
3 网络结构
该多层神经网络在单层神经网络的基础上增加了一层“隐藏层”(Hidden)
4 学习算法
基本的原理就是用实际网络计算出来的值和期望的值进行比较,然后来调整自己的权值。
/// <summary>
/// 根据期望的输出和实际的输出来调整权值
/// </summary>
/// <param name="targOut">期望的输出</param>
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];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Get the delta value for the output layer
for (int i = 0; i < _nn.NumberOfOutputs; i++)
{
delta_outputs[i] =
_nn.Outputs[i] * (1.0 - _nn.Outputs[i]) * (outputs[i] - _nn.Outputs[i]);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 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.HiddenToOutputWeights[i, j] * delta_outputs[j];
}
delta_hidden[i] = _nn.Hidden[i] * (1.0 - _nn.Hidden[i]) * 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.HiddenToOutputWeights[j, i] += _nn.LearningRate * delta_outputs[i] * _nn.Hidden[j];
}
}
// 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.InputToHiddenWeights[j, i] += _nn.LearningRate * delta_hidden[i] * _nn.Inputs[j];
}
}
}
5 预告
在下一篇文章中,我将介绍一个C#实现的ANN框架:)
6 总结
这个多层神经网络也可以准确地计算AND和OR运算,但是精确度却比不上第一个例子。
不同的网络对不同的情况都有不同的适应程度,这就需要深层的理论支持了,也不是我能为大家解决的啦:(
如果您有疑问,可以先参考 FAQ
如果您未找到满意的答案,可以在下面留言:)
1 介绍
求异或(XOR)操作是计算机中常用到的一种计算:
0 XOR 0 = 0
0 XOR 1 = 1
1 XOR 0 = 1
1 XOR 1 = 0
我们可以使用第一篇文章中的代码来计算这个结果https://files.cnblogs.com/gpcuster/ANN1.rar(需要修改其中的训练集),可以发现学习后的结果不能让我们满意,原因是单层神经网络学习能力有限,需要使用更加复杂的网络来学习。
在这一篇文章中,我们将使用一个新的多层神经网络来学习。
2 范例程序的使用和说明
该程序的使用说明和人工神经网络入门(1) —— 单层人工神经网络应用示例类似,可以参考http://www.cnblogs.com/gpcuster/archive/2008/05/22/1204456.html
但是多了一个计算操作(XOR),如图:
![](https://www.cnblogs.com/images/cnblogs_com/gpcuster/www222.jpg)
3 网络结构
该多层神经网络在单层神经网络的基础上增加了一层“隐藏层”(Hidden)
![](https://www.cnblogs.com/images/cnblogs_com/gpcuster/Untitled000000.jpg)
4 学习算法
基本的原理就是用实际网络计算出来的值和期望的值进行比较,然后来调整自己的权值。
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
5 预告
在下一篇文章中,我将介绍一个C#实现的ANN框架:)
6 总结
这个多层神经网络也可以准确地计算AND和OR运算,但是精确度却比不上第一个例子。
不同的网络对不同的情况都有不同的适应程度,这就需要深层的理论支持了,也不是我能为大家解决的啦:(