📄 demohop1.html
字号:
<!--This HTML is auto-generated from an m-file.Your changes will be overwritten.--><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="color:#990000; font-weight:bold; font-size:x-large">A Two Neuron Hopfield Network</p><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd">A Hopfield network consisting of two neurons is designed with two stableequilibrium points and simulated using the above functions.</p><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd">Copyright 1992-2002 The MathWorks, Inc.$Revision: 1.17 $ $Date: 2002/03/29 19:36:23 $</p><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="color:#990000; font-weight:bold; font-size:medium; page-break-before: auto;"><a name=""></a></p><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd">We would like to obtain a Hopfield network that has the two stable pointsdefined by the two target (column) vectors in T.</p><pre xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="position: relative; left:30px">T = [+1 -1; <span style="color:blue">...</span> -1 +1];</pre><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="color:#990000; font-weight:bold; font-size:medium; page-break-before: auto;"><a name=""></a></p><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd">Here is a plot where the stable points are shown at the corners. All possiblestates of the 2-neuron Hopfield network are contained within the plotsboundaries.</p><pre xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="position: relative; left:30px">plot(T(1,:),T(2,:),<span style="color:#B20000">'r*'</span>)axis([-1.1 1.1 -1.1 1.1])title(<span style="color:#B20000">'Hopfield Network State Space'</span>)xlabel(<span style="color:#B20000">'a(1)'</span>);ylabel(<span style="color:#B20000">'a(2)'</span>);</pre><img xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" src="demohop1_img03.gif"><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="color:#990000; font-weight:bold; font-size:medium; page-break-before: auto;"><a name=""></a></p><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd">The function NEWHOP creates Hopfield networks given the stable points T.</p><pre xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="position: relative; left:30px">net = newhop(T);</pre><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="color:#990000; font-weight:bold; font-size:medium; page-break-before: auto;"><a name=""></a></p><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd">First we check that the target vectors are indeed stable. We check this bygiving the target vectors to the Hopfield network. It should return the twotargets unchanged, and indeed it does.</p><pre xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="position: relative; left:30px">[Y,Pf,Af] = sim(net,2,[],T);Y</pre><pre xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="color:gray; font-style:italic;">Y = 1 -1 -1 1</pre><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="color:#990000; font-weight:bold; font-size:medium; page-break-before: auto;"><a name=""></a></p><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd">Here we define a random starting point and simulate the Hopfield network for20 steps. It should reach one of its stable points.</p><pre xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="position: relative; left:30px">a = {rands(2,1)};[y,Pf,Af] = sim(net,{1 20},{},a);</pre><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="color:#990000; font-weight:bold; font-size:medium; page-break-before: auto;"><a name=""></a></p><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd">We can make a plot of the Hopfield networks activity.</p><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd">Sure enough, the network ends up in either the upper-left or lower rightcorners of the plot.</p><pre xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="position: relative; left:30px">record = [cell2mat(a) cell2mat(y)];start = cell2mat(a);hold onplot(start(1,1),start(2,1),<span style="color:#B20000">'bx'</span>,record(1,:),record(2,:))</pre><img xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" src="demohop1_img07.gif"><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="color:#990000; font-weight:bold; font-size:medium; page-break-before: auto;"><a name=""></a></p><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd">We repeat the simulation for 25 more initial conditions.</p><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd">Note that if the Hopfield network starts out closer to the upper-left, it willgo to the upper-left, and vise versa. This ability to find the closest memoryto an initial input is what makes the Hopfield network useful.</p><pre xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="position: relative; left:30px">color = <span style="color:#B20000">'rgbmy'</span>;<span style="color:blue">for</span> i=1:25 a = {rands(2,1)}; [y,Pf,Af] = sim(net,{1 20},{},a); record=[cell2mat(a) cell2mat(y)]; start=cell2mat(a); plot(start(1,1),start(2,1),<span style="color:#B20000">'kx'</span>,record(1,:),record(2,:),color(rem(i,5)+1))<span style="color:blue">end</span></pre><img xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" src="demohop1_img08.gif"><originalCode xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" code="%% A Two Neuron Hopfield Network
% A Hopfield network consisting of two neurons is designed with two stable
% equilibrium points and simulated using the above functions.
%
% Copyright 1992-2002 The MathWorks, Inc.
% $Revision: 1.17 $ $Date: 2002/03/29 19:36:23 $

%%
% We would like to obtain a Hopfield network that has the two stable points
% defined by the two target (column) vectors in T.

T = [+1 -1; ...
 -1 +1];

%%
% Here is a plot where the stable points are shown at the corners. All possible
% states of the 2-neuron Hopfield network are contained within the plots
% boundaries.

plot(T(1,:),T(2,:),'r*')
axis([-1.1 1.1 -1.1 1.1])
title('Hopfield Network State Space')
xlabel('a(1)');
ylabel('a(2)');

%%
% The function NEWHOP creates Hopfield networks given the stable points T.

net = newhop(T);

%%
% First we check that the target vectors are indeed stable. We check this by
% giving the target vectors to the Hopfield network. It should return the two
% targets unchanged, and indeed it does.

[Y,Pf,Af] = sim(net,2,[],T);
Y

%%
% Here we define a random starting point and simulate the Hopfield network for
% 20 steps. It should reach one of its stable points.

a = {rands(2,1)};
[y,Pf,Af] = sim(net,{1 20},{},a);

%%
% We can make a plot of the Hopfield networks activity.
% 
% Sure enough, the network ends up in either the upper-left or lower right
% corners of the plot.

record = [cell2mat(a) cell2mat(y)];
start = cell2mat(a);
hold on
plot(start(1,1),start(2,1),'bx',record(1,:),record(2,:))

%%
% We repeat the simulation for 25 more initial conditions.
% 
% Note that if the Hopfield network starts out closer to the upper-left, it will
% go to the upper-left, and vise versa. This ability to find the closest memory
% to an initial input is what makes the Hopfield network useful.

color = 'rgbmy';
for i=1:25
 a = {rands(2,1)};
 [y,Pf,Af] = sim(net,{1 20},{},a);
 record=[cell2mat(a) cell2mat(y)];
 start=cell2mat(a);
 plot(start(1,1),start(2,1),'kx',record(1,:),record(2,:),color(rem(i,5)+1))
end
"></originalCode>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -