# Struct with Conv

**URL:** <https://discourse.julialang.org/t/struct-with-conv/99061>\
**Category:** New to Julia\
**Created:** [May 18, 2023, 1:46pm UTC](https://discourse.julialang.org/t/struct-with-conv/99061 "2023-05-18T13:46:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Helectus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helectus/32/47475_2.png) [@Helectus](https://discourse.julialang.org/u/Helectus)\
**Post date:** [May 18, 2023, 1:46pm UTC](https://discourse.julialang.org/t/struct-with-conv/99061/1 "2023-05-18T13:46:58Z")

</div>

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

```julia
  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

```julia
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?

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [May 18, 2023, 2:19pm UTC](https://discourse.julialang.org/t/struct-with-conv/99061/2 "2023-05-18T14:19:09Z")

</div>

> [@Helectus](#):
>
> ```julia
> resized = reshape(mat, (x,y,z, 1))
> cap = co.conv1(resized)
> cap = reshape(mat, (x,y,z))
> 
> ```

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)`.

> [@](#):
>
> ```julia
> struct Conv1D2
> conv1::Conv
>   
> function Conv1D2(input_len::Int, output_len::Int)
> 
> ```

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.
