2013-06-05 87 views
0

我正在與matlab neural network toolbox玩弄,我遇到了一些我沒想到的事情。我的問題特別是沒有隱藏層的分類網絡,只有1個輸入和tansig傳輸函數。所以我希望這個分類器在一個由學習輸入權重和偏差定義的點上劃分一維數據集。首先,我認爲用於計算給定輸入x的輸出y的公式爲:其中w是輸入權重,b是偏差,y = f(x * w + b) 。計算網絡輸出的正確公式是什麼?Matlab神經網絡給出了意想不到的結果

我還希望將整個數據集翻譯一定的值(+77)會對偏差和/或權重產生很大影響。但似乎並非如此。爲什麼數據集的翻譯對偏差和體重沒有太大影響?

這是我的代碼:

% Generate data 
p1 = randn(1, 1000); 
t1(1:1000) = 1; 
p2 = 3 + randn(1, 1000); 
t2(1:1000) = -1; 
% view data 
figure, hist([p1', p2'], 100) 

P = [p1 p2]; 
T = [t1 t2]; 

% train network without hidden layer 
net1 = newff(P, T, [], {'tansig'}, 'trainlm'); 
[net1,tr] = train(net1, P, T); 

% display weight and bias 
w = net1.IW{1,1}; 
b = net1.b{1,1}; 
disp(w) % -6.8971 
disp(b) % -0.2280 

% make label decision 
class_correct = 0; 
outputs = net1(P); 
for i = 1:length(outputs) 
    % choose between -1 and 1 
    if outputs(i) > 0 
     outputs(i) = 1; 
    else 
     outputs(i) = -1; 
    end 
    % compare 
    if T(i) == outputs(i) 
     class_correct = class_correct + 1; 
    end 
end 
% Calculate the correct classification rate (CCR) 
CCR = (class_correct * 100)/length(outputs); 
fprintf('CCR: %f \n', CCR); 
% plot the errors 
errors = gsubtract(T, outputs); 
figure, plot(errors) 

% I expect these to be equal and near 1 
net1(0)    % 0.9521 
tansig(0*w + b)  % -0.4680 

% I expect these to be equal and near -1 
net1(4)    % -0.9991 
tansig(4*w + b)  % -1 




% translate the dataset by +77 
P2 = P + 77; 

% train network without hidden layer 
net2 = newff(P2, T, [], {'tansig'}, 'trainlm'); 
[net2,tr] = train(net2, P2, T); 

% display weight and bias 
w2 = net2.IW{1,1}; 
b2 = net2.b{1,1}; 
disp(w2) % -5.1132 
disp(b2) % -0.1556 

我生成與具有不同的平均的正態分佈由2羣的人工數據集。我用直方圖繪製這些種羣,並用它訓練網絡。 我計算了正確的分類率,它是整個數據集的正確分類實例的百分比。這在92%左右,所以我知道分類器的工作原理。

但是,我期望net1(x)和tansig(x * w + b)給出相同的輸出,但事實並非如此。計算我的訓練網絡輸出的正確公式是什麼?

而且我預計net1和net2有不同的權重和/或偏差,因爲net2是在訓練net1的數據集的翻譯版本(+77)上訓練的。爲什麼數據集的翻譯對偏差和體重沒有太大影響?

+0

如果您對神經網絡感興趣,可以考慮加入機器學習網站:http://area51.stackexchange.com/proposals/41738/machine-learning – travisbartley

回答

1

首先,您的代碼會保留完整的默認MATLAB輸入預處理。你可以檢查此:

net2.inputs{1} 

當我把你的代碼中,我得到這個:

Neural Network Input 
       name: 'Input' 
    feedbackOutput: [] 
     processFcns: {'fixunknowns', removeconstantrows, 
        mapminmax} 
    processParams: {1x3 cell array of 2 params} 
    processSettings: {1x3 cell array of 3 settings} 
    processedRange: [1x2 double] 
    processedSize: 1 
      range: [1x2 double] 
       size: 1 
      userdata: (your custom info) 

的重要組成部分,是processFncs設置爲mapminmax。根據文檔,mapminmax將「通過將行最小值和最大值映射爲[-1 1]來處理矩陣」。這就是爲什麼你對輸入任意「移位」(重新抽樣)沒有任何效果。

我假設通過「計算輸出」你的意思是檢查你的網絡的性能。首先在數據集上模擬網絡(請參閱doc nnet/sim),然後使用perform函數檢查「正確性」。像你一樣會使用相同的成本函數,當訓練:

% get predictions 
[Y] = sim(net2,T); 
% see how well we did 
perf = perform(net2,T,Y); 

Boomshuckalucka。希望有所幫助。

+0

嘿,感謝您的回覆。我不知道mapminmax。 「計算輸出」我的意思是得到sim(net2,T)給出的輸出。我希望'sim​​(net2,4)','net2(4)'和'tansig(4 * w + b)'的輸出是相同的[文檔](http://www.mathworks .NL /幫助/ NNET/UG /多層神經網絡architecture.html)。前2個結果相同,但我的手動計算結果不同,我不知道爲什麼。也許'mapminmax'與它有關係? – Xochipilli

+0

嘿,我剛剛測試過它,它確實是'mapminmax'的錯。 如果在創建網絡後添加以下內容,則問題消失:'net1.inputs {1} .processFcns = {'fixunknowns','removeconstantrows'}; net1.outputs {1} .processFcns = {'removeconstantrows'};' 非常感謝,你不知道我一直在尋找答案多久! – Xochipilli