# Knet prediction with CNN

**URL:** https://discourse.julialang.org/t/knet-prediction-with-cnn/73527
**Category:** New to Julia
**Tags:** knet
**Created:** [December 23, 2021, 11:28am UTC](https://discourse.julialang.org/t/knet-prediction-with-cnn/73527 "2021-12-23T11:28:37Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![KKesgin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kkesgin/32/32168_2.png) [@KKesgin](https://discourse.julialang.org/u/KKesgin)
#### Post date: [December 23, 2021, 11:28am UTC](https://discourse.julialang.org/t/knet-prediction-with-cnn/73527/1 "2021-12-23T11:28:37Z")

</div>

Hi,  
I’m new to neural nets with Julia and I’m guessing what I’ll ask is a very trivial question but I never struggled this much when I was getting started with the TensorFlow and I’m resisting really hard to not go back since I’m struggling really hard with the documentation and examples of Knet for neural networks.

I have the following example of a network that I can train, however I have issue with running predictions:

> dtrn = minibatch(trainX, trainY, 200; xsize = (size(trainX,1),1,1,:))  
> dtst = minibatch(testX, testY, 200; xsize = (size(testX,1),1,1,:))
> 
> struct Conv; w; b; f; end  
> (c::Conv)(x) = c.f.(pool(conv4(c.w, x) .+ c.b))  
> Conv(w1,w2,cx,cy,f=relu) = Conv(param(w1,w2,cx,cy), param0(1,1,cy,1), f);
> 
> struct Dense; w; b; f; end  
> (d::Dense)(x) = d.f.(d.w \* mat(x) .+ d.b)  
> Dense(i::Int,o::Int,f=relu) = Dense(param(o,i), param0(o), f);
> 
> struct Chain; layers; Chain(args…)=new(args); end  
> (c::Chain)(x) = (for l in c.layers; x = l(x); end; x)  
> (c::Chain)(x,y) = nll(c(x),y)
> 
> LeNet = Chain(Conv(5,1,1,10), Conv(5,1,10,20), Conv(5,1,20,50), Dense(3250,500), Dense(500,unqLabelsNum,identity))
> 
> progress!(adam(LeNet, ncycle(dtrn,10)))  
> accuracy(LeNet, dtst)

I get the accuracy score, however when I run

> predict(LeNet, testX)

I get:  
`ERROR: MethodError: no method matching predict(::Chain, ::Array{Float32, 4})`

or when I run:

> predict(LeNet, dtst.x)

`ERROR: MethodError: no method matching predict(::Chain, ::Matrix{Float32})`

or even when I run

> LeNet(dtst.x)

I get

```julia
MethodError: no method matching NNlib.DenseConvDims(::Matrix{Float32}, ::KnetArray{Float32, 4}; stride=(), padding=(), dilation=(), flipkernel=false)
Closest candidates are:
  NNlib.DenseConvDims(::AbstractArray, ::AbstractArray; kwargs...)

```

Could someone tell me how I can run a very simple prediction on a model that is already trained exactly as it is demonstrated in the Knet documentation?

Thanks in advance!

---

<div class="post-metadata">

### Author: ![denizyuret](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/denizyuret/32/568_2.png) [@denizyuret](https://discourse.julialang.org/u/denizyuret)
#### Post date: [December 30, 2021, 9:43am UTC](https://discourse.julialang.org/t/knet-prediction-with-cnn/73527/2 "2021-12-30T09:43:16Z")

</div>

Hi, the error message indicates that this is a type issue: i.e. the first argument to the DenseConvDims operation is a Matrix (a cpu array), the second is a KnetArray (a gpu array). The data and model parameters should both be on the gpu or cpu. Please take a look at the examples in the README or tutorial folder in [https://github.com/denizyuret/Knet.jl](https://github.com/denizyuret/Knet.jl)
