簡體   English   中英

MATLAB中的神經網絡成本函數

[英]Neural Network Cost Function in MATLAB

我如何在matlab中實現這個神經網絡成本函數:

神經網絡成本函數

以下是符號代表的含義:

% m is the number of training examples.   [a scalar number]
% K is the number of output nodes.   [a scalar number]
% Y is the matrix of training outputs.   [an m by k matrix]
% y^{(i)}_{k} is the ith training output (target) for the kth output node.   [a scalar number]
% x^{(i)} is the ith training input.   [a column vector for all the input nodes]
% h_{\theta}(x^{(i)})_{k} is the value of the hypothesis at output k, with weights theta, and training input i.   [a scalar number]

%note: h_{\theta}(x^{(i)}) will be a column vector with K rows.

我遇到了嵌套和,偏置節點以及這個等式的一般復雜性的問題。 我也在苦苦掙扎,因為有兩個權重矩陣,一個將輸入連接到隱藏層,另一個連接隱藏層和輸出。 到目前為止,這是我的嘗試。

定義變量

m = 100            %number of training examples
K = 2              %number of output nodes
E = 2              %number of input nodes
A = 2              %number of nodes in each hidden layer
L = 1              %number of hidden layers

Y = [2.2,   3.5    %targets for y1 and y2 (see picture at bottom of page)
     1.7,   2.1
     1.9,   3.6
      .     .      %this is filled out in the actual code but to save space I have used ellipsis. there will be m rows.
      .     .
      .     .
     2.8,   1.6]

X = [1.1,   1.8    %training inputs. there will be m rows
     8.5,   1.0
     9.5,   1.8
      .     .
      .     .
      .     . 
     1.4,   0.8]

W1 = [1.3,  .    .  0.4    %this is just an E by A matrix of random numbers. this is the matrix of initial weights.
       .    .    .  - 2
       .    .    .  3.1
       .    .    .  - 1
      2.1, -8, 1.2, 2.1]

W2 = [1.3,  .    .  0.4    %this is an A by K matrix of random numbers. this is the matrix of initial weights.
       .    .    .  - 2
       .    .    .  3.1
       .    .    .  - 1
      2.1, -8, 1.2, 2.1]

使用這些權重的假設等於......

Htheta = sigmf( dot(W2 , sigmf(dot(W1 , X))) )   %This will be a column vector with K rows.

使用這些權重的成本函數等於...(這是我在努力的地方)

  sum1 = 0
  for i = 1:K
  sum1 = sum1 + Y(k,i) *log(Htheta(k)) + (1 - Y(k,i))*log(1-Htheta(k))

我只是繼續寫這樣的事情,然后意識到這一切都是錯的。 我不能為我的生活弄清楚如何做嵌套的總和,或包括輸入矩陣,或做任何一個。 這一切都非常復雜。

我如何在matlab中創建這個等式?

非常感謝你!

具有2個輸入,2個輸出,2個隱藏節點和2個偏置單元的2層神經網絡http://imagizer.imageshack.us/v2/320x240q90/40/92bn.jpg

注意:代碼有奇怪的顏色,因為stackoverflow不知道我在MATLAB中編程。 我還將代碼直接寫入stackoverflow,因此它可能有語法錯誤。 我對如何進行此操作的一般概念更感興趣,而不僅僅是復制和粘貼代碼。 這就是我沒有打過半冒號等原因的原因。

我使用與你上面提到的相同的錯誤函數實現了神經網絡。 不幸的是,我還沒有使用過Matlab很長一段時間,但我對Octave非常熟練,希望你仍然可以找到有用的東西,因為Octave中的許多函數都與Matlab類似。

@sashkello為計算成本函數提供了很好的代碼片段。 但是,這段代碼是用循環結構編寫的,我想提供一個矢量化實現。

為了評估當前的θ值,我們需要在整個網絡中執行前饋/ forward propagation 我假設你知道如何編寫前饋代碼,因為你只關心J(theta)錯誤。 讓代表前向傳播結果的向量為F

一旦你執行了前饋,你就需要執行這個等式。 注意,我是以矢量化的方式實現它。

J = (-1/m) * sum(sum(Y .* log(F) + (1-Y) .* log(1-F),2));

這將計算有關的總和部分:

總費用的第1部分

現在我們必須添加正則化術語,即:

通常情況下,我們會有任意數量的θ矩陣,但在這種情況下我們有2個,所以我們可以只執行幾個求和:

J =J + (lambda/(2*m)) * (sum(sum(theta_1(:,2:end).^2,2)) + sum(sum(theta_2(:,2:end).^2,2)));

請注意,在每個總和中,我只是從第二列到其余部分工作。 這是因為第一列將對應於我們為“偏置單位”訓練的theta值。

所以有一個J的計算的矢量化實現。

我希望這有幫助!

我認為Htheta是K * 2陣列。 請注意,您需要在遠期成本函數計算中添加偏差( x0a0 )。 假設您在輸入,隱藏和輸出層有兩個節點作為代碼中的注釋,我向您展示了每個步驟中的數組維度。

m = size(X, 1);  
X = [ones(m,1) X]; % m*3 in your case
% W1 2*3, W2 3*2
a2 = sigmf(W1 * X');  % 2*m
a2 = [ones(m,1) a2'];  % m*3    
Htheta = sigmf(a2 * W2);  % m*2    

J = (1/m) * sum ( sum (  (-Y) .* log(Htheta)  -  (1-Y) .* log(1-Htheta) ));

t1 = W1(:,2:size(W1,2));
W2 = W2';
t2 = W2(:,2:size(W2,2));

% regularization formula
Reg = lambda  * (sum( sum ( t1.^ 2 )) + sum( sum ( t2.^ 2 ))) / (2*m);

好吧,據我所知,你的問題與神經網絡無關,但基本上問如何在matlab中進行嵌套求和。 我真的不想輸入上面的整個等式,但是,即第一個總和的第一部分將如下所示:

Jtheta = 0
for i=1:m,
    for j=1:K,
        Jtheta = Jtheta + Y(i, j) * log(Htheta(X(i))(j)) 
    end
end

Jtheta是你的結果。

這適用於任意數量的隱藏層:

% Allow arbitrary network architectures. Create cell array of all Theta parameters
Theta={Theta1; Theta2};

% Compute unregularised cost (J)
J = 1/m * sum(sum((-y .* log(hX) - (1 - y) .* log(1 - hX))));

% Add regularisation
for i = 1:length(Theta)
  J += lambda / 2 / m * sum(sum(Theta{i}(:,2:end) .^ 2));
end   

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM