Skip to content

Recontruct vs Function with array of parameters #1969

Closed as not planned
Closed as not planned
@deveshjawla

Description

@deveshjawla

There is a significant difference in the time it takes to perform inference using the reconstruct method below versus defining the function model i.e. the neural network using a function feedforward() as below. For a very simple and small problem like the IRIS dataset, the difference in efficiency when using the function method the inference happens in under one minute, and when using the reconstruct method then it takes 30 minutes. Any ideas why this happens and how to make the reconstruct method comparable?

@model function bayesnnMVG(x, y, μ_prior, σ_prior, reconstruct)
    θ ~ MvNormal(μ_prior, σ_prior)
    nn = reconstruct(θ)
    ŷ = nn(x)
    for i = 1:lastindex(y)
        y[i] ~ Categorical(ŷ[:, i])
    end
end

where parameters_initial, reconstruct = Flux.destructure(nn_initial)

As compared to the below

@model  function bayesnnMVG(x, y, μ_prior, σ_prior)
    θ ~ MvNormal(μ_prior, σ_prior)
    nn = feedforward(θ)

    ŷ = nn(x)
    for i = 1:lastindex(y)
        y[i] ~ Categorical(ŷ[:, i])
    end
end

where

function feedforward(θ::AbstractVector)
       W0 = reshape(θ[1:20], 5, 4)
      ...
       model = Chain(
           Dense(W0, b0, relu),
          ...
       return model
   end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions