Create a multiclass SVM classification with templateSVM and a custom kernel
5 views (last 30 days)
Show older comments
hi to everybody,
I would like to build a multiclass SVM classificator (20 different classes) using templateSVM() and chi_squared kernel, but I don't know how to define the custom kernel: I tryin the folowing way:
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
function gram = compute_gram_matrix(U,V)
[sample_U,~] = size(U);
[sample_V,~] = size(V);
gram = zeros(sample_U,sample_V);
for r=1:sample_U
for t =r:sample_V
temp = chi_squared_kernel(U(r,:),V(t,:));
gram(r,t) = temp;
gram(t,r) = temp;
end
end
% calcolo la media
mean = 0;
for i=1:size(gram,1)
for j=1:size(gram,2)
if i <= j
mean = mean + gram(i,j);
else
continue
end
end
end
mean = mean / ((sample_U^2 + sample_U)/2);
gram = exp(-gram/mean);
end
function value = chi_squared_kernel(hist_1,hist_2)
value = 0;
k = size(hist_1,1);
for i=1:k
if hist_1(i)==0 && hist_2(i)==0
continue
else
value = value + (hist_1(i) - hist_2(i))^2 / (hist_1(i) + hist_2(i));
end
end
value = 0.5 * value;
end
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
but it says that the kernel is not of the correct type: How can I do to solve this problem?
0 Comments
Answers (1)
Shashank Gupta
on 18 Nov 2020
Hi Alberto,
I just took your gram matrix and able to define it properly, can you elaborate what you all did? Just for the reference I will attach a piece of code which I used and it worked.
% take some data.
load fisheriris
% define SVM
t = templateSVM('KernelFunction','compute_gram_matrix');
% Specify template t to binary leaner.
Mdl = fitcecoc(meas,species,'Learners',t);
I took your "compute_gram_matrix" function and able to execute it properly without fail. Check out this and let me know if it make sense.
Cheers.
See Also
Categories
Find more on Classification Ensembles in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!