-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw_samples_TNS_CP.m
205 lines (170 loc) · 5.95 KB
/
draw_samples_TNS_CP.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
function [samples, sqrt_probs] = draw_samples_TNS_CP(A, AtA, n, no_samp)
%draw_samples_TNS_CP Draws samples for CP design matrix as discussed in our
%paper
%
%[samples, sqrt_probs] = draw_samples_TNS_CP(A, sketch, n, no_samp) returns
%no_samp samples organized into an no_samp by N matrix, where N is the
%number of modes of the tensor being decomposed, and a vector sqrt_probs
%which contains the square root of the probability of drawing each of the
%sampled rows. The cell A should contain all the CP factor matrices in
%standard order, and the n-th factor matrix is the one being solved for.
%The n-th column of samples will just be NaN, since that index is not being
%sampled. The object AtA should be a cell containing the Gram matrices of
%the factor matrices ordered in the same way as the cell A.
%
%This is an adaption of the function draw_samples_CP.m in the repo at
%https://github.com/OsmanMalik/TD-ALS-ES, which is the repo associated with
%the paper [Mal22].
%
%REFERENCES:
% [Mal22] O. A. Malik, More Efficient Sampling for Tensor Decomposition
% With Worst-Case Guarantees. ICML, 2022.
N = length(A);
% if statement to avoid issues when n=1 and A{1} is empty
if n == 1
R = size(A{2},2);
else
R = size(A{1},2);
end
% Compute matrix Phi
AmTAm = ones(R, R);
for j = 1:N
if j ~= n
AmTAm = AmTAm .* AtA{j};
end
end
Phi = pinv(AmTAm);
% Precompute the two terms in Eq (28) in paper [Mal22].
M1 = cell(1,N);
M2 = cell(1,N);
for j = 1:N
if j ~= n
M1{j} = khatrirao(A{j}.', A{j}.');
M2{j} = A{j}.' * A{j};
end
end
% Also precompute the products in the second of those terms.
% Note that these products incorporate the Phi matrix as well.
term_2 = cell(1,N);
for m = 1:N
term_2{m} = Phi(:);
for j = m+1:N
if j ~= n
term_2{m} = term_2{m} .* M2{j}(:);
end
end
end
% Main loop for drawing all samples
samples = nan(no_samp, N); % Each row is a realization (i_j)_{j \neq n}
sqrt_probs = ones(no_samp, 1); % To store the square root of the probability of each drawn sample
if n == 1
first_idx = 2;
else
first_idx = 1;
end
%% Draw first subindex for each sample
m = first_idx;
% Compute conditional probability vector
%common_terms = Phi(:);
%for j = 1:N
% if j > m && j ~= n
% common_terms = common_terms .* M2{j}(:);
% end
%end
%common_terms = common_terms.';
common_terms = term_2{m}.';
%const = common_terms * M2{m}(:);
%common_terms = common_terms / const;
prob_m = common_terms * M1{m};
prob_m = prob_m / sum(prob_m);
% Draw from the vector
samples(:, m) = randsample(length(prob_m), no_samp, true, prob_m);
% Update probability vector
sqrt_probs = sqrt(prob_m(samples(:, m)));
%% Compute rest of subindices
for samp = 1:no_samp
% Compute P(i_m | (i_j)_{j < m, ~= n}) for each m (~=n) and draw i_m
current_sample = samples(samp, :);
temp_common_terms = M1{first_idx}(:, current_sample(first_idx));
for m = first_idx+1:N
if m ~= n
% Compute conditional probability vector
%common_terms = Phi(:);
%for j = 1:N
% if j < m && j ~= n
% %common_terms = common_terms .* M1{j}(:, samples(samp, j));
% common_terms = common_terms .* M1{j}(:, current_sample(j));
% elseif j > m && j ~= n
% common_terms = common_terms .* M2{j}(:);
% end
%end
%{
common_terms = Phi(:);
for j = 1:m-1
if j ~= n
common_terms = common_terms .* M1{j}(:, current_sample(j));
end
end
common_terms = common_terms .* term_2{m};
common_terms_2 = temp_common_terms .* term_2{m};
asdf = norm(common_terms - common_terms_2);
if asdf ~= 0
asdf
end
%}
common_terms = (temp_common_terms .* term_2{m}).';
%common_terms = common_terms.';
prob_m = common_terms * M1{m};
prob_m = prob_m / sum(prob_m);
% Draw from the vector
sampled_idx = mt19937ar(prob_m);
%samples(samp, m) = sampled_idx;
current_sample(m) = sampled_idx;
temp_common_terms = temp_common_terms .* M1{m}(:, current_sample(m));
% Update probability vector
sqrt_probs(samp) = sqrt_probs(samp) * sqrt(prob_m(sampled_idx));
end
end
samples(samp,:) = current_sample;
end
%{
first_idx_flag = true;
for samp = 1:no_samp
% Compute P(i_m | (i_j)_{j < m, ~= n}) for each m (~=n) and draw i_m
for m = first_idx:N
if m == first_idx && ~first_idx_flag
continue
end
if m ~= n
% Compute conditional probability vector
common_terms = Phi(:);
for j = 1:N
if j < m && j ~= n
common_terms = common_terms .* M1{j}(:, samples(samp, j));
elseif j > m && j ~= n
common_terms = common_terms .* M2{j}(:);
end
end
common_terms = common_terms.';
%const = common_terms * M2{m}(:);
%common_terms = common_terms / const;
prob_m = common_terms * M1{m};
prob_m = prob_m / sum(prob_m);
if first_idx_flag
% Draw from the vector
samples(:, m) = randsample(length(prob_m), no_samp, true, prob_m);
% Update probability vector
sqrt_probs = sqrt(prob_m(samples(:, m)));
first_idx_flag = false;
else
% Draw from the vector
samples(samp, m) = mt19937ar(prob_m);
% Update probability vector
sqrt_probs(samp) = sqrt_probs(samp) * sqrt(prob_m(samples(samp,m)));
end
end
end
end
%}
sqrt_probs = sqrt_probs';
end