Skip to content

Zygote and UnitSimplex #31

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

Open
GBarnsley opened this issue May 31, 2024 · 0 comments
Open

Zygote and UnitSimplex #31

GBarnsley opened this issue May 31, 2024 · 0 comments

Comments

@GBarnsley
Copy link

Apologies if this is the incorrect repo for this issue

I was experimenting with UnitSimplex transformations in a DynamicHMC model and noticed that the transformation seems to be incompatible with AD via Zygote. This is a pity since Zygote is by far the fastest backend for my model.

The error occurs since transform_with on UnitSimplex mutates an array.

I've written a minimal working example based on a Multinomial model here:

using TransformVariables, LogDensityProblems, LogDensityProblemsAD, SimpleUnPack

#Mulitnomial distribution
n_cats = 3;
data = [100, 200, 300];

variable_transform = as((
    ρ = UnitSimplex(n_cats),
))

struct log_density
    data::Array{Int, 1}
end

function (problem::log_density)(θ, ::Type{T} = Float64) where {T}
    @unpack data = problem # extract the data
    @unpack ρ = θ # extract the parameters

    # log likelihood
    return sum(data .* log.(ρ))
end

problem = log_density(data);
transformed_model = TransformedLogDensity(variable_transform, problem);

#LD calculation works
pars = zeros(LogDensityProblems.dimension(transformed_model));
problem(TransformVariables.transform(variable_transform, pars))

#Forward Diff Works
model_gradient_fwd = ADgradient(:ForwardDiff, transformed_model);
LogDensityProblems.logdensity_and_gradient(model_gradient_fwd, pars)

#Zygote doesn't work
import Zygote
model_gradient_zy = ADgradient(:Zygote, transformed_model);
LogDensityProblems.logdensity_and_gradient(model_gradient_zy, pars)

Would be possible to implement so sort of work around for UnitSimplex?

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