Xtra-Computing / thundersvm

ThunderSVM: A Fast SVM Library on GPUs and CPUs
Apache License 2.0
1.56k stars 216 forks source link

Avoid freeing pointer after copy [Python] #146

Open ghost opened 5 years ago

ghost commented 5 years ago

Hello everyone,

I am actually using thundersvm with Python through Julia. During training, I would like to save the best C-SVC model encountered.

Let's say that I find such a best model during training, I would then like to copy this python object along with the pointer on the C++ SVM object. I am actually doing this for now :

py_model = thundersvm.SVC(params...)
.... training ....
best = thundersvm.SVC(py_model.get_params()...)
best.model = py_model.model # Keeping the c++ pointer from py_model

But If I fit again the py_model object I would free the pointer on the c++ object since : https://github.com/Xtra-Computing/thundersvm/blob/6e28da802e483fc741056a1768c825737c840cca/python/thundersvm/thundersvm.py#L96-L99

And consequently the pointer I have in best.model.

Is there a way to avoid freeing this pointer if the original py_model has been copied or safely copy this c++ pointer into a fitted clone of py_model ?

Thank you.

ghost commented 5 years ago

One way was to give None to py_model.model but I wont be able to use predict on it again.

I could wrap copy options and handling cases in Julia or Python but maybe there is clean to do this with c++ or python copy procedures ?

jiahuanluo commented 5 years ago

Hi, @gdestouet,

There are two methods to save the best model during training.

  1. If the model is fully trained on your training data, you can use save_to_file and load_from_file to save and load the model.

  2. You can save the best configuration and train the model again.