def _get_sample(self, number_samples=1, resample=True, observed=False, input_values={}, differentiable=True): """ Method. Used internally. It returns samples from the random variable and all its parents. Args: number_samples: . A dictionary having the brancher.variables of the model as keys and chainer.Variables as values. This dictionary has to provide values for all variables of the model except for the deterministic variables. resample: Bool. If false it returns the previously sampled values. It avoids that the parents of a variable are sampled multiple times. observed: Bool. It specifies if the sample should be formatted as observed data or Bayesian parameter. input_values: Dictionary(Variable: torch.Tensor). dictionary of values of the parents. It is used for conditioning the sample on the (inputed) values of some of the parents. Returns: Dictionary(Variable: torch.Tensor). A dictionary of samples from the variable and all its parents. """ if self.samples is not None and not resample: return {self: self.samples[-1]} if not observed: if self in input_values: return {self: input_values[self]} else: var_to_sample = self else: if self.has_observed_value: return {self: self._observed_value} elif self.has_random_dataset: var_to_sample = self.dataset else: var_to_sample = self parents_samples_dict = join_dicts_list([ parent._get_sample(number_samples, resample, observed, input_values, differentiable=differentiable) for parent in var_to_sample.parents ]) input_dict = { parent: parents_samples_dict[parent] for parent in var_to_sample.parents } parameters_dict = var_to_sample._apply_link(input_dict) sample = var_to_sample.distribution.get_sample(**parameters_dict) self.samples = [sample] #TODO: to fix output_sample = {**parents_samples_dict, self: sample} return output_sample
def _get_sample(self, number_samples, observed=False, input_values={}, differentiable=True): """ Summary """ joint_sample = join_dicts_list([var._get_sample(number_samples=number_samples, resample=False, observed=observed, input_values=input_values, differentiable=differentiable) for var in self._input_variables]) joint_sample.update(input_values) self.reset() return joint_sample
def get_sample(self, number_samples, observed=False, input_values={}): """ Summary """ sample = input_values sample.update( join_dicts_list([ var._get_sample(number_samples=number_samples, resample=False, observed=observed, input_values=input_values) for var in self.variables ])) self.reset() return sample
def get_sample(self, number_samples=1, resample=True, observed=False, input_values={}): """ Summary """ sample = input_values if self.samples and not resample: return {self: self.samples[-1]} if observed is False: if self in input_values: sample.update( {self: input_values[self]} ) #TODO: This breaks the recursion if an input is provided. The future will decide if this is a feature or a bug! return sample else: var_to_sample = self else: if self.has_observed_value: sample.update({self: self._observed_value}) return sample elif self.has_random_dataset: var_to_sample = self.dataset else: var_to_sample = self parents_samples_dict = join_dicts_list([ parent._get_sample(number_samples, resample, observed, input_values) for parent in var_to_sample.parents ]) input_dict = { parent: parents_samples_dict[parent] for parent in var_to_sample.parents } parameters_dict = var_to_sample.apply_link(input_dict) variable_sample = var_to_sample.distribution._get_sample( **parameters_dict, number_samples=number_samples) self.samples.append(sample) sample.update({**parents_samples_dict, self: variable_sample}) return sample