-
Notifications
You must be signed in to change notification settings - Fork 10
/
sketch_from_mat_ts.m
82 lines (78 loc) · 3.09 KB
/
sketch_from_mat_ts.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
function [YsT, vecYs] = sketch_from_mat_ts(J1, J2, h1int64, h2int64, s, verbose, filename, inc_size)
% SKETCH_FROM_MAT_TS This sketch function returns the various sketches
% of a tensor stored in a mat file required for
% tucker_ts.
%
% This function requires Tensor Toolbox [1]
% version 2.6.
%
% [YsT, vecYs] = SKETCH_FROM_MAT_TS(J1, J2, h1int64, h2int64, s, verbose, filename, inc_size)
% returns a cell YsT containing the sketches of each matricization of the
% tensor stored in the mat file specified in the filename variable. The
% function also returns vecYs, which is the sketch of the vectorization
% of that same tensor. The variables J1 and J2 specify the target sketch
% dimensions; h1int64, h2int64, s are the various hash functions; verbose
% is the verbose flag (true or false); and inc_size is a vector that
% contains the increment size to be used when reading in the mat file
% tensor (must divide the corresponding tensor dimension size).
%
% Please see demo3.m, which was provided together with
% this software, for an example of how to use this function.
%
% For further information about our methods, please see our paper [2].
%
% REFERENCES:
%
% [1] B. W. Bader, T. G. Kolda and others. MATLAB Tensor Toolbox
% Version 2.6, Available online, February 2015.
% URL: http://www.sandia.gov/~tgkolda/TensorToolbox/.
%
% [2] O. A. Malik, S. Becker. Low-Rank Tucker Decomposition of Large
% Tensors Using TensorSketch. Advances in Neural Information
% Processing Systems (NeurIPS), 2018.
% Author: Osman Asif Malik
% Email: osman.malik@colorado.edu
% Date: December 21, 2018
file = matfile(filename, 'Writable', false);
sizeY = size(file, 'Y');
N = length(sizeY);
ns = 1:N;
YsT = cell(N,1);
for n = 1:N
if n < N
current_inc_size = inc_size(N);
no_inc = sizeY(N)/current_inc_size;
else
current_inc_size = inc_size(N-1);
no_inc = sizeY(N-1)/current_inc_size;
end
if rem(no_inc,1) ~= 0
error('ERROR: The increment size must divide the relevant tensor dimension sizes.')
end
YsT{n} = zeros(J1, sizeY(n));
if n == 1
vecYs = zeros(J2, 1);
end
for inc = 1:no_inc
slice_start = 1+(inc-1)*current_inc_size;
slice_end = inc*current_inc_size;
if n < N
colons = repmat({':'}, 1, N-1);
Y_piece = file.Y(colons{:}, slice_start : slice_end);
else % n == N
colons = repmat({':'}, 1, N-2);
Y_piece = file.Y(colons{:}, slice_start : slice_end, :);
end
YsT{n} = YsT{n} + TensorSketchMatC3_git(double(tenmat(Y_piece, n)), h1int64(ns~=n), s(ns~=n), ...
int64(J1), int64(slice_start), int64(slice_end)).';
clear Y_piece_n
if n == 1
vecYs = vecYs + TensorSketchVecC_git(Y_piece, h2int64, s, ...
int64(J2), int64(slice_start), int64(slice_end));
end
end
if verbose
fprintf('Finished computing sketch %d out of %d...\n', n+1, N+1)
end
end
end