⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hopfield.m

📁 线性神经网络
💻 M
字号:
function varargout = Hopfield(varargin)
% HOPFIELD M-file for Hopfield.fig
%      HOPFIELD, by itself, creates a new HOPFIELD or raises the existing
%      singleton*.
%
%      H = HOPFIELD returns the handle to a new HOPFIELD or the handle to
%      the existing singleton*.
%
%      HOPFIELD('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in HOPFIELD.M with the given input arguments.
%
%      HOPFIELD('Property','Value',...) creates a new HOPFIELD or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Hopfield_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Hopfield_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Copyright 2002-2003 The MathWorks, Inc.

% Edit the above text to modify the response to help Hopfield

% Last Modified by GUIDE v2.5 26-May-2006 21:30:34

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Hopfield_OpeningFcn, ...
                   'gui_OutputFcn',  @Hopfield_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before Hopfield is made visible.
function Hopfield_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to Hopfield (see VARARGIN)

% Choose default command line output for Hopfield
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes Hopfield wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = Hopfield_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in PB_T.
function PB_T_Callback(hObject, eventdata, handles)
% hObject    handle to PB_T (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

user_data=get(findobj('Tag','PB_T'),'userdata');
switch  user_data.chose
    case 1
        T = [+1 -1; -1 +1];
        
        str='>> T = [+1 -1; -1 +1];';
        set(findobj('Tag','edit1'),'String',str);
        
        str='我们将要创建一个具有两个稳定点的Hopfield网络,其中稳定点用向量T来定义';
        set(findobj('Tag','edit2'),'String',str);
        
        
        user_data.chose=user_data.chose+1;
        user_data.T=T;
        set(findobj('Tag','PB_T'),'userdata',user_data);
        
        
        set(findobj('Tag','text2'),'String','step1');
        
        set(hObject,'String','Next>>');
    case 2
        T=user_data.T;
        
        h=findobj('Tag','axes1');
        plot(T(1,:),T(2,:),'r*');
        axis(h,[-1.1 1.1 -1.1 1.1])
        xlabel('a(1)');
        ylabel('a(2)');
        
        str=sprintf('>> plot(findobj(''Tag'',''axes1''),T(1,:),T(2,:),''r*'');\n>> xlabel(''a(1)'');\n>> ylabel(''a(2)'');');
        set(findobj('Tag','edit1'),'String',str);
        
        str='代码在绘图区的两角绘制了Hopfield网络的稳定点,所有可能的稳定点都在这两个点之间';
        set(findobj('Tag','edit2'),'String',str);
        
        user_data.chose=user_data.chose+1;
        set(findobj('Tag','PB_T'),'userdata',user_data);
                
        set(findobj('Tag','text2'),'String','step2');
    case 3
        T=user_data.T;
        
        net = newhop(T);
        
        str=sprintf('>> net = newhop(T);');
        set(findobj('Tag','edit1'),'String',str);
        
        str='利用函数newhop创建了一个Hopfield网络';
        set(findobj('Tag','edit2'),'String',str);
        
        user_data.chose=user_data.chose+1;
        user_data.net=net;
        set(findobj('Tag','PB_T'),'userdata',user_data);
        
        set(findobj('Tag','text2'),'String','step3');
    case 4
        T=user_data.T;
        net=user_data.net;
        
        [Y,Pf,Af] = sim(net,2,[],T);
        
        str1=sprintf('>> [Y,Pf,Af] = sim(net,2,[],T);\n>> Y=');
        str2=num2str(Y);
        str=strcat(str1,str2);
        set(findobj('Tag','edit1'),'String',str);
        
        str='首先我们要确定目标向量确实是稳定的,当我们把目标向量传给Hopfield网络之后,如果能不做任何改变得传回来,那就说明是稳定的';
        set(findobj('Tag','edit2'),'String',str);
        
        user_data.chose=user_data.chose+1;
        set(findobj('Tag','PB_T'),'userdata',user_data);
        
        set(findobj('Tag','text2'),'String','step4');
    case 5
        net=user_data.net;
        
        a = {rands(2,1)};
        [y,Pf,Af] = sim(net,{1 20},{},a);
        
        str=sprintf('>> a = {rands(2,1)};\n>> [y,Pf,Af] = sim(net,{1 20},{},a);');
        set(findobj('Tag','edit1'),'String',str);
        
        str='我们定义了一个随机的起始点,并仿真了Hopfield网络';
        set(findobj('Tag','edit2'),'String',str);
        
        user_data.chose=user_data.chose+1;
        user_data.net=net;
        user_data.a=a;
        user_data.y=y;
        set(findobj('Tag','PB_T'),'userdata',user_data);
        
        set(findobj('Tag','text2'),'String','step5');
    case 6
        a=user_data.a;
        y=user_data.y;
        
        record = [cell2mat(a) cell2mat(y)];
        start = cell2mat(a);
        hold on
        plot(start(1,1),start(2,1),'bx',record(1,:),record(2,:))
              
        str=sprintf('>> record = [cell2mat(a) cell2mat(y)];\n>> start = cell2mat(a);\n>> hold on\n>> plot(start(1,1),start(2,1),''bx'',record(1,:),record(2,:))');
        set(findobj('Tag','edit1'),'String',str);
        
        str='绘制Hopfield网络的行为';
        set(findobj('Tag','edit2'),'String',str);
      
        user_data.chose=user_data.chose+1;
        set(findobj('Tag','PB_T'),'userdata',user_data);
        
        set(findobj('Tag','text2'),'String','step6');
    case 7
        net=user_data.net;

        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;
        
        str1=sprintf('>> color = ''rgbmy'';\n>> for i=1:25\n>>    a = {rands(2,1)};\n>>    [y,Pf,Af] = sim(net,{1 20},{},a);');
        str2=sprintf('\n>>    start=cell2mat(a);\n>>    plot(start(1,1),start(2,1),''kx'',record(1,:),record(2,:),color(rem(i,5)+1))\n>> end');
        str=strcat(str1,str2);
        set(findobj('Tag','edit1'),'String',str);
        
        str='重复做25次仿真';
        set(findobj('Tag','edit2'),'String',str);
            
        set(findobj('Tag','text1'),'String','step 7');
        
        user_data.chose=user_data.chose+1;
        set(hObject,'userdata',user_data);
    case 8
        set(hObject,'String','Start')
        set(findobj('Tag','edit2'),'String','请按‘开始‘按钮启动演示。若有不明的函数可查看‘帮助’。');
        hold off

        str1='Hopfield网络作为一种全连接型的神经网络,曾经为人工神经网络的发展进程开辟了新的研究途径。它利用与阶层型神经网络不同的'
        str2='结构特征和学习方法,模拟生物神经网络产记忆机理,获得了令人满意的结果。';
        str=strcat(str1,str2);
        set(findobj('Tag','edit1'),'String',str)
        
        set(findobj('Tag','text2'),'String','step 0');
                
        temp_data.chose=1;
        set(hObject,'userdata',temp_data);
        
end

function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes during object creation, after setting all properties.
function PB_T_CreateFcn(hObject, eventdata, handles)
% hObject    handle to PB_T (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

user_data.chose=1;
set(findobj('Tag','PB_T'),'userdata',user_data);



function edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double


% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end




% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

close;
SysGui;


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -