# Speed of Knet

**URL:** https://discourse.julialang.org/t/speed-of-knet/1927
**Category:** Machine Learning
**Tags:** knet
**Created:** [February 5, 2017, 7:58pm UTC](https://discourse.julialang.org/t/speed-of-knet/1927 "2017-02-05T19:58:46Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Tomas\_Pevny](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomas_pevny/32/25466_2.png) [@Tomas\_Pevny](https://discourse.julialang.org/u/Tomas_Pevny)
#### Post date: [February 5, 2017, 7:58pm UTC](https://discourse.julialang.org/t/speed-of-knet/1927/1 "2017-02-05T19:58:46Z")

</div>

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
