Daniel@0: function [X, C, idx] = get_itml_deltas(r, in) Daniel@0: % [X, C, idx] = get_itml_deltas(r, in) Daniel@0: Daniel@0: %ITML Specs Daniel@0: % C: 4 column matrix Daniel@0: % column 1, 2: index of constrained points. Indexes between 1 and n Daniel@0: % column 3: 1 if points are similar, -1 if dissimilar Daniel@0: % column 4: right-hand side (lower or upper bound, depending on Daniel@0: % whether points are similar or dissimilar) Daniel@0: % Daniel@0: % X: (n x m) data matrix - each row corresponds to a single instance Daniel@0: % --- Daniel@0: % NOTE: X is thus input in transposed shape for the ITML algorithm Daniel@0: % --- Daniel@0: Daniel@0: % --- Daniel@0: % NOTE: this preallocation is not complete Daniel@0: % --- Daniel@0: X = zeros(size(in,1), 0); Daniel@0: C = zeros(0,4); Daniel@0: idx = zeros(0,2); Daniel@0: Daniel@0: for i = 1:size(r,1) Daniel@0: Daniel@0: % feature indexing Daniel@0: a = i; Daniel@0: Daniel@0: % check if ranking is valid Daniel@0: if ~isempty(r{i,1}) && ~isempty(r{i,2})&& ... Daniel@0: isempty(intersect(r{i,1}, r{i,2})); Daniel@0: Daniel@0: % --- Daniel@0: % NOTE / TODO: the follwing is intended for compability Daniel@0: % both sides of the ranking may have more than one entry. Daniel@0: % for the MTT database, the ranking may be correct, but the Daniel@0: % inequalities build from non-singular rankings are not Daniel@0: % based on the actual data Daniel@0: % --- Daniel@0: for j = 1:numel(r{i,1}) Daniel@0: b = r{i,1}(j); Daniel@0: Daniel@0: for k = 1:numel(r{i,2}) Daniel@0: c = r{i,2}(k); Daniel@0: Daniel@0: % --- Daniel@0: % get vector deltas Daniel@0: % --- Daniel@0: [dab] = in(:,a) - in(:,b); Daniel@0: [dac] = in(:,a) - in(:,c); Daniel@0: Daniel@0: % --- Daniel@0: % save deltas in new feature matrix Daniel@0: % TODO: this method has duplicate entries Daniel@0: % if the pairs appear more than once Daniel@0: % index the data set and use more efficiently!!! Daniel@0: % --- Daniel@0: X = [X dab]; Daniel@0: idx(end+1,:) = [a b]; Daniel@0: iab = size(idx, 1); Daniel@0: Daniel@0: X = [X dac]; Daniel@0: idx(end+1,:) = [a c]; Daniel@0: iac = size(idx, 1); Daniel@0: Daniel@0: % --- Daniel@0: % NOTE: Daniel@0: % in terms of the constraint, Daniel@0: % this should mean: dac - dab >= 1 Daniel@0: % Daniel@0: % 4th position cannot be 0, converges to Inf if > 1 Daniel@0: % -1,-1 learns the opposite of what constraitns say Daniel@0: % --- Daniel@0: C(end+1, :) = [iab iac -1 -1]; Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: % % --- Daniel@0: % % NOTE: here, we transpose the X for usage i nthe training Daniel@0: % % --- Daniel@0: % X = X'; Daniel@0: Daniel@0: end Daniel@0: