1. 程式人生 > >【MATLAB深度學習】神經網絡與分類問題

【MATLAB深度學習】神經網絡與分類問題

簡單 clas alpha 學習 exp 輸出 問題 分類 ros

神經網絡與分類問題

1.多元分類

  根據分類的數量確定輸出節點的數量是最可能得到良好效果的方法。輸出的類別表示可以使用one-hot編碼。通常情況下,二分類使用Sigmoid函數,多元分類使用Softmax函數。Softmax函數不僅考慮輸入的加權和,而且考慮其他輸出節點的輸出。正確地詮釋神經網絡多元分類的輸出結果需要考慮所有節點輸出的相對大小。Softmax函數保證輸出值之和為1。其也適用於二分類。

  多元分類程序示例,輸入數據為5個5*5矩陣,分別表示1,2,3,4,5。網絡結構為輸入節點25個,輸出節點5個,隱含節點50個。代碼如下:

function [W1, W2] = MultiClass(W1, W2, X, D)
  alpha = 0.9;
  
  N = 5;  
  for k = 1:N
    x = reshape(X(:, :, k), 25, 1); % k表示第k幅圖,25*1向量
    d = D(k, :)‘;
    
    v1 = W1*x;
    y1 = Sigmoid(v1);
    v  = W2*y1;
    y  = Softmax(v);
    
    e     = d - y;
    delta = e;

    e1     = W2‘*delta;
    delta1 = y1.*(1-y1).*e1; 
    
    dW1 = alpha*delta1*x‘;
    W1 = W1 + dW1;
    
    dW2 = alpha*delta*y1‘;   
    W2 = W2 + dW2;
  end
end

  Softmax函數定義如下:

function y = Softmax(x)
  ex = exp(x);
  y  = ex / sum(ex);
end

  Sigmoid函數定義如下:

function y = Sigmoid(x)
  y = 1 ./ (1 + exp(-x));
end

  測試代碼如下:

clear all

rng(3);

X  = zeros(5, 5, 5);
 
X(:, :, 1) = [ 0 1 1 0 0;
               0 0 1 0 0;
               0 0 1 0 0;
               0 0 1 0 0;
               0 1 1 1 0
             ];
 
X(:, :, 2) = [ 1 1 1 1 0;
               0 0 0 0 1;
               0 1 1 1 0;
               1 0 0 0 0;
               1 1 1 1 1
             ];
 
X(:, :, 3) = [ 1 1 1 1 0;
               0 0 0 0 1;
               0 1 1 1 0;
               0 0 0 0 1;
               1 1 1 1 0
             ];

X(:, :, 4) = [ 0 0 0 1 0;
               0 0 1 1 0;
               0 1 0 1 0;
               1 1 1 1 1;
               0 0 0 1 0
             ];
         
X(:, :, 5) = [ 1 1 1 1 1;
               1 0 0 0 0;
               1 1 1 1 0;
               0 0 0 0 1;
               1 1 1 1 0
             ];

D = [ 1 0 0 0 0;
      0 1 0 0 0;
      0 0 1 0 0;
      0 0 0 1 0;
      0 0 0 0 1
    ];
      
W1 = 2*rand(50, 25) - 1;
W2 = 2*rand( 5, 50) - 1;

for epoch = 1:10000           % train
  [W1 W2] = MultiClass(W1, W2, X, D);
end

N = 5;                        % inference
for k = 1:N
  x  = reshape(X(:, :, k), 25, 1);
  v1 = W1*x;
  y1 = Sigmoid(v1);
  v  = W2*y1;
  y  = Softmax(v)
end

  最終得到了正確的分類

2.微汙染的多元分類示例

  真實數據未必與訓練數據相符,用微微汙染的數據簡單檢驗一下上面所構建的神經網絡。代碼如下:

clear all

TestMultiClass;                 % W1, W2

X  = zeros(5, 5, 5);
 
X(:, :, 1) = [ 0 0 1 1 0;
               0 0 1 1 0;
               0 1 0 1 0;
               0 0 0 1 0;
               0 1 1 1 0
             ];
 
X(:, :, 2) = [ 1 1 1 1 0;
               0 0 0 0 1;
               0 1 1 1 0;
               1 0 0 0 1;
               1 1 1 1 1
             ];
 
X(:, :, 3) = [ 1 1 1 1 0;
               0 0 0 0 1;
               0 1 1 1 0;
               1 0 0 0 1;
               1 1 1 1 0
             ];
         
X(:, :, 4) = [ 0 1 1 1 0;
               0 1 0 0 0;
               0 1 1 1 0;
               0 0 0 1 0;
               0 1 1 1 0
             ];     
         
X(:, :, 5) = [ 0 1 1 1 1;
               0 1 0 0 0;
               0 1 1 1 0;
               0 0 0 1 0;
               1 1 1 1 0
             ];    
         
N = 5;                        % inference
for k = 1:N
  x  = reshape(X(:, :, k), 25, 1);
  v1 = W1*x;
  y1 = Sigmoid(v1);
  v  = W2*y1;
  y  = Softmax(v)
end

  輸出結果為 [0.0208,0.0006,0.0363,0.9164,0.0259] , [0.0000,0.9961,0.0038,0.0000,0.0000] ,[0.0001,0.0198,0.9798,0.0001,0.0002] ,[0.0930,0.3057,0.5397,0.0408,0.0208] ,[0.0363,0.3214,0.0717,0.0199,0.5506]。

  以上代碼中rng函數為:

function rng(x)
  randn(‘seed‘, x)
  rand(‘seed‘, x)
end

  






【MATLAB深度學習】神經網絡與分類問題