2016-12-26 46 views
0

以下是在a blog中提供的一个简单的Perceptron的实现。神经网络代码解释

input = [0 0; 0 1; 1 0; 1 1]; 
numIn = 4; 
desired_out = [0;1;1;1]; 
bias = -1; 
coeff = 0.7; 
rand('state',sum(100*clock)); 
weights = -1*2.*rand(3,1); 

iterations = 10; 

for i = 1:iterations 
    out = zeros(4,1); 
    for j = 1:numIn 
      y = bias*weights(1,1)+... 
       input(j,1)*weights(2,1)+input(j,2)*weights(3,1); 
      out(j) = 1/(1+exp(-y)); 
      delta = desired_out(j)-out(j); 
      weights(1,1) = weights(1,1)+coeff*bias*delta; 
      weights(2,1) = weights(2,1)+coeff*input(j,1)*delta; 
      weights(3,1) = weights(3,1)+coeff*input(j,2)*delta; 
    end 
end 

我有以下问题,

(1)其中之一就在这里训练数据?

(2)这里是哪一个测试数据?

(3)这里是标签?

回答

1

训练数据为[0 0; 0 1; 1 0; 1 1]另一种观点每一行是一组训练数据如下

>> input 

input = 

0  0 
0  1 
1  0 
1  1 

和目标

desired_out = 

0 
1 
1 
1 

请想一想desired_out这是你的标签 .. 每一行中训练数据(输入)在二进制集合{0,1}中有一个特定的输出(标签)(因为这个例子用于执行OR逻辑电路。)

在matlab中你可以使用或者作用如下做进一步的了解:

>> or(0,0) 

    ans = 

     0 

    >> or(1,0) 

    ans = 

     1 

    >> or(0,1) 

    ans = 

     1 

    >> or(1,1) 

    ans = 

     1 

请注意,您的代码没有任何培训测试和验证码只是试图让重量和感知的其他参数,但你可以通过只小程序训练测试添加到您的代码

NumDataTest = 10 ; 
    test=randi([0 , 1] , [ NumDataTest , 2]) ... 
     +(2*rand(NumDataTest,2)-1)/20; 

这样的测试数据将类似于下面

 test = 

    1.0048 1.0197 
    0.0417 0.9864 
    -0.0180 1.0358 
    1.0052 1.0168 
    1.0463 0.9881 
    0.9787 0.0367 
    0.9624 -0.0239 
    0.0065 0.0404 
    1.0085 -0.0109 
    -0.0264 0.0429 

测试这个数据,您可以通过下面的代码使用自己的程序:

for i=1:NumDataTest 
     y = bias*weights(1,1)+test(i,1)*weights(2,1)+test(i,2)*weights(3,1); 
     out(i) = 1/(1+exp(-y)); 
    end 

最后:

 table(test(:,1),test(:,2),out,'VariableNames',{'input1' 'input2' 'output'}) 

输出

  input1  input2  output 
     _________ _________ ________ 

      1.0048  1.0197  0.99994 
      0.041677  0.98637  0.97668 
     -0.017968  1.0358  0.97527 
      1.0052  1.0168  0.99994 
      1.0463  0.98814  0.99995 
      0.97875  0.036674  0.9741 
      0.96238 -0.023861  0.95926 
      0.0064527  0.040392 0.095577 
      1.0085 -0.010895  0.97118 
     -0.026367  0.042854 0.080808 

代码部分:

clc 
    clear 
    input = [0 0; 0 1; 1 0; 1 1]; 
    numIn = 4; 
    desired_out = [0;1;1;1]; 
    bias = -1; 
    coeff = 0.7; 
    rand('state',sum(100*clock)); 
    weights = -1*2.*rand(3,1); 

    iterations = 100; 

    for i = 1:iterations 
    out = zeros(4,1); 
     for j = 1:numIn 
      y = bias*weights(1,1)+input(j,1)*weights(2,1)+input(j,2)*weights (3,1); 
      out(j) = 1/(1+exp(-y)); 
      delta = desired_out(j)-out(j); 
      weights(1,1) = weights(1,1)+coeff*bias*delta; 
      weights(2,1) = weights(2,1)+coeff*input(j,1)*delta; 
      weights(3,1) = weights(3,1)+coeff*input(j,2)*delta; 
     end 
    end 
    %% Test Section 
    NumDataTest = 10 ; 
    test=randi([0 , 1] , [ NumDataTest , 2]) ... 
     +(2*rand(NumDataTest,2)-1)/20; 
    for i=1:NumDataTest 
     y = bias*weights(1,1)+test(i,1)*weights(2,1)+test(i,2)*weights(3,1); 
     out(i) = 1/(1+exp(-y)); 
    end 
    table(test(:,1),test(:,2),out,'VariableNames',{'input1' 'input2' 'output'}) 

我希望这有助于为我的英语对不起,如果它的坏

+0

我还有一个问题:这个程序使用2个特征和1个标签的训练数据。这就是为什么可以在二维空间中绘制它们并绘制决策边界线的原因。对?但是,如果有更多的功能呢?我们如何绘制具有两个以上特征的样本的决策边界? – anonymous

+1

我认为二维空间只是为了更好的理解,如果你有两个以上的特征,对于情节决策边界不是好主意,更好的想法是回答这个问题“我的分类器有多好?”。您可以在MATLAB中使用ROC图,并使用决策函数。 所以在这种情况下你有决定的功能,但不是好主意的情节,因为情节是为了知道“我的分类器有多好?” –