Search…

Cài Đặt LIBSVM trên Linux và macOS

Nguyễn NghĩaNguyễn Nghĩa
07/11/20204 min read
Hướng dẫn cài đặt LIBSVM trên Linux và macOS hỗ trợ thuật toán SVM - Support Vector Machine.

Thuật toán SVM là gì?

Thuật toán SVM (Support Vector Machine) là 1 thuật toán máy học dùng cho bài toán phân lớp dữ liệu. Ý tưởng của thuật toán là tìm 1 siêu phẳng tối ưu (Optimal Hyperplane) sao cho margin tới các điểm support vector có khoảng cách lớn nhất. Hiện nay thuật toán này được sử dụng khá nhiều trong lĩnh vực Machine Learning cùng với các thuật toán như cây quyết định (Tree Decision), K-NN (K-Nearest Neighbor), mạng nơ-ron (Nerual Network).

LIBSVM là gì?

LIBSVM là 1 thư viện cài đặt thuật toán SVM (Support Vector Machine), thư viện được viết bằng C nhưng sau này đã port qua hầu hết các ngôn ngữ như Java, Python, Ruby, Android, .NET, ... bởi Chih-Chung Chang và Chih-Jen Lin. LIBSVM được sử dụng trong rất nhiều phần mềm như MATLAB, R, ...

Trang chủ của thư viện LIBSVM: https://www.csie.ntu.edu.tw/~cjlin/libsvm/

Có thể download source code của thư viện này trên github, hoặc dùng lệnh sau để clone source LIBSVM về máy:

git clone https://github.com/cjlin1/libsvm.git

Cài đặt thư viện LIBSVM

Sau khi download hoặc clone LIBSVM về thì đã có sẵn tất cả các mã nguồn của thư viện này, nếu cần thiết có thể mở source mà đọc xem cách người ta implement thư viện nổi tiếng này như thế nào?

Các file của thư viện LIBSVM

Build LIBSVM

Trong thư mục này có 1 file có tên là Makefile, file này chính là file cấu hình để build source code của thư viện này ra file thực thi (excutable).

Tiếp theo sẽ mở Termial tại thư mục này và gõ lệnh make để build LIBSVM.

NguyenNghiasMBP:LIBSVM nguyennghia$ make

Sau khi chạy lệnh này thì gcc build thành các file thực thi và có thể sử dụng nó cho việc train data và predict.

Tiếp theo có thể test thư viện này xem việc build có thành công hay không?

Test LIBSVM

Gõ những dòng lệnh dưới đây.

./svm-train

NguyenNghiasMBP:LIBSVM nguyennghia$ ./svm-train 
Usage: svm-train [options] training_set_file [model_file]
options:
-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)

./svm-predict

NguyenNghiasMBP:LIBSVM nguyennghia$ ./svm-predict
Usage: svm-predict [options] test_file model_file output_file
options:
-b probability_estimates: whether to predict probability estimates, 0 or 1 (default 0); for one-class SVM only 0 is supported
-q : quiet mode (no outputs)

./svm-scale

NguyenNghiasMBP:LIBSVM nguyennghia$ ./svm-scale 
Usage: svm-scale [options] data_filename
options:
-l lower : x scaling lower limit (default -1)
-u upper : x scaling upper limit (default +1)
-y y_lower y_upper : y scaling limits (default: no y scaling)
-s save_filename : save scaling parameters to save_filename
-r restore_filename : restore scaling parameters from restore_filename

Khi gõ những dòng lệnh sẽ log ra cách sử dụng của từng file thực thi đó.

  • svm-train: sử dụng cho mục đích train data.
  • svm-predict: sử dụng để cho việc dự đoán kết quả dựa vào kết quả ở svm-train.
  • svm-scale: sử dụng để biến đổi dữ liệu trước khi đưa data vào train.

Có thể sử dụng thư viện này cho bất kỳ mục đích phân lớp, ví dụ như nhận diện hình ảnh, ký tự, ...

IO Stream

IO Stream Co., Ltd

developer@iostream.co
383/1 Quang Trung, ward 10, Go Vap district, Ho Chi Minh city
Business license number: 0311563559 issued by the Department of Planning and Investment of Ho Chi Minh City on February 23, 2012

©IO Stream, 2013 - 2025