Julia - Method Error

Even though I defined the method, the method is still not recognised.
Following are the two functions defined.

@resumable function iterate_regions(self::Conv13, image)
    h, w = size(image)
    for i in 1:h-1
        for j in 1:w-1
            im_region = image[i:(i+3), j:(j+3)]
            @yield im_region, i , j

function forward(self::Conv13, input)
    self.last_input = input
    h, w = size(input)
    output = zeros(Float64, (h - 2, w - 2, self.num_filters))
    for (im_region, i, j) in iterate_regions(self, input)
        output[i, j] = sum(im_region * self.filters, dims=(1,2))
    return output

Now when I call the forward, just for checking the function, by giving it the struct instance as well as a 2-D array, it gives an unusual error.
forward(Conv::Conv3x3, convert(Array{Float64},train_images[:,:,1]))

MethodError: no method matching *(::Array{Float64,2}, ::Array{Float64,3})
Closest candidates are:
*(::Any, ::Any, !Matched::Any, !Matched::Any…) at operators.jl:529
*(!Matched::PyObject, ::Any) at C:\Users\dishant.julia\packages\PyCall\zqDXB\src\pyoperators.jl:13
*(::Union{DenseArray{T,2}, Base.ReinterpretArray{T,2,S,A} where S where A<:Union{SubArray{T,N,A,I,true} where I<:Union{Tuple{Vararg{Real,N} where N}, Tuple{AbstractUnitRange,Vararg{Any,N} where N}} where A<:DenseArray where N where T, DenseArray}, Base.ReshapedArray{T,2,A,MI} where MI<:Tuple{Vararg{Base.MultiplicativeInverses…
[1] forward(::Conv3x3, ::Array{Float64,2}) at .\In[16]:9
[2] top-level scope at In[26]:3

And even the stack trace shows that the values passed are correct. I don’t know why it is not able to find forward.

This is the problem, * doesn’t work on 3-arrays.

ones(2,2) * ones(2) # matrix * vector is ok
ones(2,2) * ones(2,2) # matrix * matrix is ok
ones(2,2) * ones(2,2,2) # MethodError

I’m not too sure what you are doing, but you may have wanted element-wise .* here? Or if you do want matrix multiplication, then you could reshape the second one.

Hey, that’s exactly what I was trying for. An element wise multiplication and I didn’t notice that the problem was in the internal definition rather than the function itself.
Thank you so much. It works now with .*

1 Like