def param(name, *args, **kwargs): """ Saves the variable as a parameter in the param store. To interact with the param store or write to disk, see `Parameters <parameters.html>`_. :param name: name of parameter :returns: parameter """ if len(_PYRO_STACK) == 0: return _PYRO_PARAM_STORE.get_param(name, *args, **kwargs) else: msg = { "type": "param", "name": name, "args": args, "kwargs": kwargs, "scale": 1.0, "cond_indep_stack": [], "value": None, "done": False, "stop": False, } # apply the stack and return its return value out_msg = apply_stack(msg) return out_msg["value"]
def _pyro_param(self, msg): """ :param msg: current message at a trace site. :returns: the result of querying the parameter store Implements default pyro.param Poutine behavior: queries the parameter store with the site name and varargs and returns the result of the query. If the parameter doesn't exist, create it using the site varargs. If it does exist, grab it from the parameter store. Derived classes often compute a side effect, then call super(Derived, self)._pyro_param(msg). """ name, args, kwargs = \ msg["name"], msg["args"], msg["kwargs"] # msg["done"] enforces the guarantee in the poutine execution model # that a site's non-effectful primary computation should only be executed once: # if the site already has a stored return value, # don't reexecute the function at the site, # and do any side effects using the stored return value. if msg["done"]: return msg["value"] ret = _PYRO_PARAM_STORE.get_param(name, *args, **kwargs) # after the param store has been queried, update msg["done"] # to prevent it from being queried again. msg["done"] = True return ret