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

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