def get_output_message(self, variable): """ Returns output messages from this factor to the given variable node. """ om = defaultdict(list) # FIXME: make this faster by using a dictionary variable_index = [v.name for v in self.variables].index(variable.name) selected_variables = [(i, v) for i, v in enumerate(self.variables) if v.name != variable.name] list_of_lists_of_values = [v.get_values() for v in self.variables] for x in itertools.product(*list_of_lists_of_values): log_prob = self.prob_table(*x) for i, v in selected_variables: log_prob += self.input_messages[v.name][v.value_2_index[x[i]]] om[x[variable_index]].append(log_prob) omlp = np.zeros_like(variable.log_probs) for v in om: omlp[variable.value_2_index[v]] = la.sum(om[v]) return omlp
def explain(self, full=False, linear_prob=False): """This function prints the values and their probabilities for this node. """ if full: for value in sorted(self.value_2_index.keys()): p = self.log_probs[self.value_2_index[value]] if linear_prob: p = np.exp(p) print( "Name: %-20s desc:%-10s Value: %-15s Probability: % .17f" % (self.name, self.desc, value, p)) else: for v in self.get_two_most_probable_values(): if linear_prob: p = np.exp(v[1]) print( "Name: %-20s desc:%-10s Value: %-15s Probability: % .17f" % (self.name, self.desc, v[0], v[1])) p = la.sum(self.log_probs) if linear_prob: p = np.exp(p) print(('Cardinality: %.4d' + ' ' * 54 + ' Total: % .17f') % (self.cardinality, p)) print
def get_output_message(self, variable): """ Returns output messages from this factor to the given variable node. """ om = defaultdict(list) # FIXME: make this faster by using a dictionary variable_index = [v.name for v in self.variables].index(variable.name) selected_variables = [(i, v) for i, v in enumerate( self.variables) if v.name != variable.name] list_of_lists_of_values = [v.get_values() for v in self.variables] for x in itertools.product(*list_of_lists_of_values): log_prob = self.prob_table(*x) for i, v in selected_variables: log_prob += self.input_messages[v.name][v.value_2_index[x[i]]] om[x[variable_index]].append(log_prob) omlp = np.zeros_like(variable.log_probs) for v in om: omlp[variable.value_2_index[v]] = la.sum(om[v]) return omlp
def explain(self, full=False, linear_prob=False): """This function prints the values and their probabilities for this node. """ if full: for value in sorted(self.value_2_index.keys()): p = self.log_probs[self.value_2_index[value]] if linear_prob: p = np.exp(p) print("Name: %-20s desc:%-10s Value: %-15s Probability: % .17f" % (self.name, self.desc, value, p)) else: for v in self.get_two_most_probable_values(): if linear_prob: p = np.exp(v[1]) print("Name: %-20s desc:%-10s Value: %-15s Probability: % .17f" % (self.name, self.desc, v[0], v[1])) p = la.sum(self.log_probs) if linear_prob: p = np.exp(p) print(('Cardinality: %.4d' + ' ' * 54 + ' Total: % .17f') % (self.cardinality, p)) print
def normalise(self): """This function normalise the sum of all probabilities to 1.0""" self.log_probs -= la.sum(self.log_probs)