Skip to content
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

Increase training and inference performance for GAK kernel #77

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
31 changes: 28 additions & 3 deletions tslearn/svm.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,29 @@ def _kernel_func_gak(sz, d, gamma):
gamma = 1.
return lambda x, y: cdist_gak(x.reshape((-1, sz, d)), y.reshape((-1, sz, d)), sigma=numpy.sqrt(gamma / 2.))

def _sparse_kernel_func_gak(sz, d, gamma, slice_support_vectors=None):
if gamma == "auto":
gamma = 1.

def sparse_gak(X, X_fit):

if X_fit is X:
return cdist_gak(X.reshape((-1, sz, d)), None, sigma=numpy.sqrt(gamma / 2.))

if slice_support_vectors is not None:
# slice out support vectors
sliced_X_fit = X_fit[slice_support_vectors]
gak_sim_dense = cdist_gak(X.reshape((-1, sz, d)), sliced_X_fit.reshape((-1, sz, d)), sigma=numpy.sqrt(gamma / 2.))

# act like nothing has happend ...
lejafar marked this conversation as resolved.
Show resolved Hide resolved
gak_sim = numpy.empty((len(X), len(X_fit)))
gak_sim[:, slice_support_vectors] = gak_sim_dense
rtavenar marked this conversation as resolved.
Show resolved Hide resolved

return gak_sim

return cdist_gak(X.reshape((-1, sz, d)), X_fit.reshape((-1, sz, d)), sigma=numpy.sqrt(gamma / 2.))

return sparse_gak

class TimeSeriesSVC(BaseSVC):
"""Time-series specific Support Vector Classifier.
Expand Down Expand Up @@ -148,7 +171,7 @@ def __init__(self, sz, d, C=1.0, kernel="gak", degree=3, gamma="auto", coef0=0.0
self.sz = sz
self.d = d
if kernel == "gak":
kernel = _kernel_func_gak(sz=sz, d=d, gamma=gamma)
kernel = _sparse_kernel_func_gak(sz=sz, d=d, gamma=gamma)
super(TimeSeriesSVC, self).__init__(C=C, kernel=kernel, degree=degree, gamma=gamma, coef0=coef0,
shrinking=shrinking, probability=probability, tol=tol,
cache_size=cache_size, class_weight=class_weight, verbose=verbose,
Expand All @@ -169,8 +192,10 @@ def fit(self, X, y, sample_weight=None):
sklearn_X = _prepare_ts_datasets_sklearn(X)
if self.kernel == "gak" and self.gamma == "auto":
self.gamma = gamma_soft_dtw(to_time_series_dataset(X))
self.kernel = _kernel_func_gak(sz=self.sz, d=self.d, gamma=self.gamma)
return super(TimeSeriesSVC, self).fit(sklearn_X, y, sample_weight=sample_weight)
self.kernel = _sparse_kernel_func_gak(sz=self.sz, d=self.d, gamma=self.gamma)
super(TimeSeriesSVC, self).fit(sklearn_X, y, sample_weight=sample_weight)
self.kernel = _sparse_kernel_func_gak(sz=self.sz, d=self.d, gamma=self.gamma, slice_support_vectors=self.support_)
return self

def predict(self, X):
sklearn_X = _prepare_ts_datasets_sklearn(X)
Expand Down