# Method error for maxpool(x,(2,2))

**URL:** https://discourse.julialang.org/t/method-error-for-maxpool-x-2-2/24693
**Category:** Machine Learning
**Created:** [May 28, 2019, 12:09pm UTC](https://discourse.julialang.org/t/method-error-for-maxpool-x-2-2/24693 "2019-05-28T12:09:31Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Don\_Burgess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/don_burgess/32/8252_2.png) [@Don\_Burgess](https://discourse.julialang.org/u/Don_Burgess)
#### Post date: [May 28, 2019, 12:09pm UTC](https://discourse.julialang.org/t/method-error-for-maxpool-x-2-2/24693/1 "2019-05-28T12:09:31Z")

</div>

This call from a formerly work code seems to throw a method error:  
x → maxpool(x, (2,2)),

ERROR: MethodError: no method matching maxpool(::TrackedArray{…,Array{Float32,4}}, ::Tuple{Int64,Int64})

=======

# Has the interface for “maxpool” changed? How would I update the following code: thank you very much

**julia\>** m = Chain(  
Conv((5,5), 3=\>16, relu),  
x → maxpool(x, (2,2)),  
Conv((5,5), 16=\>8, relu),  
x → maxpool(x, (2,2)),  
x → reshape(x, :, size(x, 4)),  
Dense(200, 120),  
Dense(120, 84),  
Dense(84, 10),  
softmax)  
Chain(Conv((5, 5), 3=\>16, NNlib.relu), getfield(Main, Symbol(“##9#12”))(), Conv((5, 5), 16=\>8, NNlib.relu), getfield(Main, Symbol(“##10#13”))(), getfield(Main, Symbol(“##11#14”))(), Dense(200, 120), Dense(120, 84), Dense(84, 10), NNlib.softmax)

**julia\>** using Flux: crossentropy, Momentum

**julia\>** loss(x, y) = sum(crossentropy(m(x), y))

loss (generic function with 1 method)

**julia\>** opt = Momentum(params(m), 0.01)

**┌ Warning:** Momentum(params) is deprecated; use Momentum(η::Float64) instead

**│** caller = top-level scope at none:0

**└** @ Core none:0

#24 (generic function with 1 method)

**julia\>** accuracy(x, y) = mean(onecold(m(x), 1:10) .== onecold(y, 1:10))

accuracy (generic function with 1 method)

**julia\>** epochs = 10

**julia\>** for epoch = 1:epochs  
for d in train  
l = loss(d…)  
back!(l)  
opt()  
@show accuracy(valX, valY)  
end  
end  
ERROR: MethodError: no method matching maxpool(::TrackedArray{…,Array{Float32,4}}, ::Tuple{Int64,Int64})  
Closest candidates are:  
maxpool(::TrackedArray, ::PoolDims; kw…) at /Users/chedon/.julia/packages/Tracker/RRYy6/src/lib/array.jl:446  
maxpool(::AbstractArray{xT,N}, ::PoolDims; kwargs…) where {xT, N} at /Users/chedon/.julia/packages/TimerOutputs/7zSea/src/TimerOutput.jl:198  
Stacktrace:

[1] **(::getfield(Main, Symbol(“##9#12”)))(** ::TrackedArray{…,Array{Float32,4}} **)** at **./REPL[20]:3**

[2] **applychain(** ::Tuple{getfield(Main, Symbol(“##9#12”)),Conv{2,4,typeof(relu),TrackedArray{…,Array{Float32,4}},TrackedArray{…,Array{Float32,1}}},getfield(Main, Symbol(“##10#13”)),getfield(Main, Symbol(“##11#14”)),Dense{typeof(identity),TrackedArray{…,Array{Float32,2}},TrackedArray{…,Array{Float32,1}}},Dense{typeof(identity),TrackedArray{…,Array{Float32,2}},TrackedArray{…,Array{Float32,1}}},Dense{typeof(identity),TrackedArray{…,Array{Float32,2}},TrackedArray{…,Array{Float32,1}}},typeof(softmax)}, ::TrackedArray{…,Array{Float32,4}} **)** at **/Users/chedon/.julia/packages/Flux/qXNjB/src/layers/basic.jl:31** (repeats 2 times)

[3] **(::Chain{Tuple{Conv{2,4,typeof(relu),TrackedArray{…,Array{Float32,4}},TrackedArray{…,Array{Float32,1}}},getfield(Main, Symbol(“##9#12”)),Conv{2,4,typeof(relu),TrackedArray{…,Array{Float32,4}},TrackedArray{…,Array{Float32,1}}},getfield(Main, Symbol(“##10#13”)),getfield(Main, Symbol(“##11#14”)),Dense{typeof(identity),TrackedArray{…,Array{Float32,2}},TrackedArray{…,Array{Float32,1}}},Dense{typeof(identity),TrackedArray{…,Array{Float32,2}},TrackedArray{…,Array{Float32,1}}},Dense{typeof(identity),TrackedArray{…,Array{Float32,2}},TrackedArray{…,Array{Float32,1}}},typeof(softmax)}})(** ::Array{Float32,4} **)** at **/Users/chedon/.julia/packages/Flux/qXNjB/src/layers/basic.jl:33**

[4] **loss(** ::Array{Float32,4}, ::Flux.OneHotMatrix{Array{Flux.OneHotVector,1}} **)** at **./REPL[22]:1**

[5] top-level scope at **./REPL[26]:3** [inlined]

[6] top-level scope at **./none:0**

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [May 30, 2019, 1:38pm UTC](https://discourse.julialang.org/t/method-error-for-maxpool-x-2-2/24693/2 "2019-05-30T13:38:22Z")

</div>

I think it’s now MaxPool?

Please quote your code with backticks.

---

<div class="post-metadata">

### Author: ![Don\_Burgess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/don_burgess/32/8252_2.png) [@Don\_Burgess](https://discourse.julialang.org/u/Don_Burgess)
#### Post date: [May 30, 2019, 5:13pm UTC](https://discourse.julialang.org/t/method-error-for-maxpool-x-2-2/24693/3 "2019-05-30T17:13:46Z")

</div>

Thank you very much for your response. When I replace “maxpool” with “MaxPool”, I receive the same error.

code for neural network

> Blockquote  
> m = Chain(  
> Conv((5,5), 3=\>16, relu),  
> x → maxpool(x, (2,2)),  
> Conv((5,5), 16=\>8, relu),  
> x → maxpool(x, (2,2)),  
> x → reshape(x, :, size(x, 4)),  
> Dense(200, 120),  
> Dense(120, 84),  
> Dense(84, 10),  
> softmax)

code for training:

> Blockquote  
> using Flux: crossentropy, Momentum  
> loss(x, y) = sum(crossentropy(m(x), y))  
> opt = Momentum(params(m), 0.01)  
> accuracy(x, y) = mean(onecold(m(x), 1:10) .== onecold(y, 1:10))  
> epochs = 10  
> for epoch = 1:epochs  
> for d in train  
> l = loss(d…)  
> back!(l)  
> opt()  
> @show accuracy(valX, valY)  
> end  
> end

error message:

> Blockquote  
> **ERROR:** MethodError: no method matching MaxPool(::TrackedArray{…,Array{Float32,4}}, ::Tuple{Int64,Int64})  
> Closest candidates are:  
> MaxPool(::Tuple{Vararg{Int64,N}}, ::Tuple{Vararg{Int64,M}}, ::Tuple{Vararg{Int64,N}}) where {N, M} at /Users/chedon/.julia/packages/Flux/qXNjB/src/layers/conv.jl:210

---

<div class="post-metadata">

### Author: ![kiliantics](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kiliantics/32/8948_2.png) [@kiliantics](https://discourse.julialang.org/u/kiliantics)
#### Post date: [June 17, 2019, 11:44pm UTC](https://discourse.julialang.org/t/method-error-for-maxpool-x-2-2/24693/4 "2019-06-17T23:44:46Z")

</div>

I had success using the following:

```julia
x -> maxpool(x, PoolDims(x,(2,2)))

```

This can also be written:

```julia
x -> MaxPool((2,2))(x)

```

This can be understood from the code defined in [conv.jl](https://github.com/FluxML/Flux.jl/blob/8ee6af1bee39bd9f6ed8e6cdf854ea9aaccb14dd/src/layers/conv.jl)

---

<div class="post-metadata">

### Author: ![hossein\_pourbozorg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hossein_pourbozorg/32/7152_2.png) [@hossein\_pourbozorg](https://discourse.julialang.org/u/hossein_pourbozorg)
#### Post date: [June 18, 2019, 6:15am UTC](https://discourse.julialang.org/t/method-error-for-maxpool-x-2-2/24693/5 "2019-06-18T06:15:50Z")

</div>

I think you should write the code like this:

```julia
using Flux

m = Chain(
    Conv((5,5), 3=>16, relu),
    MaxPool((2,2)),
    Conv((5,5), 16=>8, relu),
    MaxPool((2,2)),
    x -> reshape(x, :, size(x, 4)),
    Dense(200, 120),
    Dense(120, 84),
    Dense(84, 10),
    softmax,
)

loss(x, y) = sum(Flux.crossentropy(m(x), y))
opt = Momentum(0.01)
accuracy(x, y) = mean(Flux.onecold(m(x), 1:10) .== Flux.onecold(y, 1:10))
n_epochs = 10

@Flux.epochs n_epochs Flux.train!(
	loss, params(m), train, opt,
	cb=() -> @show accuracy(valX, valY),
)

```
