Speed of Knet

knet

#1

Hello All,
I would like to ask if anyone experienced with KNet can explain me, what I am doing wrong and why a simple code like this is incredibly slow

push!(LOAD_PATH,joinpath(homedir(),“Work/julia/Knet”))
using Patches
using JLD
using StatsBase
using Knet
function predict(w,x)
for i=1:2:length(w)-2
x = max(0, w[i]*x .+ w[i+1])
end
return w[end-1]*x .+ w[end]
end

imgs=load(joinpath(homedir(),“Work/data/tenofbossbase.jld”),“covers”)
x=randn(64,100000)
y=sample(1:2,100000)
loss(w,x,y) = mean(max(0,1-y.*predict(w,x)))

function train(k)
w=[randn(k,size(x,1)),zeros(k),randn(k,k),zeros(k),randn(1,k),0.0]

lossgradient=grad(loss)
prms=Sgd(;lr=0.001)
for i in 1:100
idxs=sample(1:size(x,2),1000);
xx=x[:,idxs];
yy=y[idxs]
dw = lossgradient(w,xx,yy)
update!(w,dw,prms)
println(i," ",loss(w,xx,yy))
end
end

train(25)

When I compare this to the same solution in TensorFlow, it order of magnitudes faster.
I should say that I do not use GPU and I have test it on MacBook Air, 2013.

Thanks for the answer.
Tomas