Exemplo n.º 1
0
 def _record_directive_frozen(self, directiveId):
     # TODO This update will needlessly prevent freezing procedure assumes.
     value = self.trace.extractValue(directiveId)
     directive = self.directives[directiveId]
     if directive[0] == "define":
         self.directives[directiveId] = [
             "define", directive[1], v.quote(value)
         ]
     elif directive[0] == "observe":
         self.directives[directiveId] = [
             "observe", v.quote(value), directive[2]
         ]
     elif directive[0] == "evaluate":
         self.directives[directiveId] = ["evaluate", v.quote(value)]
     else:
         assert False, "Impossible directive type %s detected" % directive[0]
Exemplo n.º 2
0
    def observe_dataset(self, proc_expression, iterable, label=None):
        """Observe a general dataset.

Syntax:
ripl.observe_dataset("<expr>", <iterable>)

- The `<expr>` must evaluate to a (presumably stochastic) Venture
  procedure.  We expect in typical usage expr would just look up a
  recent assume.

- The `<iterable>` is a Python iterable each of whose elements must be a
  nonempty list of valid Venture values.

- There is no Venture syntax for this; it is accessible only when
  using Venture as a library.

Semantics:

- As to its effect on the distribution over traces, this is equivalent
  to looping over the contents of the given iterable, calling
  ripl.observe on each element as `ripl.observe("(<expr> *item[:-1])", item[-1])`.
  In other words, the first elements of each item of
  the iterable give the arguments to the procedure given by `<expr>`,
  and the last element gives the value to observe.

- The ripl method returns a list of directive ids, which correspond to
  the individual observes thus generated.

Open issues:

- If the `<expr>` is itself stochastic, it is unspecified whether we
  notionally evaluate it once per bulk_observe or once per data item.

- This is not the same as directly observing sufficient statistics
  only.

- It is currently not possible to forget the whole bulk_observe at
  once.

- Currently, list_directives will not respect the nesting structure of
  observations implied by `bulk_observe`.  How can we improve this? Do
  we represent the bulk_observe as one directive? If so, we can hardly
  return a useful representation of the iterable representing the data
  set. If not, we will hardly win anything because list_directives
  will generate all those silly per-datapoint observes (every time
  it's called!)

        """
        ret_vals = []
        parsed = self._ensure_parsed_expression(proc_expression)
        for i, args_val in enumerate(iterable):
            expr = [parsed] + [v.quote(a) for a in args_val[:-1]]
            lab = label + "_" + str(i) if label is not None else None
            ret_vals.append(self.observe(expr, args_val[-1], lab))
        return ret_vals
Exemplo n.º 3
0
 def in_model(self, model, action):
   current_model = self.model
   self.model = model
   # TODO asStackDict doesn't do the right thing because it tries to
   # be politely printable.  Maybe I should change that.
   stack_dict_action = {"type":"SP", "value":action}
   program = [v.sym("run"), v.quote(stack_dict_action)]
   try:
    with self.ripl.sivm.cleared():
     with self.inference_trace():
       did = self._do_raw_evaluate(program)
       ans = self.infer_trace.extractRaw(did)
       self.infer_trace.uneval(did) # TODO This becomes "forget" after the engine.Trace wrapper
       return (ans, model)
   finally:
     self.model = current_model
Exemplo n.º 4
0
 def _particle_swapping(self, action):
   ripl = self.ripl
   # disallow the ripl.
   class NoRipl(object):
     def __getattr__(self, attr):
       if attr in ['sample', 'sample_all', 'force']:
         return getattr(ripl, attr)
       else:
         raise VentureException('Modeling commands not allowed in for_each_particle.')
   self.ripl = NoRipl()
   # TODO asStackDict doesn't do the right thing because it tries to
   # be politely printable.  Maybe I should change that.
   stack_dict_action = {"type":"SP", "value":action}
   program = [v.sym("run"), v.quote(stack_dict_action)]
   def do_action(_trace):
     with self.inference_trace():
       did = self._do_raw_evaluate(program)
       ans = self.infer_trace.extractRaw(did)
       self.infer_trace.uneval(did) # TODO This becomes "forget" after the engine.Trace wrapper
       return ans
   try:
     yield do_action
   finally:
     self.ripl = ripl
Exemplo n.º 5
0
 def p_primary_unquote(self, op, e):
     return ast.locmerge(op, e,
                         val.quote(ast.locmerge(op, e, val.unquote(e))))
Exemplo n.º 6
0
def propRiplRoundTripThroughStack(value):
    expr = v.quote(value.asStackDict())
    result = carefully(eval_in_ripl, expr)
    assert value.equal(result)