zoukankan      html  css  js  c++  java
  • LibLinear(SVM包)使用说明之(三)实践

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

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

    zouxy09@qq.com

    http://blog.csdn.net/zouxy09

              我们在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 plaincopy
     
    1. %// Classification by LibLinear  
    2. %// LibLinear: http://www.csie.ntu.edu.tw/~cjlin/liblinear/  
    3. %// Author : zouxy  
    4. %// Date   : 2013-9-2  
    5. %// HomePage : http://blog.csdn.net/zouxy09  
    6. %// Email  : zouxy09@qq.com  
    7.   
    8. clear; clc;  
    9.   
    10. %%% step1: load data  
    11. fprintf(1,'step1: Load data... ');  
    12. % pooledFeaturesTrain大小为400*2000*3*3  
    13. % pooledFeaturesTest大小为400*3200*3*3  
    14. % 第一维是特征个数,也就是特征图个数,第二维是样本个数,第三维是特征图的宽,  
    15. % 第四维是特征图的高  
    16. load cnnPooledFeatures.mat;  
    17. load stlTrainSubset.mat % loads numTrainImages, trainImages, trainLabels  
    18. load stlTestSubset.mat  % loads numTestImages,  testImages,  testLabels  
    19.   
    20. % B = permute(A,order) 按照向量order指定的顺序重排A的各维  
    21. train_X = permute(pooledFeaturesTrain, [1 3 4 2]);  
    22. % 将每个样本的特征拉成一个列向量,每个样本一个列,矩阵大小为3600*2000  
    23. train_X = reshape(train_X, numel(pooledFeaturesTrain) / numTrainImages, numTrainImages);  
    24. train_Y = trainLabels; % 2000*1  
    25.   
    26. test_X = permute(pooledFeaturesTest, [1 3 4 2]);  
    27. test_X = reshape(test_X, numel(pooledFeaturesTest) / numTestImages, numTestImages);  
    28. test_Y = testLabels;  
    29. % release some memory  
    30. clear trainImages testImages pooledFeaturesTrain pooledFeaturesTest;  
    31.   
    32. %%% step2: scale the data  
    33. fprintf(1,'step2: Scale data... ');  
    34. % Using the same scaling factors for training and testing sets,   
    35. % we obtain much better accuracy. Note: scale each attribute(feature), not sample  
    36. % scale to [0 1]  
    37. % when a is a vector, b = (a - min(a)) .* (upper - lower) ./ (max(a)-min(a)) + lower  
    38. lower = 0;  
    39. upper = 1.0;  
    40. train_X = train_X';  
    41. X_max = max(train_X);  
    42. X_min = min(train_X);  
    43. train_X = (train_X - repmat(X_min, size(train_X, 1), 1)) .* (upper - lower) ...  
    44.             ./ repmat((X_max - X_min), size(train_X, 1), 1) + lower;  
    45. test_X = test_X';  
    46. test_X = (test_X - repmat(X_min, size(test_X, 1), 1)) .* (upper - lower) ...  
    47.             ./ repmat((X_max - X_min), size(test_X, 1), 1) + lower;  
    48. % Note: before scale the accuracy is 80.4688%, after scale it turns to 80.1875%,  
    49. % and took more time. So is that my scale operation wrong or other reasons?  
    50. % After adding bias, Accuracy = 80.75% (2584/3200)  
    51.   
    52. %%% step3: Cross Validation for choosing parameter  
    53. fprintf(1,'step3: Cross Validation for choosing parameter c... ');  
    54. % the larger c is, more time should be costed  
    55. c = [2^-6 2^-5 2^-4 2^-3 2^-2 2^-1 2^0 2^1 2^2 2^3];  
    56. max_acc = 0;  
    57. tic;  
    58. for i = 1 : size(c, 2)  
    59.     option = ['-B 1 -c ' num2str(c(i)) ' -v 5 -q'];  
    60.     fprintf(1,'Stage: %d/%d: c = %d, ', i, size(c, 2), c(i));  
    61.     accuracy = train(train_Y, sparse(train_X), option);   
    62.     if accuracy > max_acc  
    63.         max_acc = accuracy;  
    64.         best_c = i;  
    65.     end  
    66. end  
    67. fprintf(1,'The best c is c = %d. ', c(best_c));  
    68. toc;  
    69.   
    70. %%% step4: train the model  
    71. fprintf(1,'step4: Training... ');  
    72. tic;  
    73. option = ['-c ' num2str(c(best_c)) ' -B 1 -e 0.001'];  
    74. model = train(train_Y, sparse(train_X), option);  
    75. toc;  
    76.   
    77. %%% step5: test the model  
    78. fprintf(1,'step5: Testing... ');  
    79. tic;  
    80. [predict_label, accuracy, dec_values] = predict(test_Y, sparse(test_X), model);  
    81. toc;  
  • 相关阅读:
    thinkphp5 tp5 命名空间 报错 Namespace declaration statement has to be the very first statement in the script
    开启 php 错误 提示 php-fpm 重启 nginx 500错误 解决办法 wdlinux lnmp 一键包 php脚本无法解析执行
    js 设置 cookie 定时 弹出层 提示层 下次访问 不再显示 弹窗 getCookie setCookie setTimeout
    php 二维数组 转字符串 implode 方便 mysql in 查询
    nginx 重启 ps -ef|grep nginx kill -HUP 主进程号
    jquery bootstrap help-block input 表单 提示 帮助 信息
    jquery 倒计时 60秒 短信 验证码 js ajax 获取
    jQuery如何获取同一个类标签的所有的值 遍历
    linux下C语言文件操作相关函数
    gcc,gdb用法
  • 原文地址:https://www.cnblogs.com/yymn/p/4589212.html
Copyright © 2011-2022 走看看