Code Monkey home page Code Monkey logo

Comments (7)

phipsgabler avatar phipsgabler commented on June 2, 2024 1

To wrap up: unless we do crazy world-age and eval tricks, the answer is likely NO, unless we give up the possibility of defining models other than at toplevel.

from dynamicppl.jl.

devmotion avatar devmotion commented on June 2, 2024

Hmm isn't

const inner_function = Mymodel_Function_1()

problematic? But maybe that could be avoided if you define

function model_1(y)
  return Model(Mymodel_Function_1(), (y = y,), ModelGen{(:y,)}(mymodel_1, NamedTuple()))
end

from dynamicppl.jl.

phipsgabler avatar phipsgabler commented on June 2, 2024

I wrote that because otherwise, a new object every time. There is no closed-over data in this variant, anyway -- it's meant as a singleton type, so what would be problematic?

But another thing I now worry about: I think producing a type makes the macro unable to be used in non-toplevel places. Does putting a model inside e.g. a function work in all cases currently?

from dynamicppl.jl.

devmotion avatar devmotion commented on June 2, 2024

I wrote that because otherwise, a new object every time. There is no closed-over data in this variant, anyway -- it's meant as a singleton type, so what would be problematic?

I assumed it would be problematic since it would not be possible to define another model if inner_function is already defined. Additionally, there's no need for inner_function it seems since there is always only one instance of Mymodel_Function_1 as the Julia documentation explains:

Immutable composite types with no fields are singletons; there can be only one instance of such types: [...]
The === function confirms that the "two" constructed instances of NoFields are actually one and the same.

from dynamicppl.jl.

phipsgabler avatar phipsgabler commented on June 2, 2024

I assumed it would be problematic since it would not be possible to define another model if inner_function is already defined.

Oh, I assumed it to be gensymed as well.

Additionally, there's no need for inner_function it seems since there is always only one instance of Mymodel_Function_1 as the Julia documentation explains:

Right, I forgot about that! And we could even make it simpler, without having to create a separate type each time, by using a kind of "anonymous struct":

function (::ModelFunction{Symbol("##mymodel#474")})(vi, spl, ctx, model)
   local y
   # transformed model code
end

function var"##model#473"(y)
  return Model(ModelFunction{Symbol("##mymodel#474")}(), (y = y,), ModelGen{(:y,)}(var""##model#473", NamedTuple()))
end

mymodel = ModelGen{(:y,)}(var""##model#473", NamedTuple())

from dynamicppl.jl.

phipsgabler avatar phipsgabler commented on June 2, 2024

Or, more simply

function (model::Model{Symbol("##mymodel#474")})(vi, spl, ctx)
   local y
   # transformed model code
end

function mymodel(y)
  return Model{Symbol("##mymodel#474")}((y = y,), getmissings(y))
end

with

struct Model{name, Targs<:NamedTuple, Tmissings<:Val}
  args::Targs
  missings::Tmissings
end

if that's possible without the ModelGen.

from dynamicppl.jl.

devmotion avatar devmotion commented on June 2, 2024

if that's possible without the ModelGen.

I'm not familiar with all details of DynamicPPL but AFAIK the advantage of having (and saving) the ModelGen is to be able to generate a new model with different data (which can even be missing partially). But I guess that could be part of Model as well by saving the default arguments?

At least it seems possible to replace

struct Model{F, Targs <: NamedTuple, Tmodelgen, Tmissings <: Val} <: AbstractModel
f::F
args::Targs
modelgen::Tmodelgen
missings::Tmissings
end
with

struct Model{name, Targs <: NamedTuple, Tmodelgen, Tmissings <: Val} <: AbstractModel
    args::Targs
    modelgen::Tmodelgen
    missings::Tmissings
end

function Model{name}(args::NamedTuple, modelgen, missings::Val = getmissing(args))
    return Model{names,typeof(args),typeof(modelgen),typeof(missings)}(args, modelgen, missings)
end

and just implement

function (model::Model{Symbol("##mymodel#474")})(vi, spl, ctx)
   local y
   # transformed model code
end

as you suggested.

Possibly (not sure about this) one could get rid of ModelGen by adding
Targs and defaults from ModelGen to Model and replacing the ModelGen functionality with something like

Base.similar(::Model, args...) = ...

from dynamicppl.jl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.