LibLinear(SVM包)使用說明之(三)實踐

2022-03-16 04:20:28 字數 3970 閱讀 1382

liblinear(svm包)使用說明之(三)實踐

liblinear(svm包)使用說明之(三)實踐

[email protected]

我們在ufldl的教程中,exercise: convolution and pooling這一章節,已經得到了cnnpooledfeatures.mat特徵。在該練習中,我們使用的是softmax分類器來分類的。在這裡我們修改為用svm來替代softmax分類器。svm由liblinear軟體包來提供。這裡是四分類問題,所以liblinear會根據我們傳入的訓練樣本訓練四個二分類器,以實現四分類。以前由softmax分類器得到的準確率是80.406%。在這裡換成liblinear後,準確率變為80.75%。在這裡差別不是很大。

在本文的例子中,我們增加了scale和cross validation,cross validation是用來選擇乙個最好的引數c的(不知道自己這兩個步驟有沒有正確,如有錯誤,還望大家提醒,謝謝)。

具體的**如下:

[plain]view plain

copy

%// classification by liblinear  

%// liblinear:   

%// author : zouxy  

%// date   : 2013-9-2  

%// homepage :   

%// email  : [email protected]  

clear; clc;  

%%% step1: load data  

fprintf(1,'step1: load data...\n');  

% pooledfeaturestrain大小為400*2000*3*3  

% pooledfeaturestest大小為400*3200*3*3  

% 第一維是特徵個數,也就是特徵圖個數,第二維是樣本個數,第三維是特徵圖的寬,  

% 第四維是特徵圖的高  

load cnnpooledfeatures.mat;  

load stltrainsubset.mat % loads numtrainimages, trainimages, trainlabels  

load stltestsubset.mat  % loads numtestimages,  testimages,  testlabels  

% b = permute(a,order) 按照向量order指定的順序重排a的各維  

train_x = permute(pooledfeaturestrain, [1 3 4 2]);  

% 將每個樣本的特徵拉成乙個列向量,每個樣本乙個列,矩陣大小為3600*2000  

train_x = reshape(train_x, numel(pooledfeaturestrain) / numtrainimages, numtrainimages);  

train_y = trainlabels; % 2000*1  

test_x = permute(pooledfeaturestest, [1 3 4 2]);  

test_x = reshape(test_x, numel(pooledfeaturestest) / numtestimages, numtestimages);  

test_y = testlabels;  

% release some memory  

clear trainimages testimages pooledfeaturestrain pooledfeaturestest;  

%%% step2: scale the data  

fprintf(1,'step2: scale data...\n');  

% using the same scaling factors for training and testing sets,   

% we obtain much better accuracy. note: scale each attribute(feature), not sample  

% scale to [0 1]  

% when a is a vector, b = (a - min(a)) .* (upper - lower) ./ (max(a)-min(a)) + lower  

lower = 0;  

upper = 1.0;  

train_x = train_x';  

x_max = max(train_x);  

x_min = min(train_x);  

train_x = (train_x - repmat(x_min, size(train_x, 1), 1)) .* (upper - lower) ...  

./ repmat((x_max - x_min), size(train_x, 1), 1) + lower;  

test_x = test_x';  

test_x = (test_x - repmat(x_min, size(test_x, 1), 1)) .* (upper - lower) ...  

./ repmat((x_max - x_min), size(test_x, 1), 1) + lower;  

% note: before scale the accuracy is 80.4688%, after scale it turns to 80.1875%,  

% and took more time. so is that my scale operation wrong or other reasons?  

% after adding bias, accuracy = 80.75% (2584/3200)  

%%% step3: cross validation for choosing parameter  

fprintf(1,'step3: cross validation for choosing parameter c...\n');  

% the larger c is, more time should be costed  

c = [2^-6 2^-5 2^-4 2^-3 2^-2 2^-1 2^0 2^1 2^2 2^3];  

max_acc = 0;  

tic;  

for i = 1 : size(c, 2)  

option = ['-b 1 -c ' num2str(c(i)) ' -v 5 -q'];  

fprintf(1,'stage: %d/%d: c = %d, ', i, size(c, 2), c(i));  

accuracy = train(train_y, sparse(train_x), option);   

if accuracy > max_acc  

max_acc = accuracy;  

best_c = i;  

end  

end  

fprintf(1,'the best c is c = %d.\n', c(best_c));  

toc;  

%%% step4: train the model  

fprintf(1,'step4: training...\n');  

tic;  

option = ['-c ' num2str(c(best_c)) ' -b 1 -e 0.001'];  

model = train(train_y, sparse(train_x), option);  

toc;  

%%% step5: test the model  

fprintf(1,'step5: testing...\n');  

tic;  

[predict_label, accuracy, dec_values] = predict(test_y, sparse(test_x), model);  

toc;  

r mysql包 R使用RMySQL包

經常在網上看到,很多人問如何在r中如何連線資料庫,尤其是如何使用rmysql包的問題。這些問題主要分為兩部分 一是 安裝不上 二是安裝上了 結果顯示是中文亂碼或者是問號。我當初也被這種問題困擾過一段時間,所以現在簡單總結一下 希望能夠幫助部分人解決問題。首先如何載入rmysql包 細分幾個步驟 1,...

DBMS RANDOM包的使用

7月18日 dbms random包的使用 1 function value return number select dbms random.value from dual 返回乙個大於或等於 0 且小於 1 的隨機數 2 function value low in number,high in ...

javascript閉包使用

之前看到一段 很是不能理解,然後就查詢資料並且找網路上得大牛請教,最後弄懂了這段 然後就拿出來總結一下。先來看一段 var arrtest for var i 0 i 3 i arrtest function function function console.log arrtest 0 tostr...