def __init__(self, name, learnable, ranges, is_observed=False, **kwargs): #TODO: code duplication here self.name = name self._evaluated = False self._observed = is_observed self._observed_value = None self._current_value = None self.construct_deterministic_parents(learnable, ranges, kwargs) self.parents = join_sets_list( [var2link(x).vars for x in kwargs.values()]) self.ancestors = join_sets_list( [self.parents] + [parent.ancestors for parent in self.parents]) self.link = LinkConstructor(**kwargs) self.samples = None self.ranges = {} self.dataset = None self.has_random_dataset = False self.has_observed_value = False self.is_normalized = True self.partial_links = { name: var2link(link) for name, link in kwargs.items() }
def __init__(self, name, learnable, ranges, is_observed=False, has_bias=False, **kwargs): self._input = kwargs.copy() self.name = name self.learnable = learnable self._bias = has_bias self._evaluated = False self._observed = is_observed self._observed_value = None self._current_value = None if self._check_for_stochastic_process_arguments(): return if has_bias: self.construct_biases(learnable, ranges, kwargs) self.construct_deterministic_parents(learnable, ranges, kwargs) self.parents = join_sets_list( [var2link(x).vars for x in kwargs.values()]) self.link = Link(**kwargs) self.ancestors = join_sets_list( [self.parents] + [parent.ancestors for parent in self.parents]) self.samples = None self.ranges = {} self.dataset = None self.has_random_dataset = False self.has_observed_value = False self.is_normalized = True self.partial_links = { name: var2link(link) for name, link in kwargs.items() }
def __init__(self, name, learnable, ranges, is_observed=False, has_bias=False, is_policy=False, is_reward=False, **kwargs): self._input = kwargs self.name = name self._evaluated = False self._observed = is_observed self._observed_value = None self._current_value = None #if self._check_for_stochastic_process_arguments(): # return if has_bias: self.construct_biases(learnable, ranges, kwargs) self.construct_deterministic_parents(learnable, ranges, kwargs) self.parents = join_sets_list([var2link(x).vars for x in kwargs.values()]) self.link = Link(**kwargs) self.ancestors = join_sets_list([self.parents] + [parent.ancestors for parent in self.parents]) self.samples = None self.ranges = {} self.dataset = None self.has_random_dataset = False self.has_observed_value = False self.is_normalized = True self.silenced = False self.partial_links = {name: var2link(link) for name, link in kwargs.items()} # Reinforcement learning descriptors if not (is_policy and is_reward): self._policy = is_policy self._reward = is_reward else: raise ValueError("A variable cannot be both a RL policy and a RL reward")
def __init__(self, name, learnable, ranges, is_observed=False, **kwargs): class VarLink(chainer.ChainList): def __init__(self): self.kwargs = kwargs links = [ link for partial_link in kwargs.values() for link in var2link(partial_link).links ] super().__init__(*links) def __call__(self, values): return { k: var2link(x).fn(values) for k, x in self.kwargs.items() } self.name = name self._evaluated = False self._observed = is_observed self._observed_value = None self._current_value = None self.construct_deterministic_parents(learnable, ranges, kwargs) self.parents = join_sets_list( [var2link(x).vars for x in kwargs.values()]) self.link = VarLink() self.samples = [] self.ranges = {} self.dataset = None self.has_random_dataset = False self.has_observed_value = False
def var2link(var): if isinstance(var, Variable): vars = {var} fn = lambda values: values[var] elif isinstance(var, (numbers.Number, np.ndarray, torch.Tensor)): vars = set() fn = lambda values: var elif isinstance(var, (tuple, list)) and all([isinstance(v, (Variable, PartialLink)) for v in var]): vars = join_sets_list([{v} if isinstance(v, Variable) else v.vars for v in var]) fn = lambda values: tuple([values[v] if isinstance(v, Variable) else v.fn(values) for v in var]) else: return var return PartialLink(vars=vars, fn=fn, links=set(), string=str(var))
def _flatten(self): variables = list( join_sets_list( [var.ancestors.union({var}) for var in self.variables])) return sorted(variables, key=lambda v: v.name)