cjlin1 / libsvm

LIBSVM -- A Library for Support Vector Machines
https://www.csie.ntu.edu.tw/~cjlin/libsvm/
BSD 3-Clause "New" or "Revised" License
4.55k stars 1.64k forks source link

Questions about the results of libSVM #209

Closed lzm42 closed 3 months ago

lzm42 commented 10 months ago

LibSVM version: C++OneClassSVM Specify parameters: nu and gamma Call method: Use C++to pass output, C # to call Dataset invocation method: Train unlabeled datasets and predict anomalies for that dataset Now I have encountered the following problem:

  1. Although nu and gamma have been specified, the results may change during repeated training. It was observed that this is due to changes in the number of iterations, but no parameters that can control the iterations were found
  2. When nu=0.05 and gamma=0.15, not only does the number of iterations change, but in most cases, the output of the iter is 0, indicating that there is no training at all
  3. At nu=0.05 and gamma=0.15, the number of iterations is 0 and 88, where all results are -1 when item=0 and all results are 1 when item=88
  4. Let nu=0.2, gamma=0.15, and the number of iterations be 91, but the results are all -1. It is worth noting that at this point, the auc metric of OCSVM in sklearn has exceeded that version
cjlin1 commented 10 months ago

Can you show us your data so we can see why results change in repeated training?

On 2023-11-28 09:52, lzm42 wrote:

LibSVM version: C++OneClassSVM Specify parameters: nu and gamma Call method: Use C++to pass output, C # to call Dataset invocation method: Train unlabeled datasets and predict anomalies for that dataset Now I have encountered the following problem:

  • Although nu and gamma have been specified, the results may change during repeated training. It was observed that this is due to changes in the number of iterations, but no parameters that can control the iterations were found
  • When nu=0.05 and gamma=0.15, not only does the number of iterations change, but in most cases, the output of the iter is 0, indicating that there is no training at all
  • At nu=0.05 and gamma=0.15, the number of iterations is 0 and 88, where all results are -1 when item=0 and all results are -1 when item=88
  • Let nu=0.2, gamma=0.15, and the number of iterations be 91, but the results are all -1. It is worth noting that at this point, the auc metric of OCSVM in sklearn has exceeded that version

-- Reply to this email directly, view it on GitHub [1], or unsubscribe [2]. You are receiving this because you are subscribed to this thread.Message ID: @.> [ { @.": "http://schema.org", @.": "EmailMessage", "potentialAction": { @.": "ViewAction", "target": "https://github.com/cjlin1/libsvm/issues/209", "url": "https://github.com/cjlin1/libsvm/issues/209", "name": "View Issue" }, "description": "View this Issue on GitHub", "publisher": { @.***": "Organization", "name": "GitHub", "url": "https://github.com" } } ]

Links:

[1] https://github.com/cjlin1/libsvm/issues/209 [2] https://github.com/notifications/unsubscribe-auth/ABI3BHWGQ5V5LGSQ6V4LDNTYGU7WHAVCNFSM6AAAAAA7452B4KVHI2DSMVQWIX3LMV43ASLTON2WKOZSGAYTGNBUGQYTKOA

lzm42 commented 10 months ago

from sklearn.datasets import laod_wine,load_breast_cancer. I deleted the target columns of two datasets and standardized them

cjlin1 commented 10 months ago

Could you show us your code?

lzm42 commented 10 months ago

about what?Cpp code?

#include <svm.h>
#include <iostream>

class OneClassSvm2
{
public:
        OneClassSvm2(string kernel, double nu, double gamma, double c);
        ~OneClassSvm2();
        int getKernelType(string type);
        void train(std::vector<svm_node*> data);
        void predict(std::vector<svm_node*> data);

private:
        int _kernel;
        double _nu;
        double _gamma;
        double _C;
        svm_model* model;
};

OneClassSvm2::OneClassSvm2(string kernel="rbf", double nu=0.5, double gamma=0.5, double c=1.0) {
    this->_kernel = this->getKernelType(kernel);
    this->_nu = nu;
    this->_gamma = gamma;
    this->_C = c;
    this->model = nullptr;
}

OneClassSvm2::~OneClassSvm2() {
    if (this->model != nullptr) {
        svm_free_and_destroy_model(&this->model);
    }
}

int OneClassSvm2::getKernelType(string type) {
    if (type == "rbf") {
        return RBF;
    }
    else if (type == "linear") {
        return LINEAR;
    }
    else if (type == "poly") {
        return POLY;
    }
    else if (type == "sigmoid") {
        return SIGMOID;
    }
}

void OneClassSvm2::train(std::vector<svm_node*> data) {
    try {
        int label_len = data.size();
        std::vector<double> labels;
        svm_parameter param;
        param.svm_type = ONE_CLASS;
        param.kernel_type = this->_kernel;
        param.nu = this->_nu;
        param.gamma = this->_gamma;
        param.C = this->_C;
        param.eps = 1e-6;

        svm_problem prob;

        for (int i = 0; i < label_len; i++) {
            labels.push_back(1);
        }

        prob.l = label_len;
        prob.y = labels.data();
        prob.x = data.data();

        this->model = svm_train(&prob, &param);
    }
    catch (const std::exception &e) {
        cout << "训练异常:" << e.what() << endl;
    }
}

void OneClassSvm2::predict(std::vector<svm_node*> data) {
    vector<double> result;
    try {
        for (auto value : data) {
            double predicted_label = svm_predict(model, value);
            result.push_back(predicted_label);
            std::cout << " " << predicted_label;
        }
        cout << "结果大小:" << result.size() << endl;
    }
    catch (const std::exception& e) {
        cout << "预测异常:" << e.what() << endl;
    }
}

When we examine the code, we find that there are no suitable parameters to control the iteration

cjlin1 commented 10 months ago

Can you show us some exp log. Indeed because we don't have your whole code/environment to reproduce what you got, it is difficult to see what is going on

On 2023-12-04 08:51, lzm42 wrote:

about what?Cpp code?

include

include

class OneClassSvm2 { public: OneClassSvm2(string kernel, double nu, double gamma, double c); ~OneClassSvm2(); int getKernelType(string type); void train(std::vector<svm_node> data); void predict(std::vector<svm_node> data);

private: int _kernel; double _nu; double _gamma; double _C; svm_model* model; };

OneClassSvm2::OneClassSvm2(string kernel="rbf", double nu=0.5, double gamma=0.5, double c=1.0) { this->_kernel = this->getKernelType(kernel); this->_nu = nu; this->_gamma = gamma; this->_C = c; this->model = nullptr; }

OneClassSvm2::~OneClassSvm2() { if (this->model != nullptr) { svm_free_and_destroy_model(&this->model); } }

int OneClassSvm2::getKernelType(string type) { if (type == "rbf") { return RBF; } else if (type == "linear") { return LINEAR; } else if (type == "poly") { return POLY; } else if (type == "sigmoid") { return SIGMOID; } }

void OneClassSvm2::train(std::vector<svm_node*> data) { try { int label_len = data.size(); std::vector labels; svm_parameter param; param.svm_type = ONE_CLASS; param.kernel_type = this->_kernel; param.nu = this->_nu; param.gamma = this->_gamma; param.C = this->_C; param.eps = 1e-6;

    svm_problem prob;

    for (int i = 0; i < label_len; i++) {
        labels.push_back(1);
    }

    prob.l = label_len;
    prob.y = labels.data();
    prob.x = data.data();

    this->model = svm_train(&prob, &param);
}
catch (const std::exception &e) {
    cout << "训练异常:" << e.what() << endl;
}

}

void OneClassSvm2::predict(std::vector<svm_node*> data) { vector result; try { for (auto value : data) { double predicted_label = svm_predict(model, value); result.push_back(predicted_label); std::cout << " " << predicted_label; } cout << "结果大小:" << result.size() << endl; } catch (const std::exception& e) { cout << "预测异常:" << e.what() << endl; } }

-- Reply to this email directly, view it on GitHub [1], or unsubscribe [2]. You are receiving this because you commented.Message ID: @.> [ { @.": "http://schema.org", @.": "EmailMessage", "potentialAction": { @.": "ViewAction", "target": "https://github.com/cjlin1/libsvm/issues/209#issuecomment-1837673116", "url": "https://github.com/cjlin1/libsvm/issues/209#issuecomment-1837673116", "name": "View Issue" }, "description": "View this Issue on GitHub", "publisher": { @.***": "Organization", "name": "GitHub", "url": "https://github.com" } } ]

Links:

[1] https://github.com/cjlin1/libsvm/issues/209#issuecomment-1837673116 [2] https://github.com/notifications/unsubscribe-auth/ABI3BHUXQ3XGNRV5N4ES4I3YHUNCHAVCNFSM6AAAAAA7452B4KVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMZXGY3TGMJRGY

lzm42 commented 9 months ago

We only need data and parameters to reproduce it. We did not make any changes to the code, it was just a simple call, so there are no relevant logs