You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

XOR神经网络训练后输出恒为0.5的问题排查求助

Hey there, let's figure out why your XOR neural network is stuck outputting ~0.5 no matter what inputs you feed it. That's a classic red flag that the network isn't properly updating its weights during training, so let's break down the issues in your code step by step.

1. Critical Typo in Forward Pass (The Likely Culprit)

Looking at your forwardPass method, the final line has a typo that's breaking everything:

outputs[outputs.length-1] = input;

You meant to assign inputs (plural) here, not input (singular). Since input isn't defined in this scope, this line is probably throwing a compile error (or if you have a variable named input elsewhere, it's assigning the wrong value entirely).

This mistake means the forwardPass method is returning incorrect neuron values for the output layer. When your backpropagation uses these wrong values to calculate weight updates, the network can't learn any meaningful patterns—hence the constant 0.5 output (the midpoint of the sigmoid function, a sign the network's weights aren't changing at all).

Here's the fixed forwardPass method:

public double[][] forwardPass(double[] image_vector) {
    double[][] outputs = new double[sizes.length][];
    double[] inputs = image_vector;
    for(int w = 0; w < weights.length; w++) {
        outputs[w] = inputs;
        double[] output = new double[weights[w].length];
        for(int j = 0; j < weights[w].length; j++) {
            output[j] = bias[w][j];
            for(int i = 0; i < weights[w][j].length; i++) {
                output[j] += weights[w][j][i] * inputs[i];
            }
            output[j] = sigmoid(output[j]);
        }
        inputs = output;
    }
    outputs[outputs.length-1] = inputs; // Fixed typo: input -> inputs
    return outputs;
}

2. Quick Checks to Confirm Training Works

Once you fix the typo, here are a few more things to verify:

  • Network Structure: For XOR, make sure you're using a structure like new int[]{2, 2, 1} (2 inputs, 2 hidden neurons, 1 output). A single-layer network can't solve XOR, so the hidden layer is mandatory.
  • Learning Rate: Your LEARNING_RATE should be in a reasonable range (0.1 to 0.5 works well for XOR). If it's too small, the network will update too slowly; if it's too large, it might oscillate and never converge.
  • Training Loop: Ensure you're calling backpropagate repeatedly with the full XOR dataset ([[0,0], [0,1], [1,0], [1,1]] paired with [[0], [1], [1], [0]]) over many epochs (hundreds or thousands). Print the total error each epoch—you should see it decrease over time if the network is learning.

3. Verify Weight Updates

After fixing the forward pass, add a quick debug step: print a few weights before and after a backpropagation call. You should see small changes in the weights—if they stay the same, there's still an issue with your delta calculations.

Once you fix the typo, your network should start learning the XOR pattern properly. Let me know if you run into more issues!

内容的提问来源于stack exchange,提问作者Mitch

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:41:02