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 sample(name, fn, *args, **kwargs): """ Calls the stochastic function `fn` with additional side-effects depending on `name` and the enclosing context (e.g. an inference algorithm). See `Intro I <http://pyro.ai/examples/intro_part_i.html>`_ and `Intro II <http://pyro.ai/examples/intro_part_ii.html>`_ for a discussion. :param name: name of sample :param fn: distribution class or function :param obs: observed datum (optional; should only be used in context of inference) optionally specified in kwargs :param dict baseline: Optional dictionary of baseline parameters specified in kwargs. See inference documentation for details. :returns: sample """ obs = kwargs.pop("obs", None) baseline = kwargs.pop("baseline", {}) # check if stack is empty # if stack empty, default behavior (defined here) if len(_PYRO_STACK) == 0: if obs is not None: warnings.warn( "trying to observe a value outside of inference at " + name, RuntimeWarning) return obs return fn(*args, **kwargs) # if stack not empty, apply everything in the stack? else: # initialize data structure to pass up/down the stack msg = { "type": "sample", "name": name, "fn": fn, "is_observed": False, "args": args, "kwargs": kwargs, "value": None, "baseline": baseline, "scale": 1.0, "cond_indep_stack": [], "done": False, "stop": False, } # handle observation if obs is not None: msg["value"] = obs msg["is_observed"] = True # apply the stack and return its return value out_msg = apply_stack(msg) return out_msg["value"]
def sample(name, fn, *args, **kwargs): """ Calls the stochastic function `fn` with additional side-effects depending on `name` and the enclosing context (e.g. an inference algorithm). See `Intro I <http://pyro.ai/examples/intro_part_i.html>`_ and `Intro II <http://pyro.ai/examples/intro_part_ii.html>`_ for a discussion. :param name: name of sample :param fn: distribution class or function :param obs: observed datum (optional; should only be used in context of inference) optionally specified in kwargs :param dict baseline: Optional dictionary of baseline parameters specified in kwargs. See inference documentation for details. :returns: sample """ obs = kwargs.pop("obs", None) baseline = kwargs.pop("baseline", {}) # check if stack is empty # if stack empty, default behavior (defined here) if len(_PYRO_STACK) == 0: if obs is not None: warnings.warn("trying to observe a value outside of inference at " + name, RuntimeWarning) return obs return fn(*args, **kwargs) # if stack not empty, apply everything in the stack? else: # initialize data structure to pass up/down the stack msg = { "type": "sample", "name": name, "fn": fn, "is_observed": False, "args": args, "kwargs": kwargs, "value": None, "baseline": baseline, "scale": 1.0, "cond_indep_stack": [], "done": False, "stop": False, } # handle observation if obs is not None: msg["value"] = obs msg["is_observed"] = True # apply the stack and return its return value out_msg = apply_stack(msg) return out_msg["value"]