我想创建一个基于"支持向量机和自定义内核的分类器"概念。我在这里a link跟着它创建了自己的分类器。
在每件事情之前,我必须说在我的项目中我已经使用this dataset用于图像分割。作为我的代码的额外信息,我已将此数据集分段为如下结构:
我的代码:
% clc;clear;close all; %// not generally appreciated
tic;
load('dataset.mat');
% When you load this dataset, there are two matrices, data_train which contains my training data with above attribute and data_test is so.
X_train = data_train;
X_test = data_test;
[train_size, feature_size, class_size] = size(X_train);
[test_size] = size(X_test,1);
x = [];
for index=1:class_size
x=[x;X_test(:,:,index)];
end
X_test = x;
correct_label = [];
for index=1:class_size
correct_label = [correct_label;index*ones(test_size,1)];
end
g = [];
C = 1;
for i=class_size:-1:1
for j=1:(i-1)
train = [X_train(:,:,j);X_train(:,:,i)];
y = [j*ones(train_size,1);i*ones(train_size,1)];
svmModel = fitcsvm(train, y,'Standardize',true,'KernelFunction','GHI_Kernel');
group = predict(svmModel, X_test);
g = [g,group];
end
end
temp = zeros(size(X_test,1),class_size);
n = size(X_test,1);
for i=1:n
for j=1:class_size
temp(i,j) = sum(((g(i,:)==j)')); %'//
end
end
[val, esm] = max(temp,[],2);
CCR = sum(esm == correct_label)/size(correct_label,1);
error = 1 - CCR;
confusion_matrix = confusionmat(esm, correct_label);
toc;
这是我的自定义内核函数:
function output = GHI_Kernel(u, v)
beta = 2;
u=u.^beta;
v=v.^beta;
test = min(u,v);
output = sum(test,2);
end
通过这种编码,我认为我的实现没有问题,但是,当我使用自定义数据集在MATLAB中运行此代码时,我遇到了这个问题:
Error using solve
Function GHI_Kernel errors with the
following message: Matrix dimensions must
agree.
Error in
classreg.learning.impl.SVMImpl.make (line 417 [alphas,active,grad,bias,nIter,...
Error in ClassificationSVM (line 319)
this.Impl =
classreg.learning.impl.SVMImpl.make(...
Error in classreg.learning.FitTemplate/fit (line 251)
obj =
this.MakeFitObject(X,Y,W,this.ModelParams,fitArgs{:});
Error in ClassificationSVM.fit (line 237) this = fit(temp,X,Y);
Error in fitcsvm (line 279)
obj =
ClassificationSVM.fit(X,Y,varargin{:});
Error in Problem1_svm_one_vs_one (line 31)
svmModel = fitcsvm(train, y,'Standardize',true,'KernelFunction','GHI_Kernel');
作为第一次编辑,我修复了我自己的数据集链接而不是UCI存储库。
答案 0 :(得分:0)
可以找到正确的文档here。特别是,对于自定义内核函数,它声明了
您可以通过设置来设置自己的内核功能,例如内核 ' KernelFunction''内核&#39 ;. kernel必须具有以下形式:
函数G =内核(U,V)
其中:
U is an m-by-p matrix. V is an n-by-p matrix. G is an m-by-n Gram matrix of the rows of U and V.
因此,如果您的内核函数GHI_Kernel
获得两个19x1向量u
和v
,那么它应返回一个19x19矩阵。现在它看起来只是输出一个19x1向量。