-1

我遵循了这段代码: https ://github.com/HyTruongSon/Neural-Network-MNIST-CPP

这很容易理解。它产生 94% 的准确率。我必须将其转换为具有更深层次的网络(范围从 5 到 10)。为了让自己舒服,我只多加了一层。但是,无论我训练多少,准确率都不会超过 50%。我在每个隐藏层中添加了 256 个神经元。这是我修改代码的方式:我添加了这样的额外层:

// From layer 1 to layer 2. Or: Input layer - Hidden layer
double *w1[n1 + 1], *delta1[n1 + 1], *out1;

// From layer 2 to layer 3. Or; Hidden layer - 2Hidden layer
double *w2[n2 + 1], *delta2[n2 + 1], *in2, *out2, *theta2;

// From layer 3 to layer 4. Or; Hidden layer - Output layer
double *w3[n3 + 1], *delta3[n3 + 1], *in3, *out3, *theta3;

// Layer 3 - Output layer
double *in4, *out4, *theta4;
double expected[n4 + 1];

前馈部分是这样修改的:

void perceptron() {
    for (int i = 1; i <= n2; ++i) {
        in2[i] = 0.0;
    }

    for (int i = 1; i <= n3; ++i) {
        in3[i] = 0.0;
    }
    for (int i = 1; i <= n4; ++i) {
        in4[i] = 0.0;
    }

    for (int i = 1; i <= n1; ++i) {
        for (int j = 1; j <= n2; ++j) {
            in2[j] += out1[i] * w1[i][j];
        }
    }

    for (int i = 1; i <= n2; ++i) {
        out2[i] = sigmoid(in2[i]);
    }

  /////
     for (int i = 1; i <= n2; ++i) {
        for (int j = 1; j <= n3; ++j) {
            in3[j] += out2[i] * w2[i][j];
        }
    }

    for (int i = 1; i <= 3; ++i) {
        out3[i] = sigmoid(in3[i]);
    }

  ////
    for (int i = 1; i <= n3; ++i) {
        for (int j = 1; j <= n4; ++j) {
            in4[j] += out3[i] * w3[i][j];
        }
    }

    for (int i = 1; i <= n4; ++i) {
        out4[i] = sigmoid(in4[i]);
    }
}

反向传播是这样改变的:

void back_propagation() {
    double sum;

    for (int i = 1; i <= n4; ++i) {
        theta4[i] = out4[i] * (1 - out4[i]) * (expected[i] - out4[i]);
    }

    for (int i = 1; i <= n3; ++i) {
        sum = 0.0;
        for (int j = 1; j <= n4; ++j) {
            sum += w3[i][j] * theta4[j];
        }
        theta3[i] = out3[i] * (1 - out3[i]) * sum;
    }

    for (int i = 1; i <= n3; ++i) {
        for (int j = 1; j <= n4; ++j) {
            delta3[i][j] = (learning_rate * theta4[j] * out3[i]) + (momentum * delta3[i][j]);
            w3[i][j] += delta3[i][j];
        }
    }

    /////////////

       for (int i = 1; i <= n2; ++i) {
        for (int j = 1; j <= n3; ++j) {
            delta2[i][j] = (learning_rate * theta3[j] * out2[i]) + (momentum * delta2[i][j]);
            w2[i][j] += delta2[i][j];
        }
    }
   /////////////////

    for (int i = 1; i <= n1; ++i) {
        for (int j = 1 ; j <= n2 ; j++ ) {
            delta1[i][j] = (learning_rate * theta2[j] * out1[i]) + (momentum * delta1[i][j]);
            w1[i][j] += delta1[i][j];
        }
    }
}

我也发布了我的修改,因为我可能在这里的某个地方错了。一旦我将 epochs 变量设置为 1000 并让它训练 24 小时,仍然没有进展 :( 。我对此感到非常沮丧,我不知道我可能错在哪里。

4

1 回答 1

0

您是否忘记将反向传播添加到从第 3 层到第 2 层的 thetha2 参数?

for (int i = 1; i <= n2; ++i) {
    sum = 0.0;
    for (int j = 1; j <= n3; ++j) {
       sum += w2[i][j] * theta3[j];
    }
    theta2[i] = out2[i] * (1 - out2[i]) * sum;
}
于 2018-10-31T15:55:58.260 回答