Hmm, not much of an improvement.
julia> using CSV, DataFrames, BenchmarkTools
julia> df = CSV.read("/home/celrod/Documents/miscwork/winequality-red.csv",
delim=";", types=Dict(6=>Float64, 7=>Float64));
julia> X = convert(Array, df[[Symbol("fixed acidity"), Symbol("volatile acidity")]]);
julia> Y = df[:quality];
julia> w = ones(size(X)[2]);
julia> function logistic_regression(Y, X, w, iterations)
for i in 1:iterations
w -= (((1.0 ./ (1.0 .+ e .^ (-Y .* (X * w))) - 1.0) .* Y)' * X)'
end
w
end
logistic_regression (generic function with 1 method)
julia> @benchmark logistic_regression($Y, $X, $w, 1000)
BenchmarkTools.Trial:
memory estimate: 61.83 MiB
allocs estimate: 7000
--------------
minimum time: 55.850 ms (6.08% GC)
median time: 57.943 ms (8.85% GC)
mean time: 57.850 ms (8.42% GC)
maximum time: 59.761 ms (9.55% GC)
--------------
samples: 87
evals/sample: 1
julia> function logistic_regression!(w, Y, X, iterations, step = -1.0)
Xw = similar(w, size(X,1))
buffer = similar(w, length(Y));
for i in 1:iterations
A_mul_B!(Xw, X, w)
@. buffer = ((1.0 / (1.0 + exp(-Y * Xw)) - 1.0) * Y)
Base.LinAlg.BLAS.gemv!('T', 1.0, X, buffer, step, w)
end
w
end
logistic_regression! (generic function with 2 methods)
julia> @benchmark logistic_regression!($w, $Y, $X, 1000)
BenchmarkTools.Trial:
memory estimate: 25.25 KiB
allocs estimate: 2
--------------
minimum time: 29.327 ms (0.00% GC)
median time: 30.318 ms (0.00% GC)
mean time: 31.095 ms (0.00% GC)
maximum time: 47.457 ms (0.00% GC)
--------------
samples: 161
evals/sample: 1
julia> function fmlogistic_regression!(w, Y, X, iterations, step = -1.0)
Xw = similar(w, size(X,1))
buffer = similar(w, length(Y));
for i in 1:iterations
A_mul_B!(Xw, X, w)
@fastmath @. buffer = ((1.0 / (1.0 + exp(-Y * Xw)) - 1.0) * Y)
Base.LinAlg.BLAS.gemv!('T', 1.0, X, buffer, step, w)
end
w
end
fmlogistic_regression! (generic function with 2 methods)
julia> @benchmark fmlogistic_regression!($w, $Y, $X, 1000)
BenchmarkTools.Trial:
memory estimate: 25.25 KiB
allocs estimate: 2
--------------
minimum time: 29.347 ms (0.00% GC)
median time: 31.733 ms (0.00% GC)
mean time: 32.509 ms (0.00% GC)
maximum time: 49.867 ms (0.00% GC)
--------------
samples: 154
evals/sample: 1
julia> function fmlogistic_regression2!(w, Y, X, iterations, step = -1.0)
Xw = similar(w, size(X,1))
buffer = similar(w, length(Y));
for i in 1:iterations
A_mul_B!(Xw, X, w)
@. buffer = ((1.0 / (1.0 + Base.FastMath.exp_fast(-Y * Xw)) - 1.0) * Y)
Base.LinAlg.BLAS.gemv!('T', 1.0, X, buffer, step, w)
end
w
end
fmlogistic_regression2! (generic function with 2 methods)
julia> @benchmark fmlogistic_regression2!($w, $Y, $X, 1000)
BenchmarkTools.Trial:
memory estimate: 25.25 KiB
allocs estimate: 2
--------------
minimum time: 29.401 ms (0.00% GC)
median time: 30.258 ms (0.00% GC)
mean time: 30.968 ms (0.00% GC)
maximum time: 47.596 ms (0.00% GC)
--------------
samples: 162
evals/sample: 1
julia> using SLEEF
julia> function slogistic_regression!(w, Y, X, iterations, step = -1.0)
Xw = similar(w, size(X,1))
buffer = similar(w, length(Y));
for i in 1:iterations
A_mul_B!(Xw, X, w)
@. buffer = ((1.0 / (1.0 + SLEEF.exp(-Y * Xw)) - 1.0) * Y)
Base.LinAlg.BLAS.gemv!('T', 1.0, X, buffer, step, w)
end
w
end
slogistic_regression! (generic function with 2 methods)
julia> @benchmark slogistic_regression!($w, $Y, $X, 1000)
BenchmarkTools.Trial:
memory estimate: 25.25 KiB
allocs estimate: 2
--------------
minimum time: 55.602 ms (0.00% GC)
median time: 59.753 ms (0.00% GC)
mean time: 60.275 ms (0.00% GC)
maximum time: 79.889 ms (0.00% GC)
--------------
samples: 83
evals/sample: 1
julia> using Yeppp
julia> function logistic_regressiony!(w, Y, X, iterations, step = -1.0)
Xw = similar(w, size(X,1))
buffer = similar(w, length(Y));
for i in 1:iterations
A_mul_B!(Xw, X, w)
@. buffer = - Y * Xw
Yeppp.exp!(buffer)
@. buffer = ((1.0 / (1.0 + buffer) - 1.0) * Y)
Base.LinAlg.BLAS.gemv!('T', 1.0, X, buffer, step, w)
end
w
end
logistic_regressiony! (generic function with 2 methods)
julia> @benchmark logistic_regressiony!($w, $Y, $X, 1000)
BenchmarkTools.Trial:
memory estimate: 25.25 KiB
allocs estimate: 2
--------------
minimum time: 26.766 ms (0.00% GC)
median time: 27.792 ms (0.00% GC)
mean time: 27.980 ms (0.00% GC)
maximum time: 31.109 ms (0.00% GC)
--------------
samples: 179
evals/sample: 1
Hmm…not much of an improvement. And plain old @fastmath
continues to disappoint.
Hmm, especially now that I have to start adding Base.LinAlg
there’s little reason not to take advantage of great stuff like that.