从零开始的 MATLAB 感知器 - OR 函数
MATLAB Perceptron from scratch - OR function
第一次提问
我正在自学神经网络,目前正在尝试编写感知器算法。我想为 OR 功能训练它,但它不起作用。我完全不知道我做错了什么,互联网上没有不使用工具箱的解决方案。
input = [0 0; 0 1; 1 0; 1 1]%input vector
num_in = 4;% number of iterations
desired_out = [0;1;1;1] %desired output
bias = -1; %bias
w=zeros(2,1); %weight vector, initially zero
iterations = 100; % number of iterations to go through
for i = 1:iterations
out = zeros(4,1);
for j = 1:num_in %go per row of x
y = bias+input(j,1)*w(1,1)+input(j,2)*w(2,1) %sum
if(out(j,1)~=desired_out(j,1)) % modify weights and bias if mismatch exists
bias = bias+desired_out(j,1);
w(1,1) =w(1,1)+input(j,1)*desired_out(j,1);
w(2,1) = w(2,1)+input(j,2)*desired_out(j,1);
end
end
end
out %print the output
我不知道您使用的是哪种感知器算法,但我认为 the one on Wikipedia 是您要实现的算法。
- 最好将偏差纳入权重,即
w
将是 3x1
并且您必须在输入特征的末尾附加一列 1。这将允许您使用矩阵乘法(即以矢量化方式)实现 wx+b
。
- 您没有更新
out
。您应该添加以下行:
out(j,1) = y > 0;
- 为什么要加上这个条件:
if(out(j,1)~=desired_out(j,1))
?维基百科上没有提到它。无论如何,如果你只想更新错误,那么你必须对正样本和负样本的错误进行不同的更新。参见 this。
- 做
input(j,1)*desired_out(j,1)
是错误的。根据维基百科,应该是 (desired_out(j,1)-out(j,1))
.
修改后的代码如下:
input = [0 0 1; 0 1 1; 1 0 1; 1 1 1] % input vector
num_in = 4; % number of samples
desired_out = [0;1;1;1] % desired output
w=zeros(3,1); % weight vector, initially zero
iterations = 100; % number of iterations to go through
for i = 1:iterations
out = zeros(4,1);
for j = 1:num_in % go per row of x
y = input(j,1)*w(1,1)+input(j,2)*w(2,1)+w(3,1); % sum
out(j,1) = y>0;
w(1,1) =w(1,1)+input(j,1)*(desired_out(j,1)-out(j,1));
w(2,1) = w(2,1)+input(j,2)*(desired_out(j,1)-out(j,1));
w(3,1) = w(3,1)+input(j,3)*(desired_out(j,1)-out(j,1));
end
end
out %print the output
这可以通过使用矩阵乘法而不是 for
循环来进一步矢量化,但我会把它留给你。
第一次提问
我正在自学神经网络,目前正在尝试编写感知器算法。我想为 OR 功能训练它,但它不起作用。我完全不知道我做错了什么,互联网上没有不使用工具箱的解决方案。
input = [0 0; 0 1; 1 0; 1 1]%input vector
num_in = 4;% number of iterations
desired_out = [0;1;1;1] %desired output
bias = -1; %bias
w=zeros(2,1); %weight vector, initially zero
iterations = 100; % number of iterations to go through
for i = 1:iterations
out = zeros(4,1);
for j = 1:num_in %go per row of x
y = bias+input(j,1)*w(1,1)+input(j,2)*w(2,1) %sum
if(out(j,1)~=desired_out(j,1)) % modify weights and bias if mismatch exists
bias = bias+desired_out(j,1);
w(1,1) =w(1,1)+input(j,1)*desired_out(j,1);
w(2,1) = w(2,1)+input(j,2)*desired_out(j,1);
end
end
end
out %print the output
我不知道您使用的是哪种感知器算法,但我认为 the one on Wikipedia 是您要实现的算法。
- 最好将偏差纳入权重,即
w
将是3x1
并且您必须在输入特征的末尾附加一列 1。这将允许您使用矩阵乘法(即以矢量化方式)实现wx+b
。 - 您没有更新
out
。您应该添加以下行:out(j,1) = y > 0;
- 为什么要加上这个条件:
if(out(j,1)~=desired_out(j,1))
?维基百科上没有提到它。无论如何,如果你只想更新错误,那么你必须对正样本和负样本的错误进行不同的更新。参见 this。 - 做
input(j,1)*desired_out(j,1)
是错误的。根据维基百科,应该是(desired_out(j,1)-out(j,1))
.
修改后的代码如下:
input = [0 0 1; 0 1 1; 1 0 1; 1 1 1] % input vector
num_in = 4; % number of samples
desired_out = [0;1;1;1] % desired output
w=zeros(3,1); % weight vector, initially zero
iterations = 100; % number of iterations to go through
for i = 1:iterations
out = zeros(4,1);
for j = 1:num_in % go per row of x
y = input(j,1)*w(1,1)+input(j,2)*w(2,1)+w(3,1); % sum
out(j,1) = y>0;
w(1,1) =w(1,1)+input(j,1)*(desired_out(j,1)-out(j,1));
w(2,1) = w(2,1)+input(j,2)*(desired_out(j,1)-out(j,1));
w(3,1) = w(3,1)+input(j,3)*(desired_out(j,1)-out(j,1));
end
end
out %print the output
这可以通过使用矩阵乘法而不是 for
循环来进一步矢量化,但我会把它留给你。