Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

FIX : fix SVC pickle with callable kernel #789

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 22, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 2 sklearn/grid_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def fit_grid_point(X, y, base_clf, clf_params, train, test, loss_func,
ind = np.arange(X.shape[0])
train = ind[train]
test = ind[test]
if hasattr(base_clf, 'kernel_function'):
if hasattr(base_clf, 'kernel') and hasattr(base_clf.kernel, '__call__'):
# cannot compute the kernel values with custom function
raise ValueError(
"Cannot use a custom kernel function. "
Expand Down
63 changes: 46 additions & 17 deletions 63 sklearn/svm/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,13 @@ def __init__(self, impl, kernel, degree, gamma, coef0,
if not impl in LIBSVM_IMPL:
raise ValueError("impl should be one of %s, %s was given" % (
LIBSVM_IMPL, impl))
if hasattr(kernel, '__call__'):
self.kernel_function = kernel
self.kernel = 'precomputed'
else:
self.kernel = kernel
if not scale_C:
warnings.warn('SVM: scale_C will disappear and be assumed to be '
'True in scikit-learn 0.12', FutureWarning,
stacklevel=2)

self.impl = impl
self.kernel = kernel
self.degree = degree
self.gamma = gamma
self.coef0 = coef0
Expand Down Expand Up @@ -156,7 +152,7 @@ def _dense_fit(self, X, y, sample_weight=None):
sample_weight = np.asarray([] if sample_weight is None
else sample_weight, dtype=np.float64)

if hasattr(self, 'kernel_function'):
if hasattr(self.kernel, '__call__'):
# you must store a reference to X to compute the kernel in predict
# TODO: add keyword copy to copy on demand
self.__Xfit = X
Expand All @@ -172,7 +168,8 @@ def _dense_fit(self, X, y, sample_weight=None):
"X has %s samples, but y has %s." %
(X.shape[0], y.shape[0]))

if self.kernel == "precomputed" and X.shape[0] != X.shape[1]:
if (hasattr(self.kernel, '__call__') or self.kernel == "precomputed") \
and X.shape[0] != X.shape[1]:
raise ValueError("X.shape[0] should be equal to X.shape[1]")

if (self.kernel in ['poly', 'rbf']) and (self.gamma == 0):
Expand All @@ -195,6 +192,10 @@ def _dense_fit(self, X, y, sample_weight=None):

libsvm.set_verbosity_wrap(self.verbose)

kernel = self.kernel
if hasattr(kernel, '__call__'):
kernel = 'precomputed'

# we don't pass **self.get_params() to allow subclasses to
# add other parameters to __init__
self.support_, self.support_vectors_, self.n_support_, \
Expand All @@ -203,7 +204,7 @@ def _dense_fit(self, X, y, sample_weight=None):
svm_type=solver_type, sample_weight=sample_weight,
class_weight=self.class_weight_,
class_weight_label=self.class_weight_label_,
kernel=self.kernel, C=C, nu=self.nu,
kernel=kernel, C=C, nu=self.nu,
probability=self.probability, degree=self.degree,
shrinking=self.shrinking, tol=self.tol, cache_size=self.cache_size,
coef0=self.coef0, gamma=self.gamma, epsilon=epsilon)
Expand Down Expand Up @@ -269,8 +270,12 @@ def _sparse_fit(self, X, y, sample_weight=None):
"boolean masks (use `indices=True` in CV)."
% (sample_weight.shape, X.shape))

kernel = self.kernel
if hasattr(kernel, '__call__'):
kernel = 'precomputed'

solver_type = LIBSVM_IMPL.index(self.impl)
kernel_type = self._sparse_kernels.index(self.kernel)
kernel_type = self._sparse_kernels.index(kernel)

self.class_weight_, self.class_weight_label_ = \
_get_class_weight(self.class_weight, y)
Expand Down Expand Up @@ -343,7 +348,7 @@ def _dense_predict(self, X):
n_samples, n_features = X.shape
X = self._compute_kernel(X)

if self.kernel == "precomputed":
if hasattr(self.kernel, '__call__') or self.kernel == "precomputed":
if X.shape[1] != self.shape_fit_[0]:
raise ValueError("X.shape[1] = %d should be equal to %d, "
"the number of samples at training time" %
Expand All @@ -360,19 +365,29 @@ def _dense_predict(self, X):
C = 0.0 # C is not useful here

svm_type = LIBSVM_IMPL.index(self.impl)

kernel = self.kernel
if hasattr(kernel, '__call__'):
kernel = 'precomputed'

return libsvm.predict(
X, self.support_, self.support_vectors_, self.n_support_,
self.dual_coef_, self._intercept_,
self.label_, self.probA_, self.probB_,
svm_type=svm_type,
kernel=self.kernel, C=C, nu=self.nu,
kernel=kernel, C=C, nu=self.nu,
probability=self.probability, degree=self.degree,
shrinking=self.shrinking, tol=self.tol, cache_size=self.cache_size,
coef0=self.coef0, gamma=self.gamma, epsilon=epsilon)

def _sparse_predict(self, X):
X = sp.csr_matrix(X, dtype=np.float64)
kernel_type = self._sparse_kernels.index(self.kernel)

kernel = self.kernel
if hasattr(kernel, '__call__'):
kernel = 'precomputed'

kernel_type = self._sparse_kernels.index(kernel)

C = 0.0 # C is not useful here

Expand Down Expand Up @@ -435,12 +450,16 @@ def _dense_predict_proba(self, X):

C = 0.0 # C is not useful here

kernel = self.kernel
if hasattr(kernel, '__call__'):
kernel = 'precomputed'

svm_type = LIBSVM_IMPL.index(self.impl)
pprob = libsvm.predict_proba(
X, self.support_, self.support_vectors_, self.n_support_,
self.dual_coef_, self._intercept_, self.label_,
self.probA_, self.probB_,
svm_type=svm_type, kernel=self.kernel, C=C, nu=self.nu,
svm_type=svm_type, kernel=kernel, C=C, nu=self.nu,
probability=self.probability, degree=self.degree,
shrinking=self.shrinking, tol=self.tol, cache_size=self.cache_size,
coef0=self.coef0, gamma=self.gamma, epsilon=epsilon)
Expand All @@ -449,16 +468,21 @@ def _dense_predict_proba(self, X):

def _compute_kernel(self, X):
"""Return the data transformed by a callable kernel"""
if hasattr(self, 'kernel_function'):
if hasattr(self.kernel, '__call__'):
# in the case of precomputed kernel given as a function, we
# have to compute explicitly the kernel matrix
X = np.asarray(self.kernel_function(X, self.__Xfit),
X = np.asarray(self.kernel(X, self.__Xfit),
dtype=np.float64, order='C')
return X

def _sparse_predict_proba(self, X):
X.data = np.asarray(X.data, dtype=np.float64, order='C')
kernel_type = self._sparse_kernels.index(self.kernel)

kernel = self.kernel
if hasattr(kernel, '__call__'):
kernel = 'precomputed'

kernel_type = self._sparse_kernels.index(kernel)

return libsvm_sparse.libsvm_sparse_predict_proba(
X.data, X.indices, X.indptr,
Expand Down Expand Up @@ -522,12 +546,17 @@ def decision_function(self, X):
epsilon = self.epsilon
if epsilon == None:
epsilon = 0.1

kernel = self.kernel
if hasattr(kernel, '__call__'):
kernel = 'precomputed'

dec_func = libsvm.decision_function(
X, self.support_, self.support_vectors_, self.n_support_,
self.dual_coef_, self._intercept_, self.label_,
self.probA_, self.probB_,
svm_type=LIBSVM_IMPL.index(self.impl),
kernel=self.kernel, C=C, nu=self.nu,
kernel=kernel, C=C, nu=self.nu,
probability=self.probability, degree=self.degree,
shrinking=self.shrinking, tol=self.tol, cache_size=self.cache_size,
coef0=self.coef0, gamma=self.gamma, epsilon=epsilon)
Expand Down
12 changes: 11 additions & 1 deletion 12 sklearn/svm/tests/test_sparse.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
from scipy import linalg
from scipy import sparse
from sklearn import datasets, svm, linear_model
from sklearn import datasets, svm, linear_model, base
from numpy.testing import assert_array_almost_equal, \
assert_array_equal, assert_equal

Expand Down Expand Up @@ -229,6 +229,16 @@ def test_sparse_scale_C():
assert_true(error_with_scale > 1e-3)


def test_sparse_svc_clone_with_callable_kernel():
a = svm.SVC(C=1, kernel=lambda x, y: x * y.T, probability=True)
b = base.clone(a)

b.fit(X_sp, Y)
b.predict(X_sp)
b.predict_proba(X_sp)
# b.decision_function(X_sp) # XXX : should be supported


if __name__ == '__main__':
import nose
nose.runmodule()
12 changes: 11 additions & 1 deletion 12 sklearn/svm/tests/test_svm.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
assert_almost_equal
from nose.tools import assert_raises, assert_true

from sklearn import svm, linear_model, datasets, metrics
from sklearn import svm, linear_model, datasets, metrics, base
from sklearn.datasets.samples_generator import make_classification
from sklearn.utils import check_random_state

Expand Down Expand Up @@ -661,6 +661,16 @@ def test_linearsvc_verbose():
os.dup2(stdout, 1) # restore original stdout


def test_svc_clone_with_callable_kernel():
a = svm.SVC(kernel=lambda x, y: np.dot(x, y.T), probability=True)
b = base.clone(a)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK, cloning is currently not implemented with pickle. Would be better rename this test to avoid any confusion. A test that does real pickling would be nice too but I am afraid that lambdas are not picklable hence the callable kernel would have to be defined in some python class wrapper instead.


b.fit(X, Y)
b.predict(X)
b.predict_proba(X)
b.decision_function(X)


if __name__ == '__main__':
import nose
nose.runmodule()
Morty Proxy This is a proxified and sanitized view of the page, visit original site.