Scilab中的神经网络识别

时间:2017-12-27 13:19:16

标签: neural-network scilab

我尝试运行这个示例,它使用ANN Toolbox for Scilab https://burubaxair.wordpress.com/2014/03/12/artificial-neural-networks-in-scilab/

这是代码:

    T = [
1 1 1 1 1
0 0 1 0 0
0 0 1 0 0
0 0 1 0 0
0 0 1 0 0
0 0 1 0 0
0 0 1 0 0
]';
U = [
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
0 1 1 1 0
]';
N = [35 10 2];
W = ann_FF_init(N);
x = [1, 0, 0, 0, 1;
1, 0, 0, 0, 1;
1, 0, 0, 0, 1;
1, 0, 0, 0, 1;
1, 0, 0, 0, 1;
1, 0, 0, 0, 1;
0, 1, 1, 1, 0]';
t_t = [1 0]';
t_u = [0 1]';
t = [t_t, t_u];
lp = [0.01, 1e-4];
epochs = 3000;
W = ann_FF_Std_batch(x,t,N,W,lp,epochs);
y = ann_FF_run(x,N,W)
disp(y)

但我收到一个错误:

-->exec('D:\Учёба\Задачи\Recognition.sce', -1)
 !--error 15 
Подматрица задана некорректно (Submatrix is incorrect).
at line      37 of function ann_FF_grad_BP called by :  
at line      25 of function ann_FF_Std_batch called by :  
W = ann_FF_Std_batch(x,t,N,W,lp,epochs);
at line      33 of exec file called by :    
exec('D:\Учёба\Задачи\Recognition.sce', -1)

错误可能在T和U矩阵中,但我不明白为什么。你能说出我做错了什么吗?谢谢!

1 个答案:

答案 0 :(得分:0)

您的代码中出现了2个错误:

  1. 你不应该混合测试和训练集。
  2. 测试输入必须是单列。
  3. 您的第一个错误是x = [ 1.... ],因为它包含一个图像,而您在N中指定您有两个输出神经元。 如示例中所述,您应该x = [T,U];

    您的第二个错误是将x作为ann_FF_run的测试。此函数将测试输入作为单个列。但是,因为你在用5x7矩阵之前用x训练你的NN。只需将其更改为列向量。

    这是经过更正和评论的代码:

    T = [...
    1 1 1 1 1 ...
    0 0 1 0 0 ...
    0 0 1 0 0 ...
    0 0 1 0 0 ...
    0 0 1 0 0 ...
    0 0 1 0 0 ...
    0 0 1 0 0 ...
    ]';
    U = [...
    1 0 0 0 1 ...
    1 0 0 0 1 ...
    1 0 0 0 1 ...
    1 0 0 0 1 ...
    1 0 0 0 1 ...
    1 0 0 0 1 ...
    0 1 1 1 0 ...
    ]';
    // setting the traing set of two image
    xtrain = [T,U];
    // so each image as 35 pixels so N(1) is 35
    // and we have two images so N($) is 2
    N = [35 10 2];
    // training the NN
    W = ann_FF_init(N);
    // The expected response for T : 1 for T, 0 for U
    t_t = [1 0]';
    // The expected response for T : 1 for T, 0 for U
    t_u = [0 1]';
    // the overall response
    t = [t_t, t_u];
    // some parameters
    lp = [0.01, 1e-4];
    epochs = 3000;
    // getting the weight of the trained NN
    W = ann_FF_Std_batch(xtrain,t,N,W,lp,epochs);
    
    // testing the traing set. 
    y = ann_FF_run(xtrain,N,W)
    disp('Testing the traing set')
    disp(y) //should get something close to t ~ [1 0 ; 0 1]
    
    // testing a distord U
    xtest1 = matrix([1, 0, 0, 0, 1;
    1, 1, 0, 0, 1;
    1, 0, 0, 0, 1;
    1, 0, 0, 0, 1;
    1, 0, 0, 0, 1;
    1, 0, 0, 0, 1;
    0, 1, 1, 1, 1]',-1,1);
    y = ann_FF_run(xtest1,N,W)
    disp('Testing a distored U')
    disp(y) //should get something close to t_u ~ [0 1]
    
    //testing something different from T and U. should get nothing
    xtest2 = matrix([1, 0, 0, 0, 1;
    1, 1, 0, 0, 1;
    1, 0, 1, 0, 1;
    1, 0, 1, 0, 1;
    0, 0, 1, 1, 1;
    0, 0, 1, 0, 1;
    0, 1, 1, 1, 1]',-1,1);
    y = ann_FF_run(xtest2,N,W)
    disp('Testing something neither T nor U')
    disp(y)
    

    和scilab控制台的输出

     Testing the traing set   
    
        0.8538757    0.1075397  
        0.1393287    0.8957439  
    
     Testing a distored U   
    
        0.1078667  
        0.9007755  
    
     Testing something neither T nor U   
    
        0.3433933  
        0.6306797