【问题标题】:Super minimal XOR neural network cannot learn... What did I do wrong?超极小异或神经网络无法学习……我做错了什么?
【发布时间】:2016-10-24 17:52:33
【问题描述】:

自学了神经网络的基础知识,我决定尝试学习 XOR 函数的超极小网络。它由两个输入神经元、两个隐藏神经元和一个输出神经元组成。问题是它不学习..所以我的backward()一定是做错了什么?代码非常简洁,可以使用任何支持 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 = %f\n",output[0]);
            printf("expected = %f\n",expected);
        }

        // backprop...
        backward( expected );
    }

    return 0;
}

【问题讨论】:

  • 给定足够的迭代次数,输出应该与预期的输出相匹配(向后传递)。它永远不会。

标签: c++ machine-learning neural-network


【解决方案1】:

我在您的神经元模型中缺少偏差(阈值、基本信号)。

https://en.wikipedia.org/wiki/Artificial_neuron#Basic_structure

您也可能不想在反向传播中进行如此剧烈的修正。

https://en.wikipedia.org/wiki/Backpropagation#Phase_2:_Weight_update

从权重中减去梯度的比率(百分比)。

比率可能低于 100%。

【讨论】:

  • 所有层都需要偏置吗?
  • @dicroce 在这种情况下只隐藏一个。
猜你喜欢
  • 2014-05-29
  • 1970-01-01
  • 1970-01-01
  • 2016-05-25
  • 1970-01-01
  • 2020-01-16
  • 2018-04-10
  • 2016-07-11
  • 2021-07-02
相关资源
最近更新 更多