Struct with Conv

I tried to create a struct that has just one Conv layer.

  struct Conv1D2
      conv1::Conv
  
      function Conv1D2(input_len::Int, output_len::Int)
          conv1 = Conv((1, 3), input_len=>output_len)
          new(conv1)
      end
  end

and then create the function

function (co::Conv1D2)(mat::A) where A<:AbstractArray
    x,y,z = size(mat)
    resized = reshape(mat, (x,y,z, 1))
    cap = co.conv1(resized)
    cap = reshape(mat, (x,y,z))
    return cap
end

Apparently, something is wrong. While initialising a dummy value of
con = Conv1D(5,32)
I couldnt call the Conv1D function as the “con” said it was just the Conv layer.
What did i do wrong? Can someone help me?

Note that you’re throwing away the output of co.conv1. And if the last line is return reshape(cap, (x,y,z)), it will fail as the output is a different size. You probably want dropdims(cap; dims=4).

Note also that this function is probably in the wrong place. A function inside a struct like this is called an “inner constructor” and is very rarely needed. It disables the default constructors, and thus makes something like Conv1D2(Conv((1, 3), 5 => 32)) fail.

To use this struct with Flux you will also want to call Flux.@functor Conv1D2. But this also assumes that there is no inner constructor.