liblinear java_LibLinear(SVM包)使用说明之(三)实践

LibLinear(SVM包)使用说明之(三)实践

我们在 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 的(不知道自己这两个步骤有没有正确,如有错误,还望大家提醒,谢谢)。

具体的代码如下:

%// Classification by LibLinear%// LibLinear: http://www.csie.ntu.edu.tw/~cjlin/liblinear/%// Author : zouxy%// Date : 2013-9-2%// HomePage : http://blog.csdn.net/zouxy09%// Email : zouxy09@qq.comclear; clc;%%% step1: load datafprintf(1,'step1: Load data...\n');% pooledFeaturesTrain大小为400*2000*3*3% pooledFeaturesTest大小为400*3200*3*3% 第一维是特征个数,也就是特征图个数,第二维是样本个数,第三维是特征图的宽,% 第四维是特征图的高load cnnPooledFeatures.mat;load stlTrainSubset.mat % loads numTrainImages, trainImages, trainLabelsload stlTestSubset.mat % loads numTestImages, testImages, testLabels% B = permute(A,order) 按照向量order指定的顺序重排A的各维train_X = permute(pooledFeaturesTrain, [1 3 4 2]);% 将每个样本的特征拉成一个列向量,每个样本一个列,矩阵大小为3600*2000train_X = reshape(train_X, numel(pooledFeaturesTrain) / numTrainImages, numTrainImages);train_Y = trainLabels; % 2000*1test_X = permute(pooledFeaturesTest, [1 3 4 2]);test_X = reshape(test_X, numel(pooledFeaturesTest) / numTestImages, numTestImages);test_Y = testLabels;% release some memoryclear trainImages testImages pooledFeaturesTrain pooledFeaturesTest;%%% step2: scale the datafprintf(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)) + lowerlower = 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 parameterfprintf(1,'step3: Cross Validation for choosing parameter c...\n');% the larger c is, more time should be costedc = [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_accmax_acc = accuracy;best_c = i;endendfprintf(1,'The best c is c = %d.\n', c(best_c));toc;%%% step4: train the modelfprintf(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 modelfprintf(1,'step5: Testing...\n');tic;[predict_label, accuracy, dec_values] = predict(test_Y, sparse(test_X), model);toc;

欢迎加入我爱机器学习QQ14群:336582044

getqrcode.jpg

微信扫一扫,关注我爱机器学习公众号

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值