annotate core/tools/machine_learning/get_itml_deltas.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function [X, C, idx] = get_itml_deltas(r, in)
wolffd@0 2 % [X, C, idx] = get_itml_deltas(r, in)
wolffd@0 3
wolffd@0 4 %ITML Specs
wolffd@0 5 % C: 4 column matrix
wolffd@0 6 % column 1, 2: index of constrained points. Indexes between 1 and n
wolffd@0 7 % column 3: 1 if points are similar, -1 if dissimilar
wolffd@0 8 % column 4: right-hand side (lower or upper bound, depending on
wolffd@0 9 % whether points are similar or dissimilar)
wolffd@0 10 %
wolffd@0 11 % X: (n x m) data matrix - each row corresponds to a single instance
wolffd@0 12 % ---
wolffd@0 13 % NOTE: X is thus input in transposed shape for the ITML algorithm
wolffd@0 14 % ---
wolffd@0 15
wolffd@0 16 % ---
wolffd@0 17 % NOTE: this preallocation is not complete
wolffd@0 18 % ---
wolffd@0 19 X = zeros(size(in,1), 0);
wolffd@0 20 C = zeros(0,4);
wolffd@0 21 idx = zeros(0,2);
wolffd@0 22
wolffd@0 23 for i = 1:size(r,1)
wolffd@0 24
wolffd@0 25 % feature indexing
wolffd@0 26 a = i;
wolffd@0 27
wolffd@0 28 % check if ranking is valid
wolffd@0 29 if ~isempty(r{i,1}) && ~isempty(r{i,2})&& ...
wolffd@0 30 isempty(intersect(r{i,1}, r{i,2}));
wolffd@0 31
wolffd@0 32 % ---
wolffd@0 33 % NOTE / TODO: the follwing is intended for compability
wolffd@0 34 % both sides of the ranking may have more than one entry.
wolffd@0 35 % for the MTT database, the ranking may be correct, but the
wolffd@0 36 % inequalities build from non-singular rankings are not
wolffd@0 37 % based on the actual data
wolffd@0 38 % ---
wolffd@0 39 for j = 1:numel(r{i,1})
wolffd@0 40 b = r{i,1}(j);
wolffd@0 41
wolffd@0 42 for k = 1:numel(r{i,2})
wolffd@0 43 c = r{i,2}(k);
wolffd@0 44
wolffd@0 45 % ---
wolffd@0 46 % get vector deltas
wolffd@0 47 % ---
wolffd@0 48 [dab] = in(:,a) - in(:,b);
wolffd@0 49 [dac] = in(:,a) - in(:,c);
wolffd@0 50
wolffd@0 51 % ---
wolffd@0 52 % save deltas in new feature matrix
wolffd@0 53 % TODO: this method has duplicate entries
wolffd@0 54 % if the pairs appear more than once
wolffd@0 55 % index the data set and use more efficiently!!!
wolffd@0 56 % ---
wolffd@0 57 X = [X dab];
wolffd@0 58 idx(end+1,:) = [a b];
wolffd@0 59 iab = size(idx, 1);
wolffd@0 60
wolffd@0 61 X = [X dac];
wolffd@0 62 idx(end+1,:) = [a c];
wolffd@0 63 iac = size(idx, 1);
wolffd@0 64
wolffd@0 65 % ---
wolffd@0 66 % NOTE:
wolffd@0 67 % in terms of the constraint,
wolffd@0 68 % this should mean: dac - dab >= 1
wolffd@0 69 %
wolffd@0 70 % 4th position cannot be 0, converges to Inf if > 1
wolffd@0 71 % -1,-1 learns the opposite of what constraitns say
wolffd@0 72 % ---
wolffd@0 73 C(end+1, :) = [iab iac -1 -1];
wolffd@0 74 end
wolffd@0 75 end
wolffd@0 76 end
wolffd@0 77 end
wolffd@0 78
wolffd@0 79 % % ---
wolffd@0 80 % % NOTE: here, we transpose the X for usage i nthe training
wolffd@0 81 % % ---
wolffd@0 82 % X = X';
wolffd@0 83
wolffd@0 84 end
wolffd@0 85