Create a multiclass SVM classification with templateSVM and a custom kernel
9 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (1 件)
Shashank Gupta
2020 年 11 月 18 日
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.
参考
カテゴリ
Help Center および File Exchange で Classification Ensembles についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!