shogun-toolbox / shogun

Shōgun
http://shogun-toolbox.org
BSD 3-Clause "New" or "Revised" License
3.03k stars 1.04k forks source link

The sum of the weights of the core is not 1? #4410

Open zym-wade opened 5 years ago

zym-wade commented 5 years ago

I use two-category MKL training, why always there is a segmentation error, and the sum of the weights of the core is not 1?

karlnapf commented 5 years ago

Thanks for the report Could you post a stand-alone example to reproduce this, minimally if possible. Thanks

zym-wade commented 5 years ago

Thanks for the report Could you post a stand-alone example to reproduce this, minimally if possible. Thanks

print('Using mkl vector machine')
import shogun as sg

try:
    from shogun import SVMLight
except ImportError:
    print("SVMLight not available")
    exit(0)

# create combined train features
feats_train = sg.CombinedFeatures()
feats_train.append_feature_obj( sg.RealFeatures(sg.LongIntFeatures(x_tr.T )))
feats_test = sg.CombinedFeatures()
feats_test.append_feature_obj( sg.RealFeatures(sg.LongIntFeatures(x_te.T )) )

# and corresponding combined kernel
kernel = sg.CombinedKernel()
kernel.append_kernel(sg.GaussianKernel(3.0))
kernel.append_kernel(sg.PolyKernel(10, 1))
ret = kernel.init(feats_train, feats_train)
print(ret)
for i in range(np.size(y_tr)):
    if y_tr[i] == 0:
        y_tr[i] = -1
for i in range(np.size(y_te)):
    if y_te[i] == 0:
        y_te[i] = -1
#train mkl

labels = sg.BinaryLabels(y_tr)
label_te = sg.BinaryLabels(y_te)

mkl = sg.MKLClassification(sg.LibSVM());
mkl.set_interleaved_optimization_enabled(False);

# which norm to use for MKL
mkl.set_mkl_norm(2)  # 2,3

# set cost (neg, pos)
mkl.set_C(1, 1)

# set kernel and labels
mkl.set_kernel(kernel)
mkl.set_labels(labels)

# train
mkl.train()
w = kernel.get_subkernel_weights()
print(w)
kernel.set_subkernel_weights(w)

kernel.init(feats_train, feats_test)
mkl.set_kernel(kernel)
out = mkl.apply_binary()
evaluator = sg.MulticlassAccuracy()
accuracy = evaluator.evaluate(out, label_te)
print("Accuracy = %2.2f%%" % (100 * accuracy))

------------------------------------------------------

[0.60014169 0.79989371]
experiments.py:267: RuntimeWarning: [WARN] In file /home/zym/shogun/src/shogun/labels/MulticlassLabels.cpp line 225: Converting non-contiguous multiclass labels to contiguous version:

  accuracy = evaluator.evaluate(out, label_te)
experiments.py:267: RuntimeWarning: [WARN] In file /home/zym/shogun/src/shogun/labels/MulticlassLabels.cpp line 230: Converting -1 to 0.

  accuracy = evaluator.evaluate(out, label_te)
experiments.py:267: RuntimeWarning: [WARN] In file /home/zym/shogun/src/shogun/labels/MulticlassLabels.cpp line 230: Converting 1 to 1.

  accuracy = evaluator.evaluate(out, label_te)
Accuracy = 98.50%
*** Error in `python3': corrupted size vs. prev_size: 0x00000000021e08bf ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f6d19e0b7e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x7e913)[0x7f6d19e12913]
/lib/x86_64-linux-gnu/libc.so.6(+0x80678)[0x7f6d19e14678]

I would like to ask you to help me see if there are other problems.Thanks!

karlnapf commented 5 years ago

It seems like a recently introduced bug. I will try to fix it

zym-wade commented 5 years ago

It seems like a recently introduced bug. I will try to fix it

And this segment error is only after the end of the program run, is there no free space?

karlnapf commented 5 years ago

No this has to do with a recent re-factoring and comes from a missed reference counter increase ...

zym-wade commented 5 years ago

No this has to do with a recent re-factoring and comes from a missed reference counter increase ...

I also want to ask, it takes a long time to save and download the svm model. Is there any way to improve it?

karlnapf commented 5 years ago

No this has to do with a recent re-factoring and comes from a missed reference counter increase ...

I also want to ask, it takes a long time to save and download the svm model. Is there any way to improve it?

That is more for a discussion on the mailing list or IRC, rather than here. But short answer: this is probably because the whole training set is stored (downside of kernel methods). You could try calling store_model_features which prunes all data that have zero coefficients. Not sure that works for MKL, feel free to issue a feature request

karlnapf commented 5 years ago

Your code snippet is incomplete. Could you provide a minimal stand-alone example that I can just copy paste to run? I need to fill so many gaps here that it is not clear what the error would be

zym-wade commented 5 years ago

Your code snippet is incomplete. Could you provide a minimal stand-alone example that I can just copy paste to run? I need to fill so many gaps here that it is not clear what the error would be

ok,I will sort it out on Monday, thank you.

zym-wade commented 5 years ago

Your code snippet is incomplete. Could you provide a minimal stand-alone example that I can just copy paste to run? I need to fill so many gaps here that it is not clear what the error would be What is your email address, I also need to send the data set to you together.

karlnapf commented 5 years ago

Does it also crash with minimal random data? Like x_tr = np.random.randn(100,10) etc? email is on our website

karlnapf commented 5 years ago

Ok I figured there is a memory bug in CombinedKernel that is at least contributing to your problem.

Reproducible with

import shogun as sg

import numpy as np
x_tr = np.random.randn(10, 2)

temp = sg.RealFeatures(x_tr)
feats_train = sg.CombinedFeatures()
feats_train.append_feature_obj( temp)

kernel = sg.CombinedKernel()
kernel.append_kernel(sg.GaussianKernel(3.0))
kernel.append_kernel(sg.PolyKernel(10, 1))

kernel.init(feats_train, feats_train)

sg.get_global_io().set_loglevel(0)
del kernel
print temp # segfaults as object's refcounter decreases to zero when destroying the kernel