1. 程式人生 > >機器學習與演算法(6)--學習向量化

機器學習與演算法(6)--學習向量化

                                                           學習向量化                        

學習向量量化(Learning Vector Quantization,簡稱LVQ),與1988年提出的一種用於模式分類的有監督學習演算法,是一種結構簡單、功能強大的有監督式神經網路分類演算法。典型的學習向量量化演算法有:LVQ1、LVQ2、LVQ3,其中,前兩種演算法應用較為廣泛,尤其以LVQ2應用最為廣泛和有效。

學習向量量化是一種結構簡單、功能強大的有監督式神經網路分類方法。作為一種最近鄰原型分類器,LVQ在訓練過程中通過對神經元權向量(原型向量)的不斷更新,對其學習率的不斷調整,能夠使不同類別權向量之間的邊界逐步收斂至貝葉斯分類邊界。演算法中,對獲勝神經元(最近鄰權向量)的選取是通過計算輸入樣本和權向量之間的距離的大小來判斷的。與向量量化(VQ)相比,LVQ最突出的特點就是其具有自適應性

1.向量量化 向量量化的思路是,將高維輸入空間分成若干不同的區域,對每個區域確定一箇中心向量作為聚類的中心,與其處於同一區域的輸入向量可用該中心向量來代表,從而形成了以各中心向量為聚類中心的點集。在影象處理領域常用各區域中心點(向量)的編碼代替區域內的點來儲存或傳輸,從而提出了各種基於向量量化的有失真壓縮技術,在二維輸入平面上表示的中心向量分佈稱為Voronoi圖,如圖所示:

自組織對映可以起到聚類作用,但無法直接分類或識別,因此它只是自適應解決模式分類問題兩步中的第一步。第二步:學習向量量化,採用監督機制,在訓練中加入訊號作為分類資訊對權值進行細調,並對輸出神經元預先指定其類別。

2.學習向量量化網路結構與工作原理 學習向量量化神經網路有三層組成:輸入層,競爭層,線性輸出層。

競爭層有m個神經元,輸入層有n個神經元,兩層之間完全連線。輸出層每個神經元只與競爭層中的一組神經元連線,連線權重固定為1,訓練過程中輸入層和競爭層之間的權值逐漸被調整為聚類中心。當一個樣本輸入LVQ網路時,競爭層的神經元通過勝者為王學習規則產生獲勝神經元,容許其輸出為1,其它神經元輸出為0。與獲勝神經元所在組相連的輸出神經元輸出為1,而其它輸出神經元為0,從而給出當前輸入樣本的模式類。將競爭層學習得到的類成為子類,而將輸出層學習得到的類成為目標類。

3.學習向量量化網路學習演算法 學習向量量化學習規則結合了競爭學習規則和有導師學習規則,所以樣本集應當為{(xi,di)}。其中di為l維,對應輸出層的l個神經元,它只有一個分量為1,其他分量均為0。通常把競爭層的每個神經元指定給一個輸出神經元,相應的權值為1,從而得到輸出層的權值。比如某LVQ網路競爭層6個神經元,輸出層3個神經元,代表3類。若將競爭層的1,3指定為第一個輸出神經元,2,5指定為第二個輸出神經元,3,6指定為第三個輸出神經元。則競爭層到輸出層的權值矩陣為:

訓練前預先定義好競爭層到輸出層權重,從而指定了輸出神經元類別,訓練中不再改變。網路的學習通過改變輸入層到競爭層的權重來進行。根據輸入樣本類別和獲勝神經元所屬類別,可判斷當前分類是否正確。若分類正確,則將獲勝神經元的權向量向輸入向量方向調整,分類錯誤則向相反方向調整。

學習向量量化網路學習演算法的步驟如下:

  • 初始化。競爭層各神經元權值向量隨機賦值小隨機數,確定初始學習速率和訓練次數。
  • 輸入樣本向量。
  • 尋找啟用神經元。
  • 根據分類是否正確按照不同規則調整獲勝神經元的權值,當網路分類結果與教師訊號一致時,向輸入樣本方向調整權值:

當網路分類結果與教師訊號不一致時,向輸入樣本反方向調整權值:

其他非啟用神經元權值保持不變。 * 更新學習速率

* 當訓練次數未達到設定的次數時,轉到步驟輸入樣本向量輸入下一個樣本,重複各步驟直到達到設定訓練次數為止。上述訓練過程中,要保證η(t)為單調下降函式。

學習向量量化網路是SOFM網路一種有監督形式的擴充套件,兩者有效結合可更好地發揮競爭學習和有監督學習的優點。

連線方式:

輸入層與競爭層之間採用全連線的方式,競爭層與線性輸出層之間採用部分連線的方式。競爭層神經元個數總是大於線性輸出層神經元個數,每個競爭層神經元只與一個線性輸出層神經元相連線且連線權值恆為1。但是,每個線性輸出層神經元可以與多個競爭層神經元相連線。競爭層神經元與線性輸出層神經元的值只能是1或0。 當某個輸入模式被送至網路時,與輸入模式距離最近的競爭層神經元被啟用,神經元的狀態為“1”,而其他競爭層神經元的狀態均為“0”。因此,與被啟用神經元相連線的線性輸出層神經元狀態也為“1”,而其他線性輸出層神經元的狀態均為“0”

基本步驟為:

初始化輸入層與競爭層之間的權值W_ij及學習率η(η>0)。 將輸入向量送入到輸入層,並計算競爭層神經元與輸入向量的距離: 

應用案例

function [dw,ls] = learnlv3(w,p,z,n,a,t,e,gW,gA,d,lp,ls)
%LEARNLV2 LVQ2 weight learning function.
%
% Syntax
% 
% [dW,LS] = learnlv3(w,p,n,a,T,lp,ls,Ttrain,C)
% info = learnlv2(code)
%
% Description
%
% LEARNLV3 is the OLVQ weight learning function.
%
% LEARNLV2(W,P,Z,N,A,T,E,gW,gA,D,LP,LS) takes several inputs,
% W - SxR weight matrix (or Sx1 bias vector).
% P - RxQ input vectors (or ones(1,Q)).
% Z - SxQ weighted input vectors.
% N - SxQ net input vectors.
% A - SxQ output vectors.
% T - SxQ layer target vectors.
% E - SxQ layer error vectors.
% gW - SxR weight gradient with respect to performance.
% gA - SxQ output gradient with respect to performance.
% D - SxS neuron distances.
% LP - Learning parameters, none, LP = [].
% LS - Learning state, initially should be = [].
% and returns,
% dW - SxR weight (or bias) change matrix.
% LS - New learning state.
%
% Learning occurs according to LEARNLV1's learning parameter,
% shown here with its default value.
% LP.lr - 0.01 - Learning rate
%
% LEARNLV2(CODE) returns useful information for each CODE string:
% 'pnames' - Returns names of learning parameters.
% 'pdefaults' - Returns default learning parameters.
% 'needg' - Returns 1 if this function uses gW or gA.
%
% Examples
%
% Here we define a sample input P, output A, weight matrix W, and
% output gradient gA for a layer with a 2-element input and 3 neurons.
% We also define the learning rate LR.
%
% p = rand(2,1);
% w = rand(3,2);
% n = negdist(w,p);
% a = compet(n);
% gA = [-1;1; 1];
% lp.lr = 0.5;
%
% Since LEARNLV2 only needs these values to calculate a weight
% change (see Algorithm below), we will use them to do so.
%
% dW = learnlv3(w,p,n,a,lp,Ttrain,C)
%
% Network Use
%
% You can create a standard network that uses LEARNLV2 with NEWLVQ.
%
% To prepare the weights of layer i of a custom network
% to learn with LEARNLV1:
% 1) Set NET.trainFcn to 'trainwb1'.
% (NET.trainParam will automatically become TRAINWB1's default parameters.)
% 2) Set NET.adaptFcn to 'adaptwb'.
% (NET.adaptParam will automatically become TRAINWB1's default parameters.)
% 3) Set each NET.inputWeights{i,j}.learnFcn to 'learnlv2'.
% Set each NET.layerWeights{i,j}.learnFcn to 'learnlv2'.
% (Each weight learning parameter property will automatically
% be set to LEARNLV2's default parameters.)
%
% To train the network (or enable it to adapt):
% 1) Set NET.trainParam (or NET.adaptParam) properties as desired.
% 2) Call TRAIN (or ADAPT).
%
% Algorithm
%
% LEARNLV3 calculates the weight change dW for a given neuron from
% the neuron's input P, output A, train vector target T train, output
% conexion matrix C and learning rate LR
% according to the OLVQ rule, given i the index of the neuron whose
% output a(i) is 1:
%
% dw(i,:) = +lr*(p-w(i,:)) if C(:,i) = Ttrain
% = -lr*(p-w(i,:)) if C(:,i) ~= Ttrain
%
% if C(:,i) ~= Ttrain then the index j is found of the neuron with the
% greatest net input n(k), from the neurons whose C(:,k)=Ttrain. This
% neuron's weights are updated as follows:
%
% dw(j,:) = +lr*(p-w(i,:))
%
% See also LEARNLV1, ADAPTWB, TRAINWB, ADAPT, TRAIN.

% Mark Beale, 11-31-97
% Copyright (c) 1992-1998 by The MathWorks, Inc.
% $Revision: 1.1.1.1 $

% FUNCTION INFO
% =============
if isstr(w)
  switch lower(w)
  case 'name'
      dw = 'Learning Vector Quantization 3';
  case 'pnames'
    dw = {'lr';'window'};
  case 'pdefaults'
    lp.lr = 0.01;
    lp.window = 0.25;
    dw = lp;
  case 'needg'
    dw = 1;
  otherwise
    error('NNET:Arguments','Unrecognized code.')
  end
  return
end


% CALCULATION
% ===========

[S,R] = size(w);
Q = size(p,2);
pt = p';
dw = zeros(S,R);
% For each q...
for q=1:Q

  % Find closest neuron k1 
  nq = n(:,q);
  k1 = find(nq == max(nq));
  k1 = k1(1);

  % Find next closest neuron k2 
  nq(k1) = -inf;
  k2 = find(nq == max(nq));
  k2 = k2(1);


  % and if x falls into the window...
  d1 = abs(n(k1,q)); % Shorter distance
  d2 = abs(n(k2,q)); % Greater distance

  if d2/d1 > ((1-lp.window)/(1+lp.window))

      % then move incorrect neuron away from input,
      % and the correct neuron towards the input
      ptq = pt(q,:);
      if gA(k1,q) ~= gA(k2,q)
          % indicate the incorrect neuron with i, the other with j
          if gA(k1,q) ~= 0
              i = k1;
              j = k2;
          else
              i = k2;
              j = k1;
          end
          dw(i,:) = dw(i,:) - lp.lr*(ptq - w(i,:));
          dw(j,:) = dw(j,:) + lp.lr*(ptq - w(j,:));
      else
          dw(k1,:) = dw(k1,:) + 0.11*lp.window*(ptq-w(k1,:));
       % dw(k2,:) = dw(k2,:) + 0.11*lp.window*(ptq-w(k2,:));
      end
  end
end