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

Issue in the dot product within a NormalMeanVariance distribution #390

Closed
jesper-jpg opened this issue Dec 17, 2024 · 3 comments
Closed

Comments

@jesper-jpg
Copy link

Hi, I just tried to create a model in which I use the dot product to calculate \mu, the mean prediction. For clarification, i'll provide the code below:

@model function polynomial_regression(y, X, D, σ2)

# Prior distribution
β ~ MvNormalMeanCovariance(zeros(D + 1), 0.1 * diagm(ones(D + 1)))
# Define empty z
z = Vector{Vector{Float64}}(undef, length(X))

# Likelihood
for i in eachindex(X)

    # Polynomial
    z[i] = [X[i]^j for j in 0:D]
    
    # prediction
    μ = dot(β, z[i])
    # μ = β ⋅ z
    y[i] ~ NormalMeanVariance(μ, σ2[i])

end

end

Initialize variables

y = df[!,:y]
X = df[!,:x]
D = 3
σ2 = df[!,:σ2]
results = infer(
model = polynomial_regression(D = D, σ2 = σ2),
data = (y = y, X = X),
)

This code gives me the following error: StackOverflowError: Stacktrace: [1] dot(x::GraphPPL.NodeLabel, y::Float64) (repeats 79984 times) @ LinearAlgebra C:\Users\jespe.julia\juliaup\julia-1.10.7+0.x64.w64.mingw32\share\julia\stdlib\v1.10\LinearAlgebra\src\generic.jl:861

The error occurs in the dot function and it seems like it enters an infinite loop, creating a huge model or it tries to recursively resolve something adding TODO's in the stack until it overflows.

However, when I switch from:
μ = dot(β, z[i]) y[i] ~ NormalMeanVariance(μ, σ2[i])

To:
y[i] ~ NormalMeanVariance(dot(β, z[i]), σ2[i])

The function/model seems to work fine. Could this be an error in the package? Thanks for looking into it.
Kind regards,
Jesper

@bvdmitri
Copy link
Member

You should use := for deterministic relationships in the @model definition (ref1, ref2).

So in your example you should write

μ := dot(β, z[i])

Can you confirm that this fixes issue for you?

@jesper-jpg
Copy link
Author

That works! Thank you for the clarification and my apologies for the inconvenience.

@bvdmitri
Copy link
Member

@jesper-jpg Not a problem ,feel free to open other issues! I used your feedback here #391

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

2 participants