Mercurial > hg > smallbox
comparison examples/SMALL_DL_test.m @ 195:d50f5bdbe14c luisf_dev
- Added SMALL_DL_test: simple DL showcase
- Added dico_decorr_symmetric: improved version of INK-SVD decorrelation step
- Debugged SMALL_learn, SMALLBoxInit and SMALL_two_step_DL
author | Daniele Barchiesi <daniele.barchiesi@eecs.qmul.ac.uk> |
---|---|
date | Wed, 14 Mar 2012 14:42:52 +0000 |
parents | |
children | fd0b5d36f6ad |
comparison
equal
deleted
inserted
replaced
190:759313488e7b | 195:d50f5bdbe14c |
---|---|
1 function SMALL_DL_test | |
2 clear, clc, close all | |
3 % Create a 2-dimensional dataset of points that are oriented in 3 | |
4 % directions on a x/y plane | |
5 | |
6 % | |
7 % Centre for Digital Music, Queen Mary, University of London. | |
8 % This file copyright 2012 Daniele Barchiesi. | |
9 % | |
10 % This program is free software; you can redistribute it and/or | |
11 % modify it under the terms of the GNU General Public License as | |
12 % published by the Free Software Foundation; either version 2 of the | |
13 % License, or (at your option) any later version. See the file | |
14 % COPYING included with this distribution for more information. | |
15 | |
16 nData = 10000; %number of data | |
17 theta = [pi/6 pi/3 4*pi/6]; %angles | |
18 nAngles = length(theta); %number of angles | |
19 Q = [cos(theta); sin(theta)]; %rotation matrix | |
20 X = Q*randmog(nAngles,nData); %training data | |
21 | |
22 % find principal directions using PCA | |
23 XXt = X*X'; %cross correlation matrix | |
24 [U ~] = svd(XXt); %svd of XXt | |
25 | |
26 scale = 3; %scale factor for plots | |
27 subplot(1,2,1), hold on | |
28 title('Principal Component Analysis') | |
29 scatter(X(1,:), X(2,:),'.'); %scatter training data | |
30 O = zeros(size(U)); %origin | |
31 quiver(O(1,1:2),O(2,1:2),scale*U(1,:),scale*U(2,:),... | |
32 'LineWidth',2,'Color','k') %plot atoms | |
33 axis equal %scale axis | |
34 | |
35 subplot(1,2,2), hold on | |
36 title('K-SVD Dictionary') | |
37 scatter(X(1,:), X(2,:),'.'); | |
38 axis equal | |
39 | |
40 nAtoms = 3; %number of atoms in the dictionary | |
41 nIter = 1; %number of dictionary learning iterations | |
42 initDict = normc(randn(2,nAtoms)); %random initial dictionary | |
43 O = zeros(size(initDict)); %origin | |
44 | |
45 % apply dictionary learning algorithm | |
46 ksvd_params = struct('data',X,... %training data | |
47 'Tdata',1,... %sparsity level | |
48 'dictsize',nAtoms,... %number of atoms | |
49 'initdict',initDict,...%initial dictionary | |
50 'iternum',10); %number of iterations | |
51 DL = SMALL_init_DL('ksvd','ksvd',ksvd_params); %dictionary learning structure | |
52 DL.D = initDict; %copy initial dictionary in solution variable | |
53 problem = struct('b',X); %copy training data in problem structure | |
54 | |
55 xdata = DL.D(1,:); | |
56 ydata = DL.D(2,:); | |
57 qPlot = quiver(O(1,:),O(2,:),scale*initDict(1,:),scale*initDict(2,:),... | |
58 'LineWidth',2,'Color','k','UDataSource','xdata','VDataSource','ydata'); | |
59 | |
60 for iIter=1:nIter | |
61 DL.ksvd_params.initdict = DL.D; | |
62 pause | |
63 DL = SMALL_learn(problem,DL); %learn dictionary | |
64 xdata = scale*DL.D(1,:); | |
65 ydata = scale*DL.D(2,:); | |
66 refreshdata(gcf,'caller'); | |
67 end | |
68 | |
69 | |
70 function X = randmog(m, n) | |
71 % RANDMOG - Generate mixture of Gaussians | |
72 s = [0.2 2]; | |
73 % Choose which Gaussian | |
74 G1 = (rand(m, n) < 0.9); | |
75 % Make them | |
76 X = (G1.*s(1) + (1-G1).*s(2)) .* randn(m,n); |