2017-12-27 165 views
0

認識我試着運行這個例子,它採用神經網絡工具箱的Scilab https://burubaxair.wordpress.com/2014/03/12/artificial-neural-networks-in-scilab/神經網絡的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矩陣中,但我不明白爲什麼。你能告訴我做錯了什麼嗎?謝謝!

回答

0

你讓你的代碼2級的錯誤:

  1. 你不應該混合測試和訓練集。
  2. 測試輸入必須是單列。

您的第一個錯誤是x = [ 1.... ],因爲它包含一個圖像,而您在N中指定您有兩個輸出神經元。 正如例子說,你應該有x = [T,U];

你的第二個錯誤是給X作爲測試ann_FF_run。該功能將測試輸入作爲單個列。但是因爲你在x是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