超级最小的XOR神经网络无法学习..我做错了什么
Super minimal XOR neural network cannot learn... What did I do wrong?
自学神经网络的基础知识,我决定在学习XOR函数的超级网络上进行破解。它由两个输入神经元,两个隐藏的神经元和一个输出神经元组成。问题是它没有学到。该代码超级最小,应使用任何C 11支持编译器进行编译。
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <cmath>
#include <algorithm>
#include <numeric>
using namespace std;
float tanh_activate(float x) { return (exp(2*x)-1)/(exp(2*x)+1); }
float tanh_gradient(float x) { return 1-x*x; }
vector<float> input = { 0.0f, 0.0f };
vector<float> hiddenW = { 0.5f, 0.5f };
vector<float> hidden = { 0.0f, 0.0f };
vector<float> output = { 0.0f };
void forward()
{
float inputSum = accumulate( input.begin(), input.end(), 0.0f );
hidden[0] = tanh_activate( inputSum ) * hiddenW[0];
hidden[1] = tanh_activate( inputSum ) * hiddenW[1];
output[0] = tanh_activate( accumulate( hidden.begin(), hidden.end(), 0.0f ) );
}
void backward( float answer )
{
auto outputError = answer - output[0];
auto error = outputError * tanh_gradient( output[0] );
auto layerError = accumulate( hiddenW.begin(),
hiddenW.end(),
0.0f,
[error]( float sum, float w ) {
return sum + (w * error);
} );
// Calculating error for each activation in hidden layer but this is unused
// currently since their is only one hidden layer.
vector<float> layerE( hidden.size() );
transform( hidden.begin(),
hidden.end(),
layerE.begin(),
[layerError]( float a ) {
return tanh_gradient( a ) * layerError;
} );
// update weights
for( auto wi = hiddenW.begin(), ai = hidden.begin(); wi != hiddenW.end(); ++wi, ++ai )
*wi += *ai * error;
}
int main( int argc, char* argv[] )
{
for( int i = 0; i < 10000000; ++i )
{
// pick two bits at random...
int i1 = ((random() % 2)==0)?1.0f:0.0f;
int i2 = ((random() % 2)==0)?1.0f:0.0f;
// load our input layer...
input[0] = (float)i1;
input[1] = (float)i2;
// compute network output...
forward();
// we're teaching our network XOR
float expected = ((i1^i2)==0) ? 0.0f : 1.0f;
if( i % 10000 == 0 )
{
printf("output = %fn",output[0]);
printf("expected = %fn",expected);
}
// backprop...
backward( expected );
}
return 0;
}
我缺少神经元模型中的偏差(treshold,base信号)。
https://en.wikipedia.org/wiki/artcover_neuron#basic_structure
您也可能不想在反向传播中进行如此严重的校正。
https://en.wikipedia.org/wiki/backpropagation#phase_2:_weight_update
从重量中减去梯度的比率(百分比)。
比率可能低于100%。
相关文章:
- 学习多线程C++:添加线程不会使执行速度更快,即使它看起来应该
- 我是 c++ 的新手.学习基本知识后,我想做井字游戏.对于印刷板,我在下面写代码,但它显示错误
- 神经网络不学习.卡在50%
- 在学习数据结构之前对STL有一个了解是好的吗?
- 如何将经过训练的机器学习模型保存在python中并将其加载到C++中进行预测?
- 学习 c++,编译错误"no matching function for call"
- 学习数据结构和算法的简单方法
- 我正在尝试学习如何在 c++ 中传递指针,但出现错误:没有用于调用"test"的匹配函数。我做错了什么?
- 学习嵌套循环C++与示例混淆
- 从书本中学习C++无法使该程序与类一起工作
- 刚开始学习C++
- 学习C++并在早期示例中遇到错误(在非静态数据成员之前需要构造函数)
- 学习时空复杂性时语句XYZ的含义是什么?
- Q学习扫雷行为
- 我可以在没有C++的情况下学习 Win32 API(仅使用 C)吗?
- 学习C,我的编译器已经知道bool(Visual Studio 2017)
- 我正在学习C++,我不能使用指针访问参考吗?(举个例子)
- 试图学习指针,为什么要使用它们而不是仅仅使用&?
- Microsoft CNTK加强学习C 示例
- 与机器学习和小数据库进行分类