Create a multiclass SVM classification with templateSVM and a custom kernel
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (1)
Shashank Gupta
le 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.
1 commentaire
Voir également
Catégories
En savoir plus sur Classification Ensembles dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!