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

Call svm_check_parameter before training #72

Merged
merged 2 commits into from
Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 15 additions & 0 deletions src/LIBSVM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,17 @@ function set_num_threads(nt::Integer)
ccall((:svm_set_num_threads, libsvm), Cvoid, (Cint,), nt)
end

function check_parameter(problem::Vector{SVMProblem}, param::Vector{SVMParameter})
@assert length(problem) == 1
@assert length(param) == 1
err = ccall((:svm_check_parameter, libsvm), Cstring,
(Ptr{SVMProblem}, Ptr{SVMParameter}),
problem, param)
if err != C_NULL
throw(ArgumentError("Incorrect parameter: $(unsafe_string(err))"))
end
end

"""
svmtrain(
X::AbstractMatrix{U}, y::AbstractVector{T} = [];
Expand Down Expand Up @@ -359,6 +370,9 @@ function svmtrain(
problem = SVMProblem[SVMProblem(Int32(size(X, 2)), pointer(idx),
pointer(nodeptrs))]

# Validate the given parameters
check_parameter(problem, param)

if verbose
# set to stdout
ccall((:svm_set_print_string_function, libsvm), Cvoid,
Expand Down Expand Up @@ -413,6 +427,7 @@ function svmpredict(model::SVM{T}, X::AbstractMatrix{U}; nt::Integer = 0) where
(Ptr{Cvoid},), @cfunction(svmnoprint, Cvoid, (Ptr{UInt8},)))

cmod, data = svmmodel(model)

iblislin marked this conversation as resolved.
Show resolved Hide resolved
ma = [cmod]

for i = 1:ninstances
Expand Down
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,10 @@ end
end
end

@testset "Check parameters" begin
bad_params = Dict(:svmtype => OneClassSVM, :probability => true)
@test_throws ArgumentError svmtrain(rand(2, 5), ones(5); bad_params...)
end


end # @testset "LIBSVM"