function model = llsvm(x,r,y) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%% a simple implementation of LL-SVM %%% (Ladický and Torr, Locally Linear Support Vector Machines, ICML'11) %%% %%% inputs: %%% x: input data (D1*N matrix, a vector for each data) %%% r: the coordinate coefficients (D2*N matrix, a vector for each data) %%% y: data labels (1*N vector, 1...L) %%% outputs: %%% model: multiclass svm model (learned by liblinear) %%% %%% NOTICE: %%% (1) In the original paper, the margin is defined as H(x)=r(x)^T*W*x+r(x)^T*b, %%% where r(x) is a vector, W is a matrix, x is a vector, and b is a vector. %%% Here, I represent each data x as a matrix x=r(x)*x^T and vectorize it, %%% and correspondingly W is represented as a long vector, and b=r(x)^T*b. %%% (2) Here liblinear toolbox (Fan et. al., LIBLINEAR: A library for large %%% linear classification,JMLR'08) is used instead of stochastic gradient decent %%% to learn W and b. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%% represent the data for learning [D1 N] = size(x); [D2 N] = size(r); d = zeros(D1*D2,N,'single'); for i = 1:N X = x(:,i)*r(:,i)'; d(:,i) = X(:); end %%% multiclass svm (liblinear, parameters should be tuned) L = max(y); h = hist(y,1:L); h = max(h)./h; str = []; for i = 1:L str = [str,' -w',num2str(i),' ',num2str(h(i))]; end d = sparse(double(d')); model = train(double(y'),d,['-s 1 -B 1 -c 1e1',str]); %%% classification can be performed using the "predict" function in the %%% liblinear toolbox return