mhtess / rwebppl

RWebPPL, an R interface to Webppl http://webppl.org
22 stars 7 forks source link

caching compiled code #41

Closed mcfrank closed 8 years ago

mcfrank commented 8 years ago

would be awesome if a model, once compiled, could be rerun with no extra compilation cost. seems like the web-app does this? there are some comparable workarounds for stan.

not sure the best way to implement/think about this? or even whether it's possible.

use case:

probs <- expand.grid(feature = c(1, 2, 3), 
                   value = TRUE, 
                   n_examples = 1:5) %>%
  split(.$n_examples) %>%
  map_df(function(x) {
    results <- webppl(program_file = "concept_learning.js", 
           data = x, 
           data_var = "df") 

    results$n_examples <- x$n_examples[1]
    return(results)
    })

and model:

// noise up feature
var noisy_feature = function (f) {
  return(flip(0.1) ? !f : f);
};

// sample concept
var get_concept = function () {
  return([flip(0.5), flip(0.5), flip(0.5)]);
};

// returns concept, conditioning on noisy observation
var model = function() {
  var this_concept = get_concept();

  var input = repeat(df[0].n_examples, 
                     function() {map(noisy_feature, this_concept)});
  var target = repeat(df[0].n_examples, 
                     function() {[df[0].value, df[1].value, df[2].value]});             

  factor(_.isEqual(input, target) ? 0 : -Infinity);

  return([this_concept[0], this_concept[1], this_concept[2]]);
};

Infer({method: 'enumerate'}, model);
hawkrobe commented 8 years ago

This is a great idea! This has actually been an open webppl issue for over a year now and I believe some progress is being made in the next couple weeks (see also #484 and #270) -- once those changes get made, rwebppl will automatically inherit the speed-up, so I'm closing this for now.