-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualize.m
55 lines (47 loc) · 1.87 KB
/
visualize.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
% Visualization of Gaussian embeddings
% Requires LSSVMlab already imported: http://www.esat.kuleuven.be/sista/lssvmlab/
%
% The three files generated by the binary2text tool are needed:
% Means of the distributions: embeddings.bin_mu.txt
% Covariance matrices: embeddings.bin_sigma.txt
% Vocabulary: embeddings.bin_words.txt
MU = import_vectors('embeddings.bin_mu.txt', 1);
SIGMA = import_vectors('embeddings.bin_sigma.txt', 1);
vocab = import_words('embeddings.bin_words.txt',1);
%Example to show "Madrid"-"Spain"+"France" ~ "Paris"
words = {'madrid' 'paris' 'spain' 'france'};
colors={'r' 'g' 'b' 'c' 'm' 'y' 'k'};
vectors = [];
variances = [];
for w = words
i = find(strcmp(vocab,w));
vectors = [vectors ; MU(i,:)];
variances = [variances ; SIGMA(i,:)];
end
% calculate the eigenvectors in the feature space (principal components)
sig2 = 50;
[U,V] = kpca(vectors','RBF_kernel',sig2,[],'eigs', 2,'o');
%[U,V] = kpca(vectors','lin_kernel',[],[],'eigs', 2);
% calculate the projections on the principal components
Xax = -3:.1:3; Yax = -3.2:.1:3.2;
[A,B] = meshgrid(Xax,Yax);
grid = [reshape(A,prod(size(A)),1) reshape(B,1,prod(size(B)))'];
mu_projections = vectors*V;
figure;
x = -5:.1:5; %// x axis
y = -4:.1:4; %// y axis
[X Y] = meshgrid(x,y); %// all combinations of x, y
sigma_projections = [];
color_i = 1;
for i=1:length(words)
sigma_projections = V'*diag(variances(i,:))*V;
Z = mvnpdf([X(:) Y(:)],mu_projections(i,:),sigma_projections); %// compute Gaussian pdf
Z = reshape(Z,size(X)); %// put into same size as X, Y
contour(X,Y,Z, [0.05 0.05], colors{color_i}), axis equal %// contour plot; set same scale for x and y...
text(mu_projections(i,1),mu_projections(i,2), words(i), 'color', colors{color_i} );
color_i = mod(color_i, length(colors)) +1;
disp(words(i));
disp(prod(sigma_projections(:)));
hold on
end
hold off