在 Julia Flux 中评估简单的 RNN

Evaluate simple RNN in Julia Flux

我正在尝试使用 Flux.jl in Julia by following along some tutorials, like Char RNN from the FluxML/model-zoo 学习递归神经网络 (RNN)。

我设法构建并训练了一个包含一些 RNN 单元的模型,但我未能在训练后评估模型。

有人可以指出我在评估简单(未经训练的)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\Fluxo4DQ\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\Fluxo4DQ\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

我在 Windows 10.

上使用 Julia 1.6.1

原来只是输入类型的问题

这样做会奏效:

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

我尝试了很多组合,直到找到有效的组合。可能有一种方法可以使用一些评估函数自动转换输入。