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

📄 tkm.m

📁 竞争学习的matlab工具箱
💻 M
字号:
% Basic implementation of the Temporal Kohonen Map (TKM)
%
% Source: 
%    
%
% NOTE: This implementation aims to be simple and direct. More powerful
% implementations of the SOM can be certainly found elsewhere.
% 
% Authors: Guilherme A. Barreto
% Date: September 13th 2006

clear; clc; close all;

% Load data
load train_series_data.dat;    % Load the time series to be clustered

%% Building the input vectors from an univariate time series
p=100;       % Dimension of the input vector (length of the time window)
lap=0;     % Amount of overlapping between consecutive input vectors
Dw=buffer(train_series_data,p,lap); 
clear train_series_data
if lap>0,
     Dw=Dw(:,p:end)';  % Eliminate the first 'p-1' vectors with zeros)
else Dw=Dw';
end

% Get size of data matrix (1 input vector per row)
[LEN_DATA DIM_INPUT]=size(Dw);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Define size of the network %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Mx = 6;   % Number of neurons in the X-dimension
My = 6;   % Number of neurons in the Y-dimension
MAP_SIZE = [Mx My];        % Size of SOM map

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Create a CL network structure  %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
sMap = som_map_struct(DIM_INPUT,'msize',MAP_SIZE,'rect','sheet');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Different weights initialization methods %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
sMap  = som_randinit(Dw, sMap);     % Random weight initialization
% sMap  = som_lininit(Dw, sMap);    % Linear weight initialization
%I=randperm(LEN_DATA); sMap.codebook=Dw(I(1:Mx*My),:);  % Select Mx*My data vectors at random

Co=som_unit_coords(sMap); % Coordinates of neurons in the map

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Specification of some training parameters %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
si=round(max(Mx,My)/2);  % Initial neighborhood
sf=0.001;                % Final neighborhood
ei=0.5;                  % Initial learning rate
ef=0.001;              % Final learning rate
Nti=500;                % Number of training iterations
T=0:Nti;              % Time index for training iteration
eta=ei*power(ef/ei,T/Nti);  % Learning rate vector
sig=si*power(sf/si,T/Nti);  % Neighborhood width vector

vec=[1.0 0.95 0.78 0.73 0.625 0.45 0.40 0.35];       % Memory parameter vector
lambda=vec(8);
Lep=find(vec==lambda);   % Length of the episode

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Train Temporal Kohonen Map (TKM)  %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
counter=zeros(1,Mx*My);  % Counter for the number of victories
for t=1:Nti,  % loop for the training iterations
    
    t,
    
    Act=zeros(Mx*My,1); % Reset STM for each episode
    
    I=randperm(LEN_DATA);          % Shuffle the integer sequence {1, ..., LENDATA}
    tti=I(1);                      % Random selection of the initial instant of the episode
    ttf=min(tti+Lep-1,LEN_DATA);   % Determination of the final instant of the episode
 
    % Beginning of the episode
    for tt=tti:ttf,
         Di=sqrt(som_eucdist2(sMap,Dw(tt,:)));      % Compute distances to current input vector
         Act=(1-lambda)*Act-0.5*Di.*Di;          % Compute neuronal STM activations
    end
    
    [Act_min win] = max(Act);      % Find the BMU

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Weight updating at the end of the episode %%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    for i=1:Mx*My,
        % Squared distance (in map coordinates) between winner and neuron i
        D2=power(norm(Co(win,:)-Co(i,:)),2);

        % Compute corresponding value of the neighborhood function
        H=exp(-0.5*D2/(sig(t)*sig(t)));

        % Update the weights of neuron i
        sMap.codebook(i,:)=sMap.codebook(i,:) + eta(t)*H*(Dw(tt,:)-sMap.codebook(i,:));
    end

    % Quantization error per training epoch
    Qerr(t) = som_quality(sMap, Dw);
end


% Plot prototypes and data altogether
figure, plot(Dw(:,1),Dw(:,2),'+r'), hold on
som_grid(sMap,'Coord',sMap.codebook)
%plot(sMap.codebook(:,1),sMap.codebook(:,2),'b*')
title('Prototype vectors in input space'), hold off


⌨️ 快捷键说明

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