Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Google's Winner Takes All hashing implemmentation
function[binary_codes,G_vecs] = WTA_kernel_binary(X,G_vecs,G, K)
% G is number of permutation vectors
% K is the insterest span
if size(G_vecs,1) == 0
G_vec = zeros(G,size(X,2));
for i = 1:G
G_vecs(i,:) = randperm(size(X,2),size(X,2));
end
end
c = zeros(size(X,1),G*K);
binary_codes = zeros(size(X,1),K*G);
for i = 1:G
interest_segment = X(:,G_vecs(i,1:K));
codes = zeros(size(X,1),K);
[max_vals, max_indx] = max(interest_segment, [], 2);
[min_vals, min_indx] = min(interest_segment, [], 2);
max_indx = bsxfun(@eq,interest_segment,max_vals);
min_indx = bsxfun(@eq,interest_segment,min_vals);
codes(max_indx) = 1;
codes(min_indx) = -1;
star_column = ((i-1)*K+1);
end_column = (i*K);
binary_codes(:,star_column:end_column) = codes;
end
% slower but better discimrimination
% for i = 1:G
% interest_segment = X(:,G_vecs(i,1:K));
% codes = zeros(size(X,1),K);
%
% for j = 1:size(X,1)
% unique_items = unique(interest_segment(j,:));
%
% % Check all same
% if numel(unique_items) == 1
% codes(j,:) = zeros(1,size(interest_segment,2));
% else
% [max_val, min_ind] = max(interest_segment(j,:));
% max_val_indices = find(interest_segment(j,:) >= max_val);
% codes(j,max_val_indices) = 1;
%
% [min_val, min_ind] = min(interest_segment(j,:));
% min_val_indices = find(interest_segment(j,:) <= min_val);
% codes(j,min_val_indices) = -1;
% end
% end
% star_column = ((i-1)*K+1);
% end_column = (i*K);
% binary_codes(:,star_column:end_column) = codes;
% end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.