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

Using Kernels.Precomputed, svmpredict doesn't verify the dimensions of the input #85

Closed
till-m opened this issue Nov 1, 2021 · 2 comments · Fixed by #87
Closed

Using Kernels.Precomputed, svmpredict doesn't verify the dimensions of the input #85

till-m opened this issue Nov 1, 2021 · 2 comments · Fixed by #87

Comments

@till-m
Copy link
Member

till-m commented Nov 1, 2021

Current behavior

It is possible to provide malformed gram matrices to svmpredict when using precomputed kernels:

using LIBSVM

# Training data
X = [-2 -1 -1 1 1 2;
     -1 -1 -2 1 2 1]
y = [1, 1, 1, 2, 2, 2]

# Testing data
T = [-1 2 3;
     -1 2 2]

# Precomputed matrix for training (corresponds to linear kernel)
K = X' * X

model = svmtrain(K, y, kernel=Kernel.Precomputed)

# Precomputed matrix for prediction
KK = X' * T

# truncate KK
KK_malformed = KK[1:1,:]

ỹ, decision_values = svmpredict(model, KK_malformed)

Output:

julia> ỹ
3-element Vector{Int64}:
 2
 2
 2

julia> decision_values
2×3 Matrix{Float64}:
 NaN    NaN    NaN
   0.0    0.0    0.0

Expected behavior

As described in the README, the gram matrix should have dimensions (l, n) when predicting n items on l training vectors and produce an error otherwise. Alternatively, it could also accept gram matrices of shape (k, n) where k is the number of support vectors of the model.

@till-m
Copy link
Member Author

till-m commented Nov 2, 2021

I've implemented the behavior as described above and added simple test cases. If you consider this a good change, let me know and I can make a pull request.

@iblislin
Copy link
Member

iblislin commented Nov 5, 2021

Ah, PRs are welcomed :)

iblislin pushed a commit that referenced this issue Nov 11, 2021
* Implement dimension check for prediction gram matrix

* Add tests for gram matrix dimension check

* Disallow providing only support vector related entries

* Adjust error message

* Minor style changes

Resolves #85
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants