implementation of bilinear / tensor svm / support tensor machine
显示 更早的评论
I am trying to implement the tensor svm as described in Cai in his paper Support Tensor Machines for Text Categorization in order to extract the weights for use with another classifier. The paper computes bilinear weights iteratively and shows to converge. I am trying to replicate the approach using fitcsvm but am not sure how to show whether the weights converge or not. Moreover, the weights cause my classifier to do less well than if I had not used them; which is not the result that I expected. I would appreciate any help with my code.
Support Tensor Machine is a tensor generalization of SVM. The algorithmic procedure from the paper is:
1. Initialization: Let 

2. Computing v: Let
and
, v be computed by solving the following optimization problem:
and
, v be computed by solving the following optimization problem: subject to 


3. Computing u: Once v is obtained, let
and
. u can be computed by solving the following optimization problem:
and
. u can be computed by solving the following optimization problem: subject to 


4. Iteratively computing u and v: By step 2 and 3, we can iteratively compute u and v until they tend to converge.
In my implementation, each tensor
is a 13x15 matrix containing features extracted from skeletal joints extracted from a video sequence (13 joints, 15 temporal windows). I have 100 training examples which are stored in the matrix train. Train has 16 columns; columns 1 and 2 both contain the label, column 3 the video sequence and columns 4:16 the features; the features from each video sequence are concatenated together within train. As I have 8 classes (labeled 1:8), I am using one-versus-all SVM for each class.
is a 13x15 matrix containing features extracted from skeletal joints extracted from a video sequence (13 joints, 15 temporal windows). I have 100 training examples which are stored in the matrix train. Train has 16 columns; columns 1 and 2 both contain the label, column 3 the video sequence and columns 4:16 the features; the features from each video sequence are concatenated together within train. As I have 8 classes (labeled 1:8), I am using one-versus-all SVM for each class.I have given my code below. I suspect that my problem has something to do with beta_one, but I can't get it to work. Any help would be most appreciated!
many thanks. Fiona
train = csvread('train.csv');
v = ones(8,15);
u = NaN(8,39);
% fix v
for iteration = 1:5
iteration
v_fixed = nan(222,42);
for i = 1:max(train(:,3)) % select each matrix of video sequence one-by-one
video = train(train(:,3)==i,:);
if ~isempty(video)
action = video(1,1);
v_fixed(i,1:3) = video(1,1:3);
v_fixed(i,4:end) = video(:,4:end)'*v(action,:)'; % temporal weights are fixed, creating a single vector for each video
end
end
v_fixed(isnan(v_fixed(:,1)),:) =[]; %remove empty rows
v_fixed(v_fixed(:,1)==0,:) =[];
for j = 1:8 % find weights for each class using one-versus-all
classes = [1:8];
trainz = v_fixed;
leaveout = classes;
leaveout(leaveout(j)) = [];
for out = leaveout
trainz(trainz(:,1)==out,1) = -1;
end
trainz(trainz(:,1)==j,1) = 1;
mdl = fitcsvm(trainz(:,4:end),trainz(:,1));
u(j,:) = mdl.Beta; %extract the linear spatial weight
u(j,:) = u(j,:)/sqrt(beta_one(j)); %conver to bilinear weight
u(j,:) = abs(u(j,:))./sum(abs(u(j,:)),2); %restrict the weights to a range between 0 and 1
end
beta_two = sum(u.^2,2);
% fix u
u_fixed = nan(222,18);
for i = 1:max(nn_train(:,3))
video = train(train(:,3)==i,:);
if ~isempty(video)
action = video(1,1);
u_fixed(i,1:3) = video(1,1:3);
u_fixed(i,4:end) = video(:,4:end)*u(action,:)';
end
end
u_fixed(isnan(u_fixed(:,1)),:) =[];
v = NaN(8,15);
classes = [1:8];
% for j = classes
for j = 1:8
trainz = u_fixed;
leaveout = classes;
leaveout(leaveout(j)) = [];
for out = leaveout
trainz(trainz(:,1)==out,1) = -1;
end
trainz(trainz(:,1)==j,1) = 1;
mdl = fitcsvm(trainz(:,4:end),trainz(:,1));
v(j,:) = mdl.Beta;
v(j,:) = v(j,:)/sqrt(beta_two(j));
v(j,:) = abs(v(j,:))./sum(abs(v(j,:)),2);
end
beta_one = sum(v.^2,2);
end
回答(1 个)
wejdan binduaij
2022-2-20
0 个投票
train = csvread('train.csv');
v = ones(8,15);
u = NaN(8,39);
% fix v
for iteration = 1:5
iteration
v_fixed = nan(222,42);
for i = 1:max(train(:,3)) % select each matrix of video sequence one-by-one
video = train(train(:,3)==i,:);
if ~isempty(video)
action = video(1,1);
v_fixed(i,1:3) = video(1,1:3);
v_fixed(i,4:end) = video(:,4:end)'*v(action,:)'; % temporal weights are fixed, creating a single vector for each video
end
end
v_fixed(isnan(v_fixed(:,1)),:) =[]; %remove empty rows
v_fixed(v_fixed(:,1)==0,:) =[];
for j = 1:8 % find weights for each class using one-versus-all
classes = [1:8];
trainz = v_fixed;
leaveout = classes;
leaveout(leaveout(j)) = [];
for out = leaveout
trainz(trainz(:,1)==out,1) = -1;
end
trainz(trainz(:,1)==j,1) = 1;
mdl = fitcsvm(trainz(:,4:end),trainz(:,1));
u(j,:) = mdl.Beta; %extract the linear spatial weight
u(j,:) = u(j,:)/sqrt(beta_one(j)); %conver to bilinear weight
u(j,:) = abs(u(j,:))./sum(abs(u(j,:)),2); %restrict the weights to a range between 0 and 1
end
beta_two = sum(u.^2,2);
% fix u
u_fixed = nan(222,18);
for i = 1:max(nn_train(:,3))
video = train(train(:,3)==i,:);
if ~isempty(video)
action = video(1,1);
u_fixed(i,1:3) = video(1,1:3);
u_fixed(i,4:end) = video(:,4:end)*u(action,:)';
end
end
u_fixed(isnan(u_fixed(:,1)),:) =[];
v = NaN(8,15);
classes = [1:8];
% for j = classes
for j = 1:8
trainz = u_fixed;
leaveout = classes;
leaveout(leaveout(j)) = [];
for out = leaveout
trainz(trainz(:,1)==out,1) = -1;
end
trainz(trainz(:,1)==j,1) = 1;
mdl = fitcsvm(trainz(:,4:end),trainz(:,1));
v(j,:) = mdl.Beta;
v(j,:) = v(j,:)/sqrt(beta_two(j));
v(j,:) = abs(v(j,:))./sum(abs(v(j,:)),2);
end
beta_one = sum(v.^2,2);
end
类别
在 帮助中心 和 File Exchange 中查找有关 Construct and Work with Object Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!