Hopfield神經網絡使用說明。
該神經網絡有兩個特點:
1,輸出值只有0,1
2,Hopfield沒有輸入(input)
這里解釋一下第二個特點,什么叫沒有輸入?因為在使用Hopfield網絡的時候,多用於圖像仿真,圖像仿真意思就是先給你一些標准的圖像,
比如1~9的數字,然后用一些別的測試圖像(模糊不清,讓人識別基本靠半猜半看)去逼近標准圖像。而所謂的沒有輸入,意思就是指,你輸入的圖像就是
輸出結果,那么Hopfield就認為沒有輸入。MATLAB官方說明:Since Hopfield networks have no inputs……balabala
接着我們實際使用該神經來仿真數字圖片,例子運用網絡上的識別“1,2”的例子,但具體解釋為原創,也在這里摒棄一些網上以訛傳訛的錯誤解釋。
Hopfield調用流程:
1 newhop()
函數功能:創建一個離散型Hopfield網絡。
調用格式:net=newhop(T);
這里T代表輸出,上文解釋過,為什么T不是輸入,T代表M*N的矩陣
2,sim()函數
% 調用格式:[Y,Af,E,perf] = sim(net,P,[],Ai,T)
% [Y,Af,E,perf] = sim(net,{Q TS},Ai,T)
%
% P,Q:測試向量的個數;
% Ai:初始的層延時,默認為0;
% T:測試向量;
% TS:測試的步數;
% Y:網絡的輸出矢量;
% Af:訓練終止時的層延遲狀態;
% E:誤差矢量;
% perf:網絡的性能。
這是摘自網絡的解釋,顯然不夠明確,我們用matlab是help例子說明。
Here we create a Hopfield network with two three-element
stable points T.
創建T的Hopfield網絡
T = [-1 -1 1; 1 -1 1]';
net = newhop(T);
Below we check that the network is stable at these points by
using them as initial layer delay conditions. If the network is
stable we would expect that the outputs Y will be the same.
(Since Hopfield networks have no inputs, the second argument
to SIM is Q = 2 when using matrix notation).
我們用初始化數據檢查構建的網絡,如果網絡是穩定的,則我們輸出的Y就是輸入的Y。
Ai = T;
[Y,Pf,Af] = sim(net,2,[],Ai);
Y
補充,sim的四個參數:
net——表示newhop建立的網絡
2——表示T的列數(樣本維度),這里的T是轉置后的4*2維
[]——表示輸入的T是矩陣形式
Ai——需要仿真的數據,這里表示用原數據測試
ps:用矩陣形式,默認神經元是一層,即Ts步長為1
接下來我們要調用多步長的方法。
To see if the network can correct a corrupted vector, run
the following code which simulates the Hopfield network for
five timesteps. (Since Hopfield networks have no inputs,
the second argument to SIM is {Q TS} = [1 5] when using cell
array notation.)
用多步長的方法,就必須要用matlab的胞元結構(cell)
這里說明一下胞元的用法:
X={x1,x2,x3……},xi可以為任何數據,也可以是cell
調用X(1)表示取到x1,可以理解為取到了一個對象;而X{1}則是x1的具體值(http://jingyan.baidu.com/article/20095761997932cb0721b485.html)
Ai = {[-0.9; -0.8; 0.7]};
[Y,Pf,Af] = sim(net,{1 5},{},Ai);
Y{1}
參數說明:
Ai——把3*1的向量封裝為cell結構
{1,5}——1表示Ai的維度1,5表示用步長5,即5層神經元
{}——表示告訴函數,數據以cell格式傳遞進來
Ai——用原數據測試網絡
重要說明sim仿真函數是橫向仿真,就是以列數為仿真單位,例如在下文代碼中T是數字“1“,”2”的圖片合並,圖片格式為12*10,合並后為24*10.
但在仿真前,由於仿真方向按照列為單位,需要轉置為10*24,可以形象理解把圖片橫放后排列。
因此,在設置維度以12(一幅圖)為單位。
函數介紹完畢,來分析一下網上給出的數字識別代碼:
% ------------------------------number array--------------------------------- one=[-1 -1 -1 -1 -1 -1 -1 -1 -1 -1;... -1 -1 -1 -1 1 1 -1 -1 -1 -1;-1 -1 -1 -1 1 1 -1 -1 -1 -1;... -1 -1 -1 -1 1 1 -1 -1 -1 -1;-1 -1 -1 -1 1 1 -1 -1 -1 -1;... -1 -1 -1 -1 1 1 -1 -1 -1 -1;-1 -1 -1 -1 1 1 -1 -1 -1 -1;... -1 -1 -1 -1 1 1 -1 -1 -1 -1;-1 -1 -1 -1 1 1 -1 -1 -1 -1;... -1 -1 -1 -1 1 1 -1 -1 -1 -1;-1 -1 -1 -1 1 1 -1 -1 -1 -1;... -1 -1 -1 -1 -1 -1 -1 -1 -1 -1]; two=[-1 -1 -1 -1 -1 -1 -1 -1 -1 -1;... -1 1 1 1 1 1 1 1 1 -1;-1 1 1 1 1 1 1 1 1 -1;... -1 -1 -1 -1 -1 -1 -1 1 1 -1;-1 -1 -1 -1 -1 -1 -1 1 1 -1;... -1 1 1 1 1 1 1 1 1 -1;-1 1 1 1 1 1 1 1 1 -1;... -1 1 1 -1 -1 -1 -1 -1 -1 -1;-1 1 1 -1 -1 -1 -1 -1 -1 -1;... -1 1 1 1 1 1 1 1 1 -1;-1 1 1 1 1 1 1 1 1 -1;... -1 -1 -1 -1 -1 -1 -1 -1 -1 -1]; % --------------------------plot standard number figure-------------- subplot(2,3,1) imshow(imresize(one,20)) title('standard number') subplot(2,3,4) imshow(imresize(two,20)) title('standard number') % ---------------------------creat hopfield net--------------------- T=[one;two]'; net=newhop(T); % --------------------------generate rand noise------------------ for i=2:11 for j=2:9 a=rand; if a<=0.1 one(i,j)=-one(i,j); two(i,j)=-two(i,j); end end end noise_one=one noise_two=two % -------------------------plot noise figure---------------------------- subplot(2,3,2) imshow(imresize(noise_one,20)) title('noise number') subplot(2,3,5) imshow(imresize(noise_two,20)) title('noise number')
上述代買為顯示圖片和加入噪聲后顯示圖片,沒什么分析的必要。
% ------------------------plot identify figure---------------------------
noise1={(noise_one)'};%把圖變為cell結構,並轉置(橫放圖片排列) tu1=sim(net,{12,3},{},noise1);%每12像素為一張圖subplot(2,3,3)
%轉回圖片並顯示,這里補充一下,{3}表示有三層神經網絡輸出,取第三層輸出值
imshow(imresize(tu1{3}',20))%放大20倍 title('identify number') noise2={(noise_two)'}; tu2=sim(net,{12,3},{},noise2); tu2{3}' subplot(2,3,6) imshow(imresize(tu2{3}',20)) title('identify number')
延展:其實根據sim函數的說明,以下兩端代碼效果是一樣的,但用cell只能是一層神經網絡
%用cell
noise1={(noise_one)'}; tu1=sim(net,{12,1},{},noise1); %tu1{3}' subplot(2,3,3) imshow(imresize(tu1{1}',20)) title('identify number')
%用矩陣
noise1=(noise_one)'; tu1=sim(net,12,[],noise1); %tu1{3}' subplot(2,3,6) imshow(imresize(tu1',20)) title('identify number')