TuringLang / Bijectors.jl

Implementation of normalising flows and constrained random variable transformations
https://turinglang.org/Bijectors.jl/
MIT License
200 stars 33 forks source link

Error "No method matching length" when calling "transformed" method #167

Closed mgmverburg closed 3 years ago

mgmverburg commented 3 years ago

Hi,

So I was trying out Bijectors and for some reason when I call the transformed method, I get an error:

julia> z = transformed(Beta())
Error showing value of type Bijectors.TransformedDistribution{Beta{Float64},Bijectors.Logit{0,Float64},Univariate}:
ERROR: MethodError: no method matching length(::Bijectors.Logit{0,Float64})
Closest candidates are:
  length(::Tracker.Grads, ::Any...; kwargs...) at /home/michiel/.julia/packages/MacroTools/gME9C/src/examples/forward.jl:17
  length(::LLVM.PhiIncomingSet) at /home/michiel/.julia/packages/LLVM/F9DFY/src/core/instructions.jl:212
  length(::DataStructures.IntSet) at /home/michiel/.julia/packages/DataStructures/5hvIb/src/int_set.jl:191
  ...
Stacktrace:
 [1] _use_multline_show(::Bijectors.TransformedDistribution{Beta{Float64},Bijectors.Logit{0,Float64},Univariate}, ::Tuple{Symbol,Symbol}) at /home/michiel/.julia/packages/Distributions/y63ft/src/show.jl:39
 [2] show(::IOContext{REPL.Terminals.TTYTerminal}, ::Bijectors.TransformedDistribution{Beta{Float64},Bijectors.Logit{0,Float64},Univariate}, ::Tuple{Symbol,Symbol}) at /home/michiel/.julia/packages/Distributions/y63ft/src/show.jl:19
 [3] show(::IOContext{REPL.Terminals.TTYTerminal}, ::Bijectors.TransformedDistribution{Beta{Float64},Bijectors.Logit{0,Float64},Univariate}) at /home/michiel/.julia/packages/Distributions/y63ft/src/show.jl:12
 [4] show(::IOContext{REPL.Terminals.TTYTerminal}, ::MIME{Symbol("text/plain")}, ::Bijectors.TransformedDistribution{Beta{Float64},Bijectors.Logit{0,Float64},Univariate}) at ./multimedia.jl:47
 [5] display(::REPL.REPLDisplay, ::MIME{Symbol("text/plain")}, ::Any) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/REPL/src/REPL.jl:214
 [6] display(::REPL.REPLDisplay, ::Any) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/REPL/src/REPL.jl:218
 [7] display(::Any) at ./multimedia.jl:328
 [8] (::Media.var"#15#16"{Bijectors.TransformedDistribution{Beta{Float64},Bijectors.Logit{0,Float64},Univariate}})() at /home/michiel/.julia/packages/Media/ItEPc/src/compat.jl:28
 [9] hookless(::Media.var"#15#16"{Bijectors.TransformedDistribution{Beta{Float64},Bijectors.Logit{0,Float64},Univariate}}) at /home/michiel/.julia/packages/Media/ItEPc/src/compat.jl:14
 [10] render(::Media.NoDisplay, ::Bijectors.TransformedDistribution{Beta{Float64},Bijectors.Logit{0,Float64},Univariate}) at /home/michiel/.julia/packages/Media/ItEPc/src/compat.jl:27
 [11] render(::Bijectors.TransformedDistribution{Beta{Float64},Bijectors.Logit{0,Float64},Univariate}) at /home/michiel/.julia/packages/Media/ItEPc/src/system.jl:160
 [12] display(::Media.DisplayHook, ::Bijectors.TransformedDistribution{Beta{Float64},Bijectors.Logit{0,Float64},Univariate}) at /home/michiel/.julia/packages/Media/ItEPc/src/compat.jl:9
 [13] display(::Any) at ./multimedia.jl:328
 [14] #invokelatest#1 at ./essentials.jl:710 [inlined]
 [15] invokelatest at ./essentials.jl:709 [inlined]
 [16] print_response(::IO, ::Any, ::Bool, ::Bool, ::Any) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/REPL/src/REPL.jl:238
 [17] print_response(::REPL.AbstractREPL, ::Any, ::Bool, ::Bool) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/REPL/src/REPL.jl:223
 [18] (::REPL.var"#do_respond#54"{Bool,Bool,REPL.var"#64#73"{REPL.LineEditREPL,REPL.REPLHistoryProvider},REPL.LineEditREPL,REPL.LineEdit.Prompt})(::Any, ::Any, ::Any) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/REPL/src/REPL.jl:822
 [19] #invokelatest#1 at ./essentials.jl:710 [inlined]
 [20] invokelatest at ./essentials.jl:709 [inlined]
 [21] run_interface(::REPL.Terminals.TextTerminal, ::REPL.LineEdit.ModalInterface, ::REPL.LineEdit.MIState) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/REPL/src/LineEdit.jl:2355
 [22] run_frontend(::REPL.LineEditREPL, ::REPL.REPLBackendRef) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/REPL/src/REPL.jl:1144
 [23] (::REPL.var"#38#42"{REPL.LineEditREPL,REPL.REPLBackendRef})() at ./task.jl:356

I noticed now that it seems like this error only causes a problem with the TransformedDistribution not being able to be printed, because apparently, despite the error I can still actually use the result z to sample from for example. E.g. even rand(transformed(Beta())) actually works without the error. So I guess this is potentially a minor thing, but still weird that error gets thrown. Additionally, I can just arbitrarily do something like length(::Logit{0, Float64}) = 1 which also fixes the problem, which leads me to believe that maybe there is another package that defines this function, which I simply don't have installed in my Julia environment?

I am using Julia 1.5.2 and the output of "] st" is as follows:

] st output ``` [fbe9abb3] AWS v1.23.1 [4f1ea46c] AWSCore v0.6.17 [1c724243] AWSS3 v0.8.1 [0d499d91] AWSSDK v0.5.0 [99985d1d] AbstractGPs v0.2.16 [c7e460c6] ArgParse v1.1.1 [76274a88] Bijectors v0.8.14 [336ed68f] CSV v0.8.2 [a93c6f00] DataFrames v0.22.2 [b4f34e82] Distances v0.10.0 [31c24e10] Distributions v0.24.8 [634d3b9d] DrWatson v1.16.6 [366bfd00] DynamicPPL v0.10.5 [1a297f60] FillArrays v0.10.1 [682c06a0] JSON v0.21.1 [ec8451be] KernelFunctions v0.8.11 [929cbde3] LLVM v3.5.2 [c7f686f2] MCMCChains v4.4.0 [c03570c3] Memoize v0.4.4 [90014a1f] PDMats v0.10.1 [91a5bcdd] Plots v1.9.1 [7f904dfe] PlutoUI v0.6.10 [37e2e3b7] ReverseDiff v1.4.5 [21efa798] SIMDPirates v0.8.26 [2d09df54] StatisticalRethinking v2.2.9 [4c63d2b9] StatsFuns v0.9.6 [f3b207a7] StatsPlots v0.14.17 [5d786b92] TerminalLoggers v0.1.2 [fce5fe82] Turing v0.15.5 [56ddb016] Logging ```
devmotion commented 3 years ago

This looks like a bug in Distributions that was fixed in >= 0.24.10: https://github.com/JuliaStats/Distributions.jl/pull/1244 Updating your old version of Distributions should fix the problem.

mgmverburg commented 3 years ago

Yep that indeed fixes it, thanks!