Thundersvm source code viewing and learning

Thundersvm source code viewing and learning

Estimator and Mixin

In the library introduction section, see that two classes are inherited thundersvmfrom itsklearn.base

image-20230629150025202

One is BaseEstimatorthe base evaluator class and the other is Mixina helper class

In scikit-learn, an estimator is an object that implements a machine learning algorithm for fitting and predicting data . For example, linear regression models, decision tree classifiers, etc. are all estimators.

Mixin is a technique for mixing (mix-in) functions in a class , and only two kinds of Mixin are introduced here : RegressionMixinand ClassfierMixin, namely, regression tasks and classification tasks

The main functions provided by these two mixin classes are as follows:

  1. RegressorMixinProvides methods and attributes commonly used in regression tasksfit , such as (fitting the model), predict(making predictions), score(calculating the performance score of the model), etc.
  2. ClassifierMixinProvides methods and attributes commonly used in classification tasksfit , such as , predict, score, and other classification-specific methods, such as predict_proba(returns the probability that a sample belongs to each category) , ** decision_function(returns the decision function value of a sample)**, etc.

In the source code of the evaluator sklearn.base, you can see the following functions

image-20230629150102997

Basically, there are functions related to the acquisition and setting ofBaseEstimator some parameters , that is, BaseEstimator mainly realizes the functions related to the input and setting of parameters

image-20230629151232600

There are many types of Mixin , which mainly implement related functions such as model training and evaluation

question

Then in the initial introduction, it is said that the evaluator is used to fit and predict the data, but seeing the source code, it implements some functions related to parameter passing and setting

  • So how does fitting and prediction work in the estimator?
  • And Mixin mainly implements functions such as model training and evaluation, what is the relationship between it and Mixin auxiliary classes?
  • What is its specific combination and calling method?

understand

Obviously, the evaluator calls a function in the helper class .

The estimator class usually defines some general methods, such as functions for passing in and returning parameters, but does not directly define specific training and evaluation functions . Instead, the estimator class leverages inherited helper classes for specific training and evaluation functionality.

That is to say, sklearn.basethe idea of ​​function construction is based on the following points:

  • Separate the basic functions such as parameter input settings from the definition of the function in the training . Because there are many types of training tasks, the relevant function definitions are made for each training type, and then implemented in the specific training function call
  • Pass parameters into basic functions such as settings as the main part of the evaluator as baseEstimator
  • Inherit the variable and function definitions in BaseEstimator in the specific Estimator to implement specific training details
  • For different types of training tasks, considering that the required functions are not uniform, some tasks require specific functions , so define different types of Mixin base classes

In a specific task, such as svm, it is necessary to build an svm evaluator, which inherits BaseEstimator, and then calls the corresponding Mixin auxiliary class to train and evaluate the model according to the task type of svm training

And thundersvm is exactly this way of implementation

image-20230629153235999

After naming it, inherit ThundersvmBase in the svm model, which is BaseEstimator

image-20230629153414339

image-20230629153424272

Then pass in parameters and implement related functions

OS verification identification

image-20230629154153669

You can see that Thundersvm is supported for Windows , Linux and Mac

Darwin refers to Darwin, which is Mac OS , and has nothing to do with Windows

function implementation

image-20230630085928474

It only implements the detailed implementation of SvmModel, and adopts the method of inheriting SvmModel for other algorithms, and adds the inheritance of Mixin auxiliary class according to the algorithm and the corresponding task type

Digging a hole - study the specific implementation inside after the examination

Guess you like

Origin blog.csdn.net/ahahayaa/article/details/131469238