juliarecurrent-neural-networkflux.jl

Evaluate simple RNN in Julia Flux


I'm trying to learn Recurrent Neural Networks (RNN) with Flux.jl in Julia by following along some tutorials, like Char RNN from the FluxML/model-zoo.

I managed to build and train a model containing some RNN cells, but am failing to evaluate the model after training.

Can someone point out what I'm missing for this code to evaluate a simple (untrained) RNN?

julia> using Flux
julia> simple_rnn = Flux.RNN(1, 1, (x -> x))
julia> simple_rnn.([1, 2, 3])

ERROR: MethodError: no method matching (::Flux.RNNCell{var"#1#2", Matrix{Float32}, Vector{Float32}, Matrix{Float32}})(::Matrix{Float32}, ::Int64)
Closest candidates are:
  (::Flux.RNNCell{F, A, V, var"#s263"} where var"#s263"<:AbstractMatrix{T})(::Any, ::Union{AbstractMatrix{T}, AbstractVector{T}, Flux.OneHotArray}) where {F, A, V, T} at C:\Users\UserName\.julia\packages\Flux\6o4DQ\src\layers\recurrent.jl:83
Stacktrace:
 [1] (::Flux.Recur{Flux.RNNCell{var"#1#2", Matrix{Float32}, Vector{Float32}, Matrix{Float32}}, Matrix{Float32}})(x::Int64)
   @ Flux C:\Users\UserName\.julia\packages\Flux\6o4DQ\src\layers\recurrent.jl:34
 [2] _broadcast_getindex_evalf
   @ .\broadcast.jl:648 [inlined]
 [3] _broadcast_getindex
   @ .\broadcast.jl:621 [inlined]
 [4] getindex
   @ .\broadcast.jl:575 [inlined]
 [5] copy
   @ .\broadcast.jl:922 [inlined]
 [6] materialize(bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}, Nothing, Flux.Recur{Flux.RNNCell{var"#1#2", Matrix{Float32}, Vector{Float32}, Matrix{Float32}}, Matrix{Float32}}, Tuple{Vector{Int64}}})
   @ Base.Broadcast .\broadcast.jl:883
 [7] top-level scope
   @ REPL[3]:1
 [8] top-level scope
   @ C:\Users\UserName\.julia\packages\CUDA\LTbUr\src\initialization.jl:81

I'm using Julia 1.6.1 on Windows 10.


Solution

  • Turns out it's just a problem with the input type.

    Doing something like this will work:

    julia> v = Vector{Vector{Float32}}([[1], [2], [3]])
    julia> simple_rnn.(v)
    3-element Vector{Vector{Float32}}:
     [9.731078]
     [16.657223]
     [28.398548]
    

    I tried a lot of combinations until I found the working one. There is probably a way to automatically convert the input with some evaluation function.