2017-02-04 5 views
1

내 처음 질문하기.MATLAB Perceptron을 처음부터 - OR 함수

저는 신경 네트워크를 가르치고 있으며 현재 퍼셉트론 알고리즘을 프로그래밍하려고합니다. 나는 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 

답변

1

당신은 다음과 같습니다하지만 the one on Wikipedia 당신이 구현하려고하는 것입니다 생각하는 퍼셉트론 알고리즘 모른다.

  1. w3x1 될 것입니다 당신이 당신의 입력 기능에 말에 사람의 열을 추가 할 필요가 무게에 바이어스를 통합하는 것이 좋습니다. 이렇게하면 행렬 곱셈을 사용하여 즉 벡터화 된 방식으로 wx+b을 구현할 수 있습니다.
  2. out을 업데이트하지 않습니다. 다음 줄을 추가해야합니다. out(j,1) = y > 0;
  3. 왜이 조건을 넣으십시오 : if(out(j,1)~=desired_out(j,1))? Wikipedia에서는 언급하지 않았습니다. 어쨌든 실수로만 업데이트하려면 양수 및 음수 샘플에서 실수로 업데이트해야합니다. this을 참조하십시오.
  4. Doing input(j,1)*desired_out(j,1)이 잘못되었습니다. Wikipedia에 따르면, 그것은 (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 루프의 행렬 곱셈을 사용하여 더 벡터화 할 수 있지만, 내가 당신에게 그를 떠나 다음과 같이

수정 된 코드입니다.