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

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

I think it’s now MaxPool?

Please quote your code with backticks.

1 Like

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

I had success using the following:

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

This can also be written:

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

This can be understood from the code defined in conv.jl

I think you should write the code like this:

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),
)