Chinaunix首页 | 论坛 | 博客
  • 博客访问: 712139
  • 博文数量: 98
  • 博客积分: 3257
  • 博客等级: 中校
  • 技术积分: 966
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-15 17:19
文章存档

2020年(1)

2018年(1)

2017年(1)

2016年(2)

2015年(2)

2013年(3)

2012年(24)

2011年(28)

2010年(4)

2009年(9)

2008年(23)

我的朋友

分类: 大数据

2013-07-24 16:46:34

%SOM_DEMO1 Basic properties and behaviour of the Self-Organizing Map.
%SOM Toolbox 2.0 Mar_17_2005 for matlab 2012a on winxp in virtualbox4.2.16
%    ==========================================================
%    SOM_DEMO1 - BEHAVIOUR AND PROPERTIES OF SOM
%    ==========================================================

%    som_make        - Create, initialize and train a SOM.
%     som_randinit   - Create and initialize a SOM.
%     som_lininit    - Create and initialize a SOM.
%     som_seqtrain   - Train a SOM.
%     som_batchtrain - Train a SOM.
%    som_bmus        - Find best-matching units (BMUs).
%    som_quality     - Measure quality of SOM.

%    SELF-ORGANIZING MAP (SOM):

%    A self-organized map (SOM) is a "map" of the training data, 
%    dense where there is a lot of data and thin where the data 
%    density is low. 

%    The map constitutes of neurons located on a regular map grid. 
%    The lattice of the grid can be either hexagonal or rectangular.
%    神经网络网格形状可以是六边形或矩形,下面分别绘制了2种形状的网格
subplot(1,2,1)
som_cplane('hexa',[10 15],'none')
title('Hexagonal SOM grid')

subplot(1,2,2)
som_cplane('rect',[10 15],'none')
title('Rectangular SOM grid')
%    网格中的每个神经元都有一个原型向量(也称权重向量)
%    经过训练后,SOM相邻神经元的原型向量会比较相似(相似性一般用欧式距离度量)
%    SOM可用于数据可视化、聚类、分类、估计或其他用途
clf
clc
%    INITIALIZE AND TRAIN THE SELF-ORGANIZING MAP
%    ============================================
D = rand(300,2);    %初始化300个随机的2维数据点
msize = [10 10];    %指定神经网络输出层网格为10x10
%    SOM_RANDINIT 和 SOM_LININIT都可用于初始化网络神经元的原型向量
%    神经元网格大小是个可选参数,如果不指定,它会基于数据向量数和数据集的主特征向量而自动确定
%   下面的函数采用随机数初始化

sMap  = som_randinit(D, 'msize', msize);

%    实际上,SOM的每个网元有2套坐标系
%      (1) in the input space:  the prototype vectors 输入空间:原型向量
%      (2) in the output space: the position on the map 输出空间:神经元映射位置
%    下面通过绘图展示:
subplot(1,3,1) 
som_grid(sMap)    %画出网格的形状位置
axis([0 11 0 11]), view(0,-90), title('Map in output space')

subplot(1,3,2) 
plot(D(:,1),D(:,2),'+r'), hold on    %画出数据点的位置,采用红色的‘+’表示一个数据点
som_grid(sMap,'Coord',sMap.codebook)    %画出原型向量的坐标位置
title('Map in input space')

sMap  = som_seqtrain(sMap,D,'radius',[5 1],'trainlen',10);    %采用顺序训练的方法训练SOM

subplot(1,3,3)
som_grid(sMap,'Coord',sMap.codebook)    %画出训练后的som网格,黑点为神经元位置,灰色线条为邻居链接关系
hold on, plot(D(:,1),D(:,2),'+r')        %画出数据点,红色‘+’表示数据点
title('Trained map')
clf

clc
%    TRAINING THE SELF-ORGANIZING MAP
%    ================================
%    为了更好的展示训练过程,可以通过可视化形式演示出来
sMap = som_randinit(D,'msize',msize);
sMap.codebook = sMap.codebook + 1;    %特意把som网络偏移一个位置

subplot(1,2,1)
som_grid(sMap,'Coord',sMap.codebook)
hold on, plot(D(:,1),D(:,2),'+r'), hold off
title('Data and original map')
%    网络的训练基于以下2个原则: 
%     
%      Competitive learning(竞争学习): 原型向量最接近数据向量的单元被修改得更接近于数据向量
%     这样,som网络将学习数据云的位置。This way the map learns the position of the data cloud.
%
%      Cooperative learning(协作学习): 不仅是最接近的单元,包括它的邻居都朝数据向量移动
%      这样实现了网络的自组织。This way the map self-organizes.
echo off
subplot(1,2,2)
o = ones(5,1);
r = (1-[1:60]/60);
for i=1:60,
  sMap = som_seqtrain(sMap,D,'tracking',0,...
      'trainlen',5,'samples',...
      'alpha',0.1*o,'radius',(4*r(i)+1)*o);
  som_grid(sMap,'Coord',sMap.codebook)
  hold on, plot(D(:,1),D(:,2),'+r'), hold off
  title(sprintf('%d/300 training steps',5*i))
  drawnow
 end
title('Sequential training after 300 steps')
clf

clc
%    TRAINING DATA: THE UNIT CUBE
%    ============================
%    上面的粒子,神经网络的维度和输入数据空间的维度刚好相等都是2维
%    实际情况中,一般输入数据都是高维的.这种情况下,网络无法完美的拟合数据 
%    因而需要在两个目标中寻求平衡:
%      - data representation accuracy,数据表示的精度
%      - data set topology representation accuracy,数据集拓扑表示精度
%    如下三维立方体的粒子:
D = rand(500,3);

subplot(1,3,1), plot3(D(:,1),D(:,2),D(:,3),'+r')
view(3), axis on, rotate3d on
title('Data')
clc
%    DEFAULT TRAINING PROCEDURE
%    ==========================
 
%    上面的例子用了随机初始化和顺序训练,缺省情况下
%    采用的是线性初始化和批量训练算法(initialization is linear, and batch training algorithm)
%    训练分2个阶段进行: 首先是较大邻居半径的粗学习,然后再采用较小半径的微调。

%    函数SOM_MAKE可用于缺省状况下的网络训练:
sMap = som_make(D);

sMap0 = som_lininit(D); 

subplot(1,3,2)
som_grid(sMap0,'Coord',sMap0.codebook,...
 'Markersize',2,'Linecolor','k','Surf',sMap0.codebook(:,3)) 
axis([0 1 0 1 0 1]), view(-120,-25), title('After initialization')

subplot(1,3,3)
som_grid(sMap,'Coord',sMap.codebook,...
 'Markersize',2,'Linecolor','k','Surf',sMap.codebook(:,3)) 
axis([0 1 0 1 0 1]), view(3), title('After training'), hold on

clc
%    BEST-MATCHING UNITS (BMU)
%    =========================

%    在介绍映射质量前,一个重要的概念需要介绍,那就是最佳匹配单元BMU
%   the Best-Matching Unit (BMU). 一个数据向量的BMU是一个在网络中能够跟数据向量最相似的网络单元
%   实际应用中采用最小距离来确定最佳相似度,BMU可采用函数SOM_BMUS来计算. 
%    该函数返回最佳单元的索引。

%    Here the BMU is searched for the origin point (from the
%    trained map):

bmu = som_bmus(sMap,[0 0 0]);

%    Here the corresponding unit is shown in the figure. You can
%    rotate the figure to see better where the BMU is.
co = sMap.codebook(bmu,:);
text(co(1),co(2),co(3),'BMU','Fontsize',20)
plot3([0 co(1)],[0 co(2)],[0 co(3)],'ro-')
clc
%    SELF-ORGANIZING MAP QUALITY
%    ===========================

%    som网络有2个主要质量评估属性:
%      - data representation accuracy 数据精度
%      - data set topology representation accuracy 拓扑精度

%    前者 通常使用数据向量和其对应BMU间的均方差来度量
%    后者提供了多种度量方式,例如,  地形误差度量(the topographic error
%    measure):数据向量中第一和第二BMU不是相邻单元的数据向量的百分比

%    SOM_QUALITY 函数实现2中精度的度量.
%    Here are the quality measures for the trained map: 

[q,t] = som_quality(sMap,D)

[q0,t0] = som_quality(sMap0,D)
阅读(6962) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~