【问题标题】:XOR with Neural Networks (Matlab)XOR 与神经网络 (Matlab)
【发布时间】:2015-07-21 17:10:30
【问题描述】:

所以,我希望这是我正在做的一件真正愚蠢的事情,并且有一个简单的答案。我正在尝试训练一个 2x3x1 神经网络来解决 XOR 问题。它不起作用,所以我决定深入了解发生了什么。最后,我决定自己分配权重。这是我想出的权重向量:

theta1 = [11 0 -5; 0 12 -7;18 17 -20];
theta2 = [14 13 -28 -6];

(在 Matlab 表示法中)。我故意试图让两个权重不一样(除了零)

而且,我的代码在 matlab 中非常简单

function layer2 = xornn(iters)
    if nargin < 1
        iters = 50
    end
    function s = sigmoid(X)
        s = 1.0 ./ (1.0 + exp(-X));
    end
    T = [0 1 1 0];
    X = [0 0 1 1; 0 1 0 1; 1 1 1 1];
    theta1 = [11 0 -5; 0 12 -7;18 17 -20];
    theta2 = [14 13 -28 -6];
    for i = [1:iters]
        layer1 = [sigmoid(theta1 * X); 1 1 1 1];
        layer2 = sigmoid(theta2 * layer1)
        delta2 = T - layer2;
        delta1 = layer1 .* (1-layer1) .* (theta2' * delta2);
        % remove the bias from delta 1. There's no real point in a delta on the bias.
        delta1 = delta1(1:3,:);
        theta2d = delta2 * layer1';
        theta1d = delta1 * X';
        theta1 = theta1 - 0.1 * theta1d;
        theta2 = theta2 - 0.1 * theta2d;
    end
end

我相信这是对的。我用有限差分法测试了各种参数(thetas),看看它们是否正确,它们似乎是正确的。

但是,当我运行它时,它最终归结为返回全零。如果我执行 xornn(1)(1 次迭代),我会得到

0.0027    0.9966    0.9904    0.0008

但是,如果我执行 xornn(35)

0.0026    0.9949    0.9572    0.0007

(它开始朝错误的方向下降)当我到达 xornn(45) 时,我得到了

0.0018    0.0975    0.0000    0.0003

如果我运行它 10,000 次迭代,它只会返回全 0。

发生了什么事?我必须添加正则化吗?我原以为这样一个简单的网络不需要它。但是,无论如何,为什么它会远离我亲手喂给它的明显好的解决方案?

谢谢!

【问题讨论】:

    标签: matlab neural-network xor


    【解决方案1】:

    啊啊啊啊啊!解决方案只是改变

    theta1 = theta1 - 0.1 * theta1d;
    theta2 = theta2 - 0.1 * theta2d;
    

    theta1 = theta1 + 0.1 * theta1d;
    theta2 = theta2 + 0.1 * theta2d;
    

    叹息

    现在,我需要弄清楚我是如何计算负导数的,而我认为我正在计算的是……没关系。无论如何我都会在这里发帖,以防万一它帮助别人。

    所以,z = 是 sigmoid 输入的总和,y 是 sigmoid 的输出。

    C = -(T * Log[y] + (1-T) * Log[(1-y))
    
    dC/dy = -((T/y) - (1-T)/(1-y))
          = -((T(1-y)-y(1-T))/(y(1-y)))
          = -((T-Ty-y+Ty)/(y(1-y)))
          = -((T-y)/(y(1-y)))
          = ((y-T)/(y(1-y))) # This is the source of all my woes.
    dy/dz = y(1-y)
    dC/dz = ((y-T)/(y(1-y))) * y(1-y)
          = (y-T)
    

    所以,问题在于我不小心计算了 T-y,因为我忘记了成本函数前面的负号。然后,我减去了我认为的梯度,但实际上是负梯度。而且,那里。这就是问题所在。

    一旦我这样做了:

    function layer2 = xornn(iters)
        if nargin < 1
            iters = 50
        end
        function s = sigmoid(X)
            s = 1.0 ./ (1.0 + exp(-X));
        end
        T = [0 1 1 0];
        X = [0 0 1 1; 0 1 0 1; 1 1 1 1];
        theta1 = [11 0 -5; 0 12 -7;18 17 -20];
        theta2 = [14 13 -28 -6];
        for i = [1:iters]
            layer1 = [sigmoid(theta1 * X); 1 1 1 1];
            layer2 = sigmoid(theta2 * layer1)
            delta2 = T - layer2;
            delta1 = layer1 .* (1-layer1) .* (theta2' * delta2);
            % remove the bias from delta 1. There's no real point in a delta on the bias.
            delta1 = delta1(1:3,:);
            theta2d = delta2 * layer1';
            theta1d = delta1 * X';
            theta1 = theta1 + 0.1 * theta1d;
            theta2 = theta2 + 0.1 * theta2d;
        end
    end
    

    xornn(50) 返回 0.0028 0.9972 0.9948 0.0009 和 xornn(10000) 返回 0.0016 0.9989 0.9993 0.0005

    呸!也许这会帮助其他人调试他们的版本..

    【讨论】:

      猜你喜欢
      • 2023-03-23
      • 2014-05-09
      • 1970-01-01
      • 2015-02-04
      • 2019-03-15
      • 2016-05-13
      • 2012-03-28
      • 2017-01-30
      • 1970-01-01
      相关资源
      最近更新 更多