LibSVM
RBF kernel을 사용할 때 한가지 tip은
svm_type = ['-t 2 -h 0 -g ' num2str(1/xmax/xmax) '-q']; % <= RBF 커널
다음과 같이 하는 것이다. 즉 rbf kernel function의 파라미터를 1/max/max 로 할 경우 input을 normalize할 필요가 없다.
xmax, ymax가 10일 때
xmax, ymax가 1일 때
xmax, ymax 에 따른 RBF kernel 결과 변화
모두 동일한 파라미터를 사용했다. input space에 kernel svm이 민감함을 알 수 있다.
코드
%% Libsvm
clc;
clear all;
close all;
addpath('../../Solvers/libsvm-3.14');
%% 그림 그릴지 여부
plot_train = 0;
kernel_type = 2; % 0: linear // 2: RBF
%% Training data를 만든다.
rng(20);
xmax = 1; ymax = xmax;
nr_class = 5;
class_mean = zeros(nr_class, 2);
for i = 1:nr_class
class_mean(i, :) = [xmax*rand ymax*rand];
end
nr_data_per_class = 20;
train_input = zeros(nr_class*nr_data_per_class, 2);
train_label = zeros(nr_class*nr_data_per_class, 1);
for i = 1:nr_class
for j = 1:nr_data_per_class
idx = j + nr_data_per_class*(i-1);
train_input(idx, :) = class_mean(i, :) + xmax/10*randn(1, 2);
train_label(idx, :) = i;
end
end
% 그려본다.
if plot_train
colors = varyColor(nr_class);
figure(1); hold on;
for i = 1:nr_class
for j = 1:nr_data_per_class
idx = j + nr_data_per_class*(i-1);
plot(train_input(idx, 1), train_input(idx, 2), 'o' ...
, 'Color', colors(i, :) ...
, 'MarkerFaceColor', colors(i, :));
end
end
hold off; axis equal; axis([0 xmax 0 ymax]);
end
%% SVM을 학습한다.
% -s svm_type : set type of SVM (default 0)
% 0 -- C-SVC (multi-class classification)
% 1 -- nu-SVC (multi-class classification)
% 2 -- one-class SVM
% 3 -- epsilon-SVR (regression)
% 4 -- nu-SVR (regression)
% -t kernel_type : set type of kernel function (default 2)
% 0 -- linear: u'*v
% 1 -- polynomial: (gamma*u'*v + coef0)^degree
% 2 -- radial basis function: exp(-gamma*|u-v|^2)
% 3 -- sigmoid: tanh(gamma*u'*v + coef0)
% 4 -- precomputed kernel (kernel values in training_set_file)
% -d degree : set degree in kernel function (default 3)
% -g gamma : set gamma in kernel function (default 1/num_features)
% -r coef0 : set coef0 in kernel function (default 0)
% -c cost : set the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1)
% -n nu : set the parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5)
% -p epsilon : set the epsilon in loss function of epsilon-SVR (default 0.1)
% -m cachesize : set cache memory size in MB (default 100)
% -e epsilon : set tolerance of termination criterion (default 0.001)
% -h shrinking : whether to use the shrinking heuristics, 0 or 1 (default 1)
% -b probability_estimates : whether to train a SVC or SVR model for probability estimates, 0 or 1 (default 0)
% -wi weight : set the parameter C of class i to weight*C, for C-SVC (default 1)
% -v n: n-fold cross validation mode
% -q : quiet mode (no outputs)
% kernel_type = 2; % 0: linear // 2: RBF
svm_type = ['-t ' num2str(kernel_type) ' -h 0 -g ' num2str(1/xmax/xmax) '-q']; % <= RBF 커널
model = svmtrain( train_label, train_input, svm_type);
%% SVM을 테스트 한다.
stage.xmin = 0;
stage.xmax = xmax;
stage.ymin = 0;
stage.ymax = ymax;
stage.xres = 40;
stage.yres = 40;
stage.xgrid = linspace(stage.xmin, stage.xmax, stage.xres);
stage.ygrid = linspace(stage.ymin, stage.ymax, stage.yres);
[mesh_X, mesh_Y] = meshgrid(stage.xgrid, stage.ygrid);
stage.points = [mesh_X(:) mesh_Y(:)];
stage.nr_points = length(stage.points);
field.zdata = zeros(stage.yres, stage.xres);
for xi = 1:stage.xres
for yi = 1:stage.yres
x = stage.xgrid(xi);
y = stage.ygrid(yi);
predict_label = svmpredict( 1, [x y], model);
field.zdata(yi, xi) = predict_label;
end
end
% 그림을 그린다.
colors = varyColor(nr_class);
fig = figure('Position', [400 300 500 500]);
subaxes(fig, 1, 1, 1); hold on;
pcolor(stage.xgrid, stage.ygrid, field.zdata); % colorbar;
% shading interp; % lighting phong;
for i = 1:nr_class
for j = 1:nr_data_per_class
idx = j + nr_data_per_class*(i-1);
plot(train_input(idx, 1), train_input(idx, 2), 'o' ...
, 'Color', 'k' ...
, 'MarkerFaceColor', colors(i, :) ...
, 'MarkerSize', 11 );
end
end
hold off; axis equal; axis([0 xmax 0 ymax]);
if kernel_type == 0
title('Linear SVM Result' ...
, 'FontSize', 15);
elseif kernel_type == 2
title('Kernel (RBF) SVM Result' ...
, 'FontSize', 15);
else
title('SVM Result' ...
, 'FontSize', 15);
end
'Enginius > Matlab' 카테고리의 다른 글
두 선분 사이의 각도를 구해보자. (5) | 2014.03.10 |
---|---|
[cprintf] colored print in console (0) | 2014.02.20 |
figure에서 동영상 저장하기 (2) | 2014.01.14 |
Simple Loop Indicator (0) | 2013.11.24 |
Reduce the Gap between SUBPLOT (subaxes) (3) | 2013.10.18 |