Пример #1
0
 def _check_user_params(self, **kwargs):
     params = self._user_params = self.user_params
     formula = kwargs.pop("f")
     if params is None:
         return False
     if formula is not None:
         raise opvi.ParametrizationError(
             "No formula is allowed if user params are provided")
     if not isinstance(params, dict):
         raise TypeError("params should be a dict")
     if not all(isinstance(k, int) for k in params.keys()):
         raise TypeError("params should be a dict with `int` keys")
     needed = set(range(len(params)))
     givens = set(params.keys())
     if givens != needed:
         raise opvi.ParametrizationError(
             "Passed parameters do not have a needed set of keys, "
             "they should be equal, needed {needed}, got {givens}".format(
                 givens=list(sorted(givens)),
                 needed="[0, 1, ..., %d]" % len(formula.flows)))
     for i in needed:
         flow = flows.flow_for_params(params[i])
         flow_keys = set(flow.__param_spec__)
         user_keys = set(params[i].keys())
         if flow_keys != user_keys:
             raise opvi.ParametrizationError(
                 "Passed parameters for flow `{i}` ({cls}) do not have a needed set of keys, "
                 "they should be equal, needed {needed}, got {givens}".
                 format(givens=user_keys,
                        needed=flow_keys,
                        i=i,
                        cls=flow.__name__))
     return True
Пример #2
0
    def create_shared_params(self,
                             trace=None,
                             size=None,
                             jitter=1,
                             start=None):
        if trace is None:
            if size is None:
                raise opvi.ParametrizationError(
                    "Need `trace` or `size` to initialize")
            else:
                if start is None:
                    start = self.model.initial_point
                else:
                    start_ = self.model.initial_point.copy()
                    self.model.update_start_vals(start_, start)
                    start = start_
                start = pm.floatX(DictToArrayBijection.map(start))
                # Initialize particles
                histogram = np.tile(start, (size, 1))
                histogram += pm.floatX(
                    np.random.normal(0, jitter, histogram.shape))

        else:
            histogram = np.empty((len(trace) * len(trace.chains), self.ddim))
            i = 0
            for t in trace.chains:
                for j in range(len(trace)):
                    histogram[i] = DictToArrayBijection.map(trace.point(j, t))
                    i += 1
        return dict(histogram=aesara.shared(pm.floatX(histogram), "histogram"))
Пример #3
0
 def __init__(self, op, tf):
     if not isinstance(op, KSD):
         raise opvi.ParametrizationError("Op should be KSD")
     ObjectiveFunction.__init__(self, op, tf)