【问题标题】:MLP not training XOR correctlyMLP 未正确训练 XOR
【发布时间】:2016-03-26 20:35:52
【问题描述】:

我是神经网络的新手。我一直在尝试实现一个两层网络来使用反向传播算法来学习 XOR 函数。隐藏层有 2 个单元,输出层有 1 个单元。所有单元都使用 sigmoid 激活函数。

我正在初始化 -1 到 +1 之间的权重,并且有 +1 偏差。

问题在于,当使用其他一些随机权重值从头开始重新初始化时,网络学习函数的次数非常少。它在非常少的迭代中学习其他布尔函数(AND、OR),几乎每次都用于几乎所有随机分配的权重。问题在于 XOR 函数 - 对于某些随机权重值,它不会收敛到最优值。

我正在使用随机梯度下降、反向传播进行学习。

我的代码:http://ideone.com/IYPW2N

隐藏层的计算输出函数代码:

public double computeOutput(double[] input){
    this.input = input;
    output = bias+w[0]*input[0] + w[1]*input[1];
    output = 1/(1+Math.pow(Math.E, -output));    
    return output;
}

计算错误函数代码:

public double computeError(double w, double outputUnitError){
    error = (output)*(1-output)*(outputUnitError*w);  
    return error;        
}

修复隐藏单元的错误:

public void fixError(){  
    for(int i=0;i<input.length;++i) w[i] += n*error*input[i]; 
}

输出单元的计算输出函数:

public void computeOutput(double[] input) { 
    this.input = input;  
    output = bias+input[0]*w[0]+input[1]*w[1];
    output = 1/(1+Math.pow(Math.E, -output));   
} 

输出单元的computeError函数:

public void computeError(double t){ 
    this.t = t; 
    error = (output)*(1-output)*(t-output); 
}

输出单元的 fixError 函数(更新权重):

public void fixError() {     
    for(int i=0;i<w.length;++i) w[i] += n*error*input[i]; 
}   

代码在任何迭代中停止训练,所有示例都被正确分类。否则当迭代次数超过 90k 时停止。

学习率设置为 0.05。如果输出单元的值大于0.5,则计为+1,否则计为0。

培训示例:

static Example[] examples = {
        new Example(new double[]{0, 0}, 0),
        new Example(new double[]{0,  1}, 1),
        new Example(new double[]{1, 0}, 1),
        new Example(new double[]{1,  1}, 0)
};

代码输出:

Iterations > 90000, stop...
Displaying outputs for all examples... 
0.018861254512881773
0.7270271284494716
0.5007550527204925
0.5024353957353963

Training complete. No of iterations = 45076
Displaying outputs for all examples... 
0.3944511789979849
0.5033004761575361
0.5008283246200929
0.2865272493546562

Training complete. No of iterations = 39707
Displaying outputs for all examples... 
0.39455754434259843
0.5008762488126696
0.5029579167912538
0.28715696580224176

Iterations > 90000, stop...
Displaying outputs for all examples... 
0.43116164638530535
0.32096730276984053
0.9758219334403757
0.32228953888593287

我尝试了几个学习率的值并增加了隐藏层单元的数量,但仍然没有学习 XOR。

请纠正我在哪里弄错了或者在实现中是否存在错误。

我检查了其他线程,但我的问题没有得到任何令人满意的解决方案。

【问题讨论】:

    标签: machine-learning neural-network backpropagation perceptron


    【解决方案1】:

    您也应该学习偏差,而您的代码假定偏差是恒定的(您应该将权重与偏差联系起来)。没有偏见,你将无法学习 XOR。

    【讨论】:

    • 我更正了代码。现在如果我使用 2 个隐藏单元(+1 个偏置单元),代码几乎收敛于 80% 随机权重分配 3 个隐藏单元(+1 个偏置单元),几乎 90% 4 个隐藏单元,几乎 99% 这看起来不错,或者在第一种情况下应该达到 100% 吗?
    • 输出层的偏差在哪里?
    • 隐藏层和输出层都得到了有偏差的输入。
    • 从新代码看来并非如此。你有新的一列相当于有偏差的隐藏层,但输出没有偏差
    • 输出确实得到了有偏差的输入。 outputHLayer(隐藏层单元的输出)数组的第 0 个索引在第 107 行设置为 1 用于偏置。然后第 112 行使用“outputsHLayer”的内容计算 outputUnit 的输出,应用适当的权重,包括偏差的权重。第 117 行:更新输出单元的输入连接的权重。接下来隐藏层单元的输入连接的权重被更新。每当需要时,我都会使用索引 0 作为偏差。
    猜你喜欢
    • 2016-03-04
    • 2019-07-17
    • 2014-01-13
    • 2016-12-07
    • 2021-11-17
    • 2012-02-10
    • 1970-01-01
    • 2016-10-01
    • 2018-08-18
    相关资源
    最近更新 更多