Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
comik/computeInstanceWideKernel.m
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
92 lines (86 sloc)
3.41 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function [instanceStarts, instanceEnds, instanceWideKernel] = computeInstanceWideKernel(allSeqsAsBags, thisInstances, sparseComputation, debugLevel, debugMsgLocation) | |
% COMPUTEINSTANCEWIDEKERNEL | |
% Computes the instance-wide kernel matrix given a bag of instances | |
% | |
% INPUT PARAMS | |
% Param 'allSeqsAsBags' (cell array) | |
% Collection of all segments per sequence | |
% | |
% Param 'thisInstances' (vector) | |
% #instances in each bag (in other words, #segments per sequence) | |
% | |
% Param 'sparseComputation' (0/1) | |
% Flag specifying whether sparseComputation is to be performed (0/1) | |
% | |
% Param 'debugLevel' (0/1/2) | |
% | |
% Param 'debugMsgLocation' (1/fileID) | |
% | |
% | |
% OUTPUT PARAMS | |
% Param 'instanceStarts' (vector) | |
% Indexes in the vector where instances (segments) of any sequence begin | |
% | |
% Param 'instanceEnds' (vector) | |
% Indexes in the vector where istances (segments) of any sequence end | |
% | |
% Param 'instanceWideKernel' (matrix) | |
% The instance-wide kernel matrix of dimension #Instances x #Instances | |
% | |
% ADDITIONAL NOTES | |
% | |
% Author: snikumbh@mpi-inf.mpg.de | |
if nargin < 3 | |
debugMsgLocation = 1; | |
debugLevel = 2; | |
sparseComputation = 0; | |
end | |
logMessages(debugMsgLocation, sprintf('Onto compute the Std MI kernel\n'), debugLevel); | |
nBags = length(allSeqsAsBags); | |
tic; | |
instanceEnds = cumsum(thisInstances); | |
instanceStarts = cumsum(thisInstances) - thisInstances + 1; | |
nInstances = instanceEnds(end); | |
logMessages(debugMsgLocation, sprintf('instances starts and ends collected in %.4f seconds\n', toc), debugLevel); | |
if sparseComputation | |
logMessages(debugMsgLocation, sprintf('Performing sparse computation for kernel on instances\n'), debugLevel); | |
tic; | |
allSeqsAsMat = zeros( size( allSeqsAsBags{1}, 1) , nInstances); | |
for i=1:nBags | |
allSeqsAsMat(:, instanceStarts(i): instanceEnds(i)) = allSeqsAsBags{i}; | |
end | |
logMessages(debugMsgLocation, sprintf('Loop converting cells to matrix completed in %.4f seconds\n', toc), debugLevel); | |
% Above is a looping version instead of cell2mat (see next line). This takes | |
% nearly half the time for really long sequences and too many instances. | |
% tic; allSeqsAsMat = cell2mat(allSeqsAsBags); toc; | |
tic; | |
instanceWideKernel = full(allSeqsAsMat' * allSeqsAsMat); | |
logMessages(debugMsgLocation, sprintf('full kernel computed in %.4f seconds\n', toc), debugLevel); | |
% nBags x nBags kernel actually need not be computed | |
clear allSeqsAsMat; | |
% | |
else | |
% direct (not sparseComputation) | |
% This is tested neither for correctness nor efficiency. We recommend using sparseComputation. | |
logMessages(debugMsgLocation, sprintf('Computing directly, %d rows.\n', nBags), debugLevel); | |
stdMultiInstanceKernel = zeros(nBags); | |
for i=1:nBags | |
if mod(i, 10) == 0 | |
logMessages(debugMsgLocation, sprintf('%d, ', i), debugLevel); | |
end | |
iInstances = size(allSeqsAsBags{i}, 2); | |
for j=i:nBags | |
term = transpose(allSeqsAsBags{i}) * allSeqsAsBags{j}; | |
jInstances = size(allSeqsAsBags{j}, 2); | |
stdMultiInstanceKernel(i,j) = sum(term(:)) * (1/iInstances) * (1/jInstances); | |
end | |
end | |
logMessages(debugMsgLocation, sprintf('\n'), debugLevel); | |
% Fill-up the other half | |
stdMultiInstanceKernel = triu(stdMultiInstanceKernel, 1) + stdMultiInstanceKernel'; | |
% Normalize the kernel matrix -- done by calling normalizeKernel | |
stdMultiInstanceKernel = normalizeKernel(stdMultiInstanceKernel); | |
% | |
% | |
end %if-else ends | |
end % function ends |