2014-02-24 24 views
5

過去我在這個網站上提出了一些關於神經網絡的問題,並得到了很好的答案,但我仍然努力爲自己實現一個。這是一個相當長的問題,但我希望它可以作爲其他人在MATLAB中創建自己的基本神經網絡的指導,所以它應該是值得的。在MATLAB中從頭開始編程一個基本的神經網絡

我到目前爲止所做的工作可能完全錯誤。我正在按照Andrew Y. Ng教授的在線stanford機器學習課程學習,並盡力實施他所教授的內容,盡我所能。

您可以告訴我,我的代碼的前饋和成本函數部分是否正確,以及我在最小化(優化)部分出錯的位置?

我有一個飼料2層前饋神經網絡。

爲前饋部分的MATLAB代碼是:

function [ Y ] = feedforward2(X,W1,W2) 
%This takes a row vector of inputs into the neural net with weight matrices W1 and W2 and returns a row vector of the outputs from the neural net 

%Remember X, Y, and A can be vectors, and W1 and W2 Matrices 

X=transpose(X);   %X needs to be a column vector 
A = sigmf(W1*X,[1 0]);  %Values of the first hidden layer 
Y = sigmf(W2*A,[1 0]);  %Output Values of the network 
Y = transpose(Y);   %Y needs to be a column vector 

因此,例如,具有兩個輸入和兩個輸出的兩層神經網絡看起來有點像這樣:

 a1 
x1 o--o--o y1  (all weights equal 1) 
    \/ \/ 
    /\ /\ 
x2 o--o--o y2 
     a2 

如果我們把:

X=[2,3]; 
W1=ones(2,2); 
W2=ones(2,2); 

Y = feedforward2(X,W1,W2) 

我們得到th È輸出:

Y = [0.5,0.5] 

這代表了神經網絡

的MATLAB爲平方誤差代價函數代碼的圖中所示的Y1和Y2的值是:

function [ C ] = cost(W1,W2,Xtrain,Ytrain) 
%This gives a value seeing how close W1 and W2 are to giving a network that represents the Xtrain and Ytrain data 
%It uses the squared error cost function 
%The closer the cost is to zero, the better these particular weights are at giving a network that represents the training data 
%If the cost is zero, the weights give a network that when the Xtrain data is put in, The Ytrain data comes out 

M = size(Xtrain,1); %Number of training examples 

oldsum = 0; 

for i = 1:M, 
     H = feedforward2(Xtrain,W1,W2); 
     temp = (H(i) - Ytrain(i))^2; 
     Sum = temp + oldsum; 
     oldsum = Sum; 
end 

C = (1/2*M) * Sum; 

end 

示例

例如,如果訓練數據是:

Xtrain =[0,0;  Ytrain=[0/57; 
     1,2;   3/57; 
     4,1;   5/57; 
     5,2;   7/57;               a1  
     3,4;   7/57; %This will be for a two input one output network x1 o--o y1 
     5,3;   8/57;               \/ \_o 
     1,5;   6/57;               /\/
     6,2;   8/57;              x2 o--o  
     2,1;   3/57;               a2  
     5,5;]   10/57;] 

我們先從初始隨機權

W1=[2,3;  W2=[3,2] 
    4,1] 

如果我們把:

Y= feedforward2([6,2],W1,W2) 

我們得到

Y = 0.9933 

遠遠從訓練數據說什麼它應該是(8/57 = 0.1404)。所以最初的隨機權重W1和W2是一個糟糕的猜測。

爲了測量隨機權權重/好猜測究竟有多壞是我們使用的成本函數:

C= cost(W1,W2,Xtrain,Ytrain) 

這給值:

C = 6.6031e+003 

最小化成本函數

如果我們通過搜索所有可能的變量W1和W2來最小化成本函數,然後選擇最低的,旨意給最佳地接近訓練數據

但是當我使用該代碼的網絡:

[W1,W2]=fminsearch(cost(W1,W2,Xtrain,Ytrain),[W1,W2]) 

它給出了一個錯誤信息。它說:「使用horzcat的錯誤.CAT參數維度不一致。」爲什麼我得到這個錯誤,我能做些什麼來解決它?


能否請你告訴我,如果前進的飼料和我的代碼的成本函數部分是正確的,我所在的地方的最小化(優化)部分的問題呢?

謝謝!!!

回答

2

你的神經網絡看起來沒問題,但是如果你正在對標記數據進行訓練,那麼你試圖做的訓練效率會非常低。在這種情況下,我會建議尋找到Back-propagation

關於你的錯誤,當訓練:你的錯誤消息的問題提示:dimensions are not consistent

作爲參數x0fminsearch這對於優化初始猜測,你送[W1, W2]但從我所看到的,這些矩陣沒有相同數量的行,因此您不能像這樣將它們添加到一起。我會建議修改你的成本函數來將一個向量作爲參數,然後爲這個向量的不同層形成你的權重向量。

您也沒有正確地爲fminsearch正確提供成本函數,因爲您只是用w1,w2,Xtrain和Ytrain就地評估cost

按照documentation(它已經多年,因爲我用Matlab的),它看起來像你通過指針成本函數作爲 fminsearch(cost, [W1; W2])

編輯:你可以表達你的體重和修改代碼如下:

global Xtrain 
global Ytrain 
W = [W1; W2] 
fminsearch(cost, W) 

成本函數必須被修改,使得它不會採取Xtrain,Ytrain作爲輸入,因爲fminsearch將嘗試以優化這些呢。修改你的成本函數是這樣的:

function [ C ] = cost(W) 
    W1 = W[1:2,:] 
    W2 = W[3,:] 
    global Xtrain 
    global Ytrain 
    ... 
+0

謝謝你的回答。 「修改你的成本函數,將一個向量作爲參數,然後爲這個向量的不同層形成你的權重向量。」我將如何做到這一點?我不知道如何從一個總和變成一個向量方程,我不明白你的意思是「從這個向量的不同層次構成你的權重向量」。 – Blue7

+0

查看更新的答案。 – PureW