使用反向传播问题的神经网络训练

时间:2015-11-18 22:08:54

标签: c++ neural-network backpropagation mnist

这是我用于在具有10个神经元的单层感知器上使用反向传播算法对MNist数据库的手写数字进行分类的代码。数字保存在展开的(最后一列1)整数2d矩阵images[60000][785]int tlabels[60000][10]中的标签中。每个标签在与数字值对应的位置包含1,在其余标签中包含0个(数字为0 - 9)。

// weights1[785][10] has been initialized to random values on [-0.5,0.5] in the constructor. 
// bias of neurons have been taken into account as the last row of the matrix.
// b is the learning rate , equal to 0.5.

const int Ninput=784;
const int Noutput=10;
float result[Noutput];
for (int epoch =0 ; epoch < 30 ; epoch ++) { 
    float error =0;
    for (int sample=0 ; sample < 1000 ; sample++) {

        for (int output= 0 ; output< Noutput; output++) {
            float sum = 0;
            for (int input = 0 ; input < Ninput+1; input++)
                sum += weights1[input][output]*images[sample][input];
            result[output] = sum;
        }
        float delta[Noutput];
        for (int output= 0 ; output< Noutput;  output++) {
            delta[output]=(tlabels[sample][output]-sigmoid(result[output]))*sigmoidDerivative(result[output]);
            error+= pow((tlabels[sample][output]-sigmoid(result[output])),2.0);    
        }
        for (int output= 0 ; output< Noutput; output++)
            for (int input=0 ; input < Ninput+1 ; input++)
                weights1[input][output] = weights1[input][output] +b*delta[output]*images[sample][input];
    }
    cout << error / 1000.0;
}

error变量收敛到0.9,这意味着此代码将所有数字分类为其中一个类,即使样本在类之间平均分配。代码中是否存在逻辑错误,或者我应该尝试不同的参数集,直到结果可以接受为止?

0 个答案:

没有答案