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

📄 skmeans.m

📁 竞争学习的matlab工具箱
💻 M
字号:
% MATLAB implementation of Sequential K-means using the SOMTOOLBOX
% Source: 
%    T. Kohonen (1997). Self-Organizing Maps, 2nd. Edition,
%    Springer-Verlag.
% 
% Code author: Guilherme A. Barreto
% Date: September 19th 2005

clear; clc; close all;

% Load data
load dataset1.dat;
Dw=dataset1; clear dataset1

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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Create a SOM structure  %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Mx = 16;   % Number of neurons
MAP_SIZE = [Mx 1];        % Size of SOM map (always uses 1-D map)

sMap = som_map_struct(DIM_INPUT,'msize', MAP_SIZE,'hexa','sheet');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Assign initial values for the weights %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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),:);  % Select Mx*My data vectors at random

% Train the Sequential K-means algorithm
disp('----------------------------------------');
disp('Running the Sequential K-means Algorithm... Please wait!');

Ep=100;      % number of training epochs
counter=zeros(Mx,1);  % Counter for the number of victories
alpha=ones(Mx,1);    % Initial learning rate values
for t=1:Ep,   % Loop for the training epochs
    Epoch=t,  % Show current epoch
    
    % Shuffle input data vectors at each training epoch
    I=randperm(LEN_DATA);  % shuffle the row indices
    Dw=Dw(I,:);
    
    for tt=1:LEN_DATA,   % Loop for iteration with each epoch
        win=som_bmus(sMap, Dw(tt,:));  % Find current winning neuron (BMU)
        counter(win)=counter(win)+1;   % Increment counter
        
        % Update the weights of the winning neuron only
        sMap.codebook(win,:)=sMap.codebook(win,:)+alpha(win)*(Dw(tt,:)-sMap.codebook(win,:));
        
        % Update learning rate for the winning neuron
        alpha(win) = 1/(counter(win)+1);
    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
plot(sMap.codebook(:,1),sMap.codebook(:,2),'b*')
title('Prototype vectors in input space'), hold off

% Plot quantization error evolution per training epoch
figure, plot(Qerr) 
title('Quantization Error per Training Epoch')

⌨️ 快捷键说明

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