# Calculate SVM dual loss effeciently

**URL:** <https://discourse.julialang.org/t/calculate-svm-dual-loss-effeciently/27171>\
**Category:** Performance\
**Created:** [August 5, 2019, 10:12am UTC](https://discourse.julialang.org/t/calculate-svm-dual-loss-effeciently/27171 "2019-08-05T10:12:30Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Charlie\_He](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/charlie_he/32/9465_2.png) [@Charlie\_He](https://discourse.julialang.org/u/Charlie_He)\
**Post date:** [August 5, 2019, 10:12am UTC](https://discourse.julialang.org/t/calculate-svm-dual-loss-effeciently/27171/1 "2019-08-05T10:12:30Z")

</div>

I am trying to implement a SVM dual optimization problem in Julia. My data set is the 60000x784 MNIST number recognition dataset. Here are my functions for  
The linear version is very fast:

```julia
function dual_loss(α::Vec, X::Mat, y::Vec) where {T<:AbstractFloat, Vec<:AbstractVector{T}, Mat<:AbstractMatrix{T}}
    t = α .* y
    return sum(α) - t' * X * X' * t
end

```

The kernel version however is extremely slow and I am not patient enough to wait for it to finish:

```julia
function dual_loss(α::Vec, X::Mat, y::Vec, kernel::Function=dot) where {T<:AbstractFloat, Vec<:AbstractVector{T}, Mat<:AbstractMatrix{T}}
    t = α .* y
    return sum(α) - sum(@views 0.5 * t[i] * t[j] * kernel(X[i, :], X[j, :]) for i=1:size(X, 1), j=1:size(X, 1))
end

```

I also tried to use the [MLKernel.jl](https://github.com/trthatcher/MLKernels.jl) library and it is still very slow. I wonder is this due to the scale of the problem itself (I’m on my Laptop PC and running on CPU), or due to my code being not optimized enough? If so, Is there any way to improve its performance?
