Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error while concatenating TrackedArray with normal array #83

Open
efmanu opened this issue May 22, 2020 · 0 comments
Open

Error while concatenating TrackedArray with normal array #83

efmanu opened this issue May 22, 2020 · 0 comments

Comments

@efmanu
Copy link

efmanu commented May 22, 2020

Hi,

In my model, I am trying to vector concatenate arrays like below:

x_rnd ~ MvNormal(x,fill(0.1,3))
 z_rnd ~ MvNormal(z,fill(0.1,3))
y = rand(3)
 a_rnd = vcat(x_rnd, y,z_rnd)
I am getting the following error while sampling by using reverse differentiation AD backend. 
ERROR: MethodError: vcat(::ReverseDiff.TrackedArray{Float64,Float64,1,Array{Float64,1},Array{Float64,1}}, ::Array{Float64,1}, ::ReverseDiff.TrackedArray{Float64,Float64,1,Array{Float64,1},Array{Float64,1}}) is ambiguous. Candidates:
  vcat(611::ReverseDiff.TrackedArray, 612::AbstractArray, x::Union{ReverseDiff.TrackedArray, ReverseDiff.TrackedReal}, xs::Union{Number, AbstractArray}...) in DistributionsAD.ReverseDiffX at C:\Users\manoos\.julia\packages\DistributionsAD\xJ5bm\src\reversediffx.jl:43
  vcat(V::AbstractArray{T,1} where T...) in Base at abstractarray.jl:1287
  vcat(x::Union{ReverseDiff.TrackedArray, ReverseDiff.TrackedReal}, xs::Union{Number, AbstractArray}...) in DistributionsAD.ReverseDiffX at C:\Users\manoos\.julia\packages\DistributionsAD\xJ5bm\src\reversediffx.jl:43
  vcat(A::Union{AbstractArray{T,2}, AbstractArray{T,1}} where T...) in Base at abstractarray.jl:1350
  vcat(609::AbstractArray, 610::AbstractArray, x::Union{ReverseDiff.TrackedArray, ReverseDiff.TrackedReal}, xs::Union{Number, AbstractArray}...) in DistributionsAD.ReverseDiffX at C:\Users\manoos\.julia\packages\DistributionsAD\xJ5bm\src\reversediffx.jl:43
  vcat(A::Union{LinearAlgebra.UniformScaling, Union{AbstractArray{T,2}, AbstractArray{T,1}} where T}...) in LinearAlgebra at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.3\LinearAlgebra\src\uniformscaling.jl:310
Possible fix, define
  vcat(::ReverseDiff.TrackedArray{V,D,1,VA,DA} where DA where VA where D where V, ::AbstractArray{T,1} where T, ::ReverseDiff.TrackedArray{V,D,1,VA,DA} where DA where VA where D where V, ::Vararg{AbstractArray{T,1} where T,N} where N)

The MWE for the above issue is as below:

using Turing
using DistributionsAD
using LazyArrays
using ReverseDiff
dist_f(a,b) = sqrt(sum((a .- b).^2))
function mean_y(a,b,r)
	y_m = map(dist_f,a,b)
	y_c = y_m.*r
	return y_c
end

function extract_cords(a) 
	(i,j,k) =  map(x->getindex.(a, x), 1:3)
	return vec(i), vec(j), vec(k)
end

@model test_samp(y, a, b, x, y1, z,::Type{T}=Float64) where {T} = begin
	mn = length(x)
	r_dis ~ filldist(Uniform(0.1, 1.0), 4)
	x_rnd ~ MvNormal(x,fill(0.1,mn))
	# y_rnd ~ MvNormal(vec(a_rn[2,:]),fill(0.1,mn))
    z_rnd ~ MvNormal(z,fill(0.1,mn))

    # a_rnd = vcat(vcat(x_rnd, y1), z_rnd)
    a_rnd = vcat(x_rnd, y1,z_rnd)
	mean_arnd = reshape(a_rnd, (3,mn))
	resha_mean_arnd = reshape([(mean_arnd[1,i], mean_arnd[2,i], mean_arnd[3,i]) for i in 1:size(mean_arnd, 2)], size(a))
	mean_proj_img = mean_y(resha_mean_arnd, b,  reshape(r_dis,(2,2)))
    dists = DistributionsAD.lazyarray(Normal, copy(mean_proj_img), 0.1)
    y ~ arraydist(dists)
end
r = rand(Float64,2,2);
a = fill((1.0, 2.0, 0.2), 2, 2)
b = fill((0.0, 0.1, 0.6), 2, 2)
(x,y,z) = extract_cords(a)
out = rand(size(a,1),size(a,2))
model = test_samp(out, a, b, x, y, z)
Turing.setadbackend(:reversediff)
chain = sample(model, NUTS(0.65), 100)

Regards,
Manu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant