Mercurial > hg > smallbox
comparison examples/SMALL_DL_test.m @ 193:cc540df790f4 danieleb
Simple example that demonstrated dictionary learning... to be completed
author | Daniele Barchiesi <daniele.barchiesi@eecs.qmul.ac.uk> |
---|---|
date | Fri, 09 Mar 2012 15:12:01 +0000 |
parents | |
children | 9b0595a8478d |
comparison
equal
deleted
inserted
replaced
192:f1e601cc916d | 193:cc540df790f4 |
---|---|
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 nData = 10000; %number of data | |
6 theta = [pi/6 pi/3 4*pi/6]; %angles | |
7 m = length(theta); | |
8 Q = [cos(theta); sin(theta)]; %rotation matrix | |
9 X = Q*randmog(m,nData); | |
10 | |
11 % find principal directions using PCA and plot them | |
12 XXt = X*X'; | |
13 [U ~] = svd(XXt); | |
14 scale = 3; | |
15 subplot(1,2,1), hold on | |
16 title('Principal Component Analysis') | |
17 scatter(X(1,:), X(2,:),'.'); | |
18 O = zeros(size(U)); | |
19 quiver(O(1,1:2),O(2,1:2),scale*U(1,:),scale*U(2,:),'LineWidth',2,'Color','k') | |
20 axis equal | |
21 | |
22 subplot(1,2,2), hold on | |
23 title('K-SVD Dictionary') | |
24 scatter(X(1,:), X(2,:),'.'); | |
25 axis equal | |
26 nAtoms = 3; | |
27 initDict = randn(2,nAtoms); | |
28 nIter = 10; | |
29 O = zeros(size(initDict)); | |
30 % apply dictionary learning algorithm | |
31 ksvd_params = struct('data',X,... %training data | |
32 'Tdata',1,... %sparsity level | |
33 'dictsize',nAtoms,... %number of atoms | |
34 'initdict',initDict,... | |
35 'iternum',10); %number of iterations | |
36 DL = SMALL_init_DL('ksvd','ksvd',ksvd_params); | |
37 DL.D = initDict; | |
38 xdata = DL.D(1,:); | |
39 ydata = DL.D(2,:); | |
40 qPlot = quiver(O(1,:),O(2,:),scale*initDict(1,:),scale*initDict(2,:),... | |
41 'LineWidth',2,'Color','k','XDataSource','xdata','YDataSource','ydata'); | |
42 problem = struct('b',X); %training data | |
43 | |
44 %plot dictionary and learn | |
45 for iIter=1:nIter | |
46 DL.ksvd_params.initdict = DL.D; | |
47 DL = SMALL_learn(problem,DL); %learn dictionary | |
48 xdata = DL.D(1,:); | |
49 ydata = DL.D(2,:); | |
50 pause | |
51 refreshdata(gcf,'caller'); | |
52 %quiver(O(1,:),O(2,:),scale*DL.D(1,:),scale*DL.D(2,:),'LineWidth',2,'Color','k'); | |
53 end | |
54 | |
55 | |
56 function X = randmog(m, n) | |
57 % RANDMOG - Generate mixture of Gaussians | |
58 s = [0.2 2]; | |
59 % Choose which Gaussian | |
60 G1 = (rand(m, n) < 0.9); | |
61 % Make them | |
62 X = (G1.*s(1) + (1-G1).*s(2)) .* randn(m,n); |