Flux.jl icon indicating copy to clipboard operation
Flux.jl copied to clipboard

Add repeat vector

Open Jafagervik opened this issue 1 year ago • 4 comments

RepeatVector Layer like in keras.

PS: I'm still new to contributing here, so if "trivial layers" like these are not requested, and rather something each user can implement on their own, then please let me know

PR Checklist

  • [X] Tests are added
  • [X] Entry in NEWS.md
  • [X] Documentation, if applicable

Jafagervik avatar Mar 24 '24 18:03 Jafagervik

I would be ok with adding a layer like this, although I don't like the naming and I can't think of a really good one. Test should be added in the test/ folder and the layer should be referenced in docs/

CarloLucibello avatar Mar 26 '24 06:03 CarloLucibello

What is this used for?

And what is the intended behaviour, on arrays of various dimensions? I'm not sure the code matches the words, nor the example given.

julia> RepeatVector(2)(rand(3)) |> size
(3, 2, 1)

julia> RepeatVector(2)(rand(3,5)) |> size
(3, 10, 1)

julia> RepeatVector(2)(rand(3,5,7)) |> size
(3, 10, 7, 1)

julia> rv = RepeatVector(3);

julia> rv([1, 2, 3])  # example says 3×3 Matrix
3×3×1 Array{Int64, 3}:

mcabbott avatar Mar 26 '24 13:03 mcabbott

The lack of clarity on how this layer should behave suggest to me that we might not want it as a built-in. Now, the good news is that Flux doesn't require a custom layer type for everything. In this case, it suffices to use a plain function/closure:

function repeatvector(x::AbstractArray, n::Int)
  expanded = reshape(x, (size(x)..., 1))
  repeated = repeat(expanded, outer = (1, rv.n, 1))
  return repeated
end
repeatvector(n::Int) = x -> repeatvector(x, n)
f = repeatvector(2)
# or, using built-in types:
f = Base.Fix2(repeatvector, n)

ToucheSir avatar Mar 28 '24 02:03 ToucheSir

Going along Brian's point, even if we were to include this, I think it ought to be a function not a struct. Though I tend to lean towards not including it.

darsnack avatar Mar 29 '24 01:03 darsnack