-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgcp_fg_est_G_rd.m
94 lines (71 loc) · 2.44 KB
/
gcp_fg_est_G_rd.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
function [G] = gcp_fg_est_G_rd(U, fh, gh, subs, xvals,d,sz,rd)
%GCP_FG_EST Estimate the GCP function and gradient with a subsample
%
% [F,G] = GCP_FG_EST(M, FH, GH, XSUBS, XVALS, WVALS) estimates the GCP
% function and gradient specified by FH and GH for M and X. In this case,
% we have only a portion of X as specified by XSUBS and XVALS along with
% the corresponding sampling weights in WVALS that are used in the estimate.
%
% See also GCP_SGD, GCP_FG, GCP_FG_SETUP.
%
%MATLAB Tensor Toolbox. Copyright 2018, Sandia Corporation.
% Created by Tamara G. Kolda, Fall 2018. Includes work with
% collaborators David Hong and Jed Duersch.
%% Hidden options
%
% Note that there are five hidden options. The first three are similar to
% the hidden options for gcp_fg and the fourth is whether or not to verify
% that M has lambda = [1,1,...,1], which is assumed and so should
% generally be checked unless the user is absolutely sure it's okay.
%% Parse inputs
%% Input checks (keep minimal for timing's sake)
% d = ndims(M);
% sz = size(M);
G = [];
% if LambdaCheck && ~all(M.lambda == 1)
% warning('Fixing M to have all ones for lambda');
% M = normalize(M,1);
% end
%% Compute model values and exploded Zk matrices
[mvals, Zexp] = gcp_fg_est_helper(U, subs);
%% Compute function value
%% Compute sample y values
yvals = gh(xvals, mvals);
%% Compute function and gradient
nsamples = size(subs,1);
S = sparse(subs(:,rd), (1:nsamples)', yvals, sz(rd), nsamples, nsamples);
G = S * Zexp{rd};
function [mvals, Zexp] = gcp_fg_est_helper(factors, subs)
% GCP_FG_EST_HELPER Model values at sample locations and exploded Zk's.
% Created by Tamara G. Kolda, Sept. 2018. Includes prior work by
% collaborators David Hong and Jed Duersch.
% Check for empty
if isempty(subs)
mvals = [];
return;
end
% Process inputs
d = size(subs,2);
% Create exploded U's from the model factor matrices
Uexp = cell(d,1);
for k = 1:d
Uexp{k} = factors{k}(subs(:,k),:);
end
% After this pass,
% Zexp{k} = Hadarmard product of Uexp{1} through Uexp{k-1}
% for k = 2,...,d.
Zexp = cell(1,d);
Zexp{2} = Uexp{1};
for k = 3:d
Zexp{k} = Zexp{k-1} .* Uexp{k-1};
end
% After this pass,
% Zexp{k} = Hadamard product of Uexp{1} though Uexp{d}, except Uexp{k}
% for k = 1,...,d.
Zexp{1} = Uexp{d};
for k=d-1:-1:2
Zexp{k} = Zexp{k} .* Zexp{1};
Zexp{1} = Zexp{1} .* Uexp{k};
end
% Compute model values at sample locations
mvals = sum(Zexp{d} .* Uexp{d},2);