XOR 与神经网络 (Matlab)
XOR with Neural Networks (Matlab)
所以,我希望这是我正在做的一件真正愚蠢的事情,并且有一个简单的答案。我正在尝试训练一个 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 次迭代,它只是 returns 全 0。
这是怎么回事?我必须添加正则化吗?我原以为这样一个简单的网络不需要它。但是,无论如何,为什么它偏离了我亲手喂给它的明显好的解决方案?
谢谢!
AAARRGGHHH!解决方案只是改变
theta1 = theta1 - 0.1 * theta1d;
theta2 = theta2 - 0.1 * theta2d;
到
theta1 = theta1 + 0.1 * theta1d;
theta2 = theta2 + 0.1 * theta2d;
感叹
现在,我需要弄清楚我是如何计算负导数的,而我认为我正在计算的是......没关系。我会 post 无论如何,以防万一它帮助别人。
因此,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) returns 0.0028 0.9972 0.9948 0.0009 和
xornn(10000) returns 0.0016 0.9989 0.9993 0.0005
呸!也许这会帮助其他人调试他们的版本..
所以,我希望这是我正在做的一件真正愚蠢的事情,并且有一个简单的答案。我正在尝试训练一个 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 次迭代,它只是 returns 全 0。
这是怎么回事?我必须添加正则化吗?我原以为这样一个简单的网络不需要它。但是,无论如何,为什么它偏离了我亲手喂给它的明显好的解决方案?
谢谢!
AAARRGGHHH!解决方案只是改变
theta1 = theta1 - 0.1 * theta1d;
theta2 = theta2 - 0.1 * theta2d;
到
theta1 = theta1 + 0.1 * theta1d;
theta2 = theta2 + 0.1 * theta2d;
感叹
现在,我需要弄清楚我是如何计算负导数的,而我认为我正在计算的是......没关系。我会 post 无论如何,以防万一它帮助别人。
因此,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) returns 0.0028 0.9972 0.9948 0.0009 和 xornn(10000) returns 0.0016 0.9989 0.9993 0.0005
呸!也许这会帮助其他人调试他们的版本..