def setUp(self): dbn_1 = DynamicBayesianNetwork() dbn_1.add_edges_from( [(('Z', 0), ('X', 0)), (('Z', 0), ('Y', 0)), (('Z', 0), ('Z', 1))]) cpd_start_z_1 = TabularCPD(('Z', 0), 2, [[0.8, 0.2]]) cpd_x_1 = TabularCPD( ('X', 0), 2, [[0.9, 0.6], [0.1, 0.4]], [('Z', 0)], [2]) cpd_y_1 = TabularCPD( ('Y', 0), 2, [[0.7, 0.2], [0.3, 0.8]], [('Z', 0)], [2]) cpd_trans_z_1 = TabularCPD( ('Z', 1), 2, [[0.9, 0.1], [0.1, 0.9]], [('Z', 0)], [2]) dbn_1.add_cpds(cpd_start_z_1, cpd_trans_z_1, cpd_x_1, cpd_y_1) dbn_1.initialize_initial_state() self.dbn_inference_1 = DBNInference(dbn_1) dbn_2 = DynamicBayesianNetwork() dbn_2.add_edges_from([(('Z', 0), ('X', 0)), (('X', 0), ('Y', 0)), (('Z', 0), ('Z', 1))]) cpd_start_z_2 = TabularCPD(('Z', 0), 2, [[0.5, 0.5]]) cpd_x_2 = TabularCPD( ('X', 0), 2, [[0.6, 0.9], [0.4, 0.1]], [('Z', 0)], [2]) cpd_y_2 = TabularCPD( ('Y', 0), 2, [[0.2, 0.3], [0.8, 0.7]], [('X', 0)], [2]) cpd_z_2 = TabularCPD( ('Z', 1), 2, [[0.4, 0.7], [0.6, 0.3]], [('Z', 0)], [2]) dbn_2.add_cpds(cpd_x_2, cpd_y_2, cpd_z_2, cpd_start_z_2) dbn_2.initialize_initial_state() self.dbn_inference_2 = DBNInference(dbn_2)
def buildDBN(): # Construct a DBN object dbn = DBN() #!!!!!!!!!!!!!!! VERY IMPORTANT !!!!!!!!!!!!!!! # MAKE SURE to NAME THE RANDOM VARIABLE "LOCATION AS "L" AND "SENSOR" OBSERVATION AS "O" ########-----YOUR CODE STARTS HERE-----######## dbn.add_edges_from([(('L', 0), ('O', 0)), (('L', 0), ('L', 1))]) l_cpd = TabularCPD(('L', 0), 4, [[0], [0], [1], [0]]) o_cpd = TabularCPD(('O', 0), 4, [ [0.7, 0.1, 0.1, 0.1], [0.1, 0.7, 0.1, 0.1], [0.1, 0.1, 0.7, 0.1], [0.1, 0.1, 0.1, 0.7], ], evidence=[('L', 0)], evidence_card=[4]) # l_i_cpd = TabularCPD(('L', 1), 4, [[.5, .5, 0, 0], # [0, .5, 0, .5], # [.5, 0, .5, 0], # [0, 0, .5, .5],], # evidence=[('L',0)], # evidence_card=[4]) l_i_cpd = TabularCPD(('L', 1), 4, [ [.5, 0, .5, 0], [.5, .5, 0, 0], [0, 0, .5, .5], [0, .5, 0, .5], ], evidence=[('L', 0)], evidence_card=[4]) # print(o_i_cpd) # print(l_cpd) # print(o_cpd) # print(l_i_cpd) dbn.add_cpds(l_cpd, o_cpd, l_i_cpd) ########-----YOUR CODE ENDS HERE-----######## # Do NOT forget to initialize before doing any inference! Otherwise, errors will be introduced. dbn.initialize_initial_state() # Create an inference object dbn_inf = DBNInference(dbn) ########-----YOUR MAY TEST YOUR CODE BELOW -----######## ########-----ADDITIONAL CODE STARTS HERE-----######## #print(dbn_inf.query([('L',3)])[('L',3)].values) # print(dbn_inf.query([('L',1)])[('L',1)].values) #print(dbn_inf.query([('L',1)], {('O', 1):2})[('L',1)].values) ########-----YOUR CODE ENDS HERE-----######## return dbn_inf
def buildDBN(): # Construct a DBN object dbn = DBN() dbn.add_edges_from([(('L', 0), ('O', 0)), (('L', 0), ('L', 1)), (('L', 1), ('O', 1))]) # setup conditional probability tables for nodes in network O0_cpd = TabularCPD(('O', 0), 4, [[0.7, 0.1, 0.1, 0.1], # A [0.1, 0.7, 0.1, 0.1], # B [0.1, 0.1, 0.7, 0.1], # C [0.1, 0.1, 0.1, 0.7]], # D evidence=[('L', 0)], evidence_card=[4]) l0_cpd = TabularCPD(('L', 0), 4, [[0], # A [0], # B [1], # C [0]]) # D l1_cpd = TabularCPD(('L', 1), 4, [[0.5, 0.0, 0.5, 0.0], # A [0.5, 0.5, 0.0, 0.0], # B [0.0, 0.0, 0.5, 0.5], # C [0.0, 0.5, 0.0, 0.5]], # D evidence=[('L', 0)], evidence_card=[4]) #add these conditional probability tables to our BayesianModel dbn.add_cpds(l0_cpd, l1_cpd, O0_cpd) #initialize our model for time series analysis dbn.initialize_initial_state() # Create an inference object to perform queries dbn_inf = DBNInference(dbn) #print(dbn_inf.query(variables=[('L',1)], evidence={('O',1): 2})[('L',1)]) return dbn_inf
class DbnCnnInterface(object): def __init__(self, model_file='../DBN/network.nx'): nx_model = nx.read_gpickle(model_file) self.dbn = DynamicBayesianNetwork(nx_model.edges()) self.dbn.add_cpds(*nx_model.cpds) self.dbn.initialize_initial_state() self.dbn_infer = DBNInference(self.dbn) def filter_q_values(self, q_values, evidence=0, method='binary'): inferred = np.ndarray(shape=(len(q_values), ), dtype=float) inferred.fill(0) variables = self.dbn.get_slice_nodes(1) ev = {node: 0 for node in self.dbn.get_slice_nodes(0)} if evidence != 0: self.set_evidence(ev, evidence) q = self.dbn_infer.query(variables=variables, evidence=ev) for variable in q.values(): action = self.get_action_id(variable.variables[0]) if method == 'binary': inferred[action] = 1 if variable.values[1] > 0 else 0 else: inferred[action] = variable.values[1] return q_values * inferred def get_action_id(self, action): if action[0] == 'Prompt': return 0 elif action[0] == 'Reward': return 1 elif action[0] == 'Abort': return 2 return 3 def set_evidence(self, evidence, id): if id == 1: evidence[("Prompt", 0)] = 1
def __init__(self, model_file='../DBN/network.nx'): nx_model = nx.read_gpickle(model_file) self.dbn = DynamicBayesianNetwork(nx_model.edges()) self.dbn.add_cpds(*nx_model.cpds) self.dbn.initialize_initial_state() self.dbn_infer = DBNInference(self.dbn)
print "Model learned successfully: ", model.check_model() # PRINTS MODEL print model.edges() for cpd in model.get_cpds(): print cpd # DRAWS RESULTING NETWORK nx.drawing.nx_pydot.write_dot(model, "../output/network.dot") os.system('dot ../output/network.dot -Tpng -o ../output/foo.png; gnome-open ../output/foo.png') # OUTPUTS RESULTING NETWORK nx.write_gpickle(model, "../output/network.nx") # TESTS MODEL dbn_infer = DBNInference(model) correct_prob_actions = np.ndarray(shape=(3,), dtype=int) correct_prob_actions.fill(0) correct_binary_actions = np.ndarray(shape=(3,), dtype=int) correct_binary_actions.fill(0) inferred_prob = np.ndarray(shape=(3,), dtype=float) inferred_binary = np.ndarray(shape=(3,), dtype=float) for sample in samples.keys(): inferred_prob.fill(0) inferred_binary.fill(0) values = samples[sample] variables = (('Prompt', 1), ('Reward', 1), ('Abort', 1)) evidence = {('Prompt', 0): 0, ('Reward', 0): 0, ('Abort', 0): 0} if values[IX_PAST_ACTION] == 1: evidence[('Prompt', 0)] = 1 q = dbn_infer.query(variables=variables, evidence=evidence)
class TestDBNInference(unittest.TestCase): def setUp(self): dbn_1 = DynamicBayesianNetwork() dbn_1.add_edges_from( [(('Z', 0), ('X', 0)), (('Z', 0), ('Y', 0)), (('Z', 0), ('Z', 1))]) cpd_start_z_1 = TabularCPD(('Z', 0), 2, [[0.8, 0.2]]) cpd_x_1 = TabularCPD( ('X', 0), 2, [[0.9, 0.6], [0.1, 0.4]], [('Z', 0)], [2]) cpd_y_1 = TabularCPD( ('Y', 0), 2, [[0.7, 0.2], [0.3, 0.8]], [('Z', 0)], [2]) cpd_trans_z_1 = TabularCPD( ('Z', 1), 2, [[0.9, 0.1], [0.1, 0.9]], [('Z', 0)], [2]) dbn_1.add_cpds(cpd_start_z_1, cpd_trans_z_1, cpd_x_1, cpd_y_1) dbn_1.initialize_initial_state() self.dbn_inference_1 = DBNInference(dbn_1) dbn_2 = DynamicBayesianNetwork() dbn_2.add_edges_from([(('Z', 0), ('X', 0)), (('X', 0), ('Y', 0)), (('Z', 0), ('Z', 1))]) cpd_start_z_2 = TabularCPD(('Z', 0), 2, [[0.5, 0.5]]) cpd_x_2 = TabularCPD( ('X', 0), 2, [[0.6, 0.9], [0.4, 0.1]], [('Z', 0)], [2]) cpd_y_2 = TabularCPD( ('Y', 0), 2, [[0.2, 0.3], [0.8, 0.7]], [('X', 0)], [2]) cpd_z_2 = TabularCPD( ('Z', 1), 2, [[0.4, 0.7], [0.6, 0.3]], [('Z', 0)], [2]) dbn_2.add_cpds(cpd_x_2, cpd_y_2, cpd_z_2, cpd_start_z_2) dbn_2.initialize_initial_state() self.dbn_inference_2 = DBNInference(dbn_2) def test_forward_inf_single_variable(self): query_result = self.dbn_inference_1.forward_inference([('X', 0)]) np_test.assert_array_almost_equal(query_result[('X', 0)].values, np.array([0.84, 0.16])) def test_forward_inf_multiple_variable(self): query_result = self.dbn_inference_1.forward_inference( [('X', 0), ('Y', 0)]) np_test.assert_array_almost_equal(query_result[('X', 0)].values, np.array([0.84, 0.16])) np_test.assert_array_almost_equal(query_result[('Y', 0)].values, np.array([0.6, 0.4])) def test_forward_inf_single_variable_with_evidence(self): query_result = self.dbn_inference_1.forward_inference([( 'Z', 1)], {('Y', 0): 0, ('Y', 1): 0}) np_test.assert_array_almost_equal(query_result[('Z', 1)].values, np.array([0.95080214, 0.04919786])) query_result = self.dbn_inference_2.forward_inference([( 'X', 2)], {('Y', 0): 1, ('Y', 1): 0, ('Y', 2): 1}) np_test.assert_array_almost_equal(query_result[('X', 2)].values, np.array([0.76738736, 0.23261264])) def test_forward_inf_multiple_variable_with_evidence(self): query_result = self.dbn_inference_1.forward_inference([('Z', 1), ( 'X', 1)], {('Y', 0): 0, ('Y', 1): 0}) np_test.assert_array_almost_equal(query_result[('Z', 1)].values, np.array([0.95080214, 0.04919786])) np_test.assert_array_almost_equal(query_result[('X', 1)].values, np.array([0.88524064, 0.11475936])) def test_backward_inf_single_variable(self): query_result = self.dbn_inference_2.backward_inference([('Y', 0)]) np_test.assert_array_almost_equal(query_result[('Y', 0)].values, np.array([0.225, 0.775])) def test_backward_inf_multiple_variables(self): query_result = self.dbn_inference_2.backward_inference([('X', 0), ('Y', 0)]) np_test.assert_array_almost_equal(query_result[('X', 0)].values, np.array([0.75, 0.25])) np_test.assert_array_almost_equal(query_result[('Y', 0)].values, np.array([0.225, 0.775])) def test_backward_inf_single_variable_with_evidence(self): query_result = self.dbn_inference_2.backward_inference([( 'X', 0)], {('Y', 0): 0, ('Y', 1): 1, ('Y', 2): 1}) np_test.assert_array_almost_equal(query_result[('X', 0)].values, np.array([0.66594382, 0.33405618])) query_result = self.dbn_inference_1.backward_inference([( 'Z', 1)], {('Y', 0): 0, ('Y', 1): 0, ('Y', 2): 0}) np_test.assert_array_almost_equal(query_result[('Z', 1)].values, np.array([0.98048698, 0.01951302])) def test_backward_inf_multiple_variables_with_evidence(self): query_result = self.dbn_inference_2.backward_inference([('X', 0), ( 'X', 1)], {('Y', 0): 0, ('Y', 1): 1, ('Y', 2): 1}) np_test.assert_array_almost_equal(query_result[('X', 0)].values, np.array([0.66594382, 0.33405618])) np_test.assert_array_almost_equal(query_result[('X', 1)].values, np.array([0.7621772, 0.2378228]))
# goal2 - right post # goal3 - right corner goal_i_cpd = TabularCPD( ('goal', 1), 4, [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]], evidence=[('goal', 0)], evidence_card=[4]) # Add the CPD tables into the DBN dbn.add_cpds(goal_cpd, state_cpd, action_cpd, goal_i_cpd) # Do NOT forget to initialize before doing any inference! Otherwise, errors will be introduced. dbn.initialize_initial_state() # Create an inference object dbn_inf = DBNInference(dbn) # Perform inference on the DBN. result = dbn_inf.query(variables=[('goal', int(time_querying))], evidence={ ('action', int(time_querying)): int(action_id) })[('goal', int(time_querying))].values # out = str(result[0]) + "," + str(result[1]) + "," + str(result[2]) + "," + str(result[3]) largest = 0 largest_val = result[0] if result[1] > largest_val: largest = 1 largest_val = result[1]
ct_cpd = TabularCPD(('C(t)', 1), 2, [[0.9388, 0.82414], [0.0611017, 0.17585]], evidence=[('V(t)', 0)], evidence_card=[2]) Vt_cpd = TabularCPD(('V(t)', 1), 2, [[0.96, 0.1], [0.04, 0.9]], evidence=[('V(t)', 0)], evidence_card=[2]) dbn.add_cpds(Vt_1_cpd, Vt_cpd, st_cpd, et_cpd, ct_cpd) dbn.initialize_initial_state() dbn_inf = DBNInference(dbn) #dbn_inf.query([('X', 0)], {('Y', 0):0, ('Y', 1):1, ('Y', 2):1})[('X', 0)].values #array([ 0.66594382, 0.33405618]) nx.draw(dbn, with_labels=True) plt.show() ''' dbnet = DBN() >>> dbnet.add_edges_from([(('Z', 0), ('X', 0)), (('X', 0), ('Y', 0)), ... (('Z', 0), ('Z', 1))]) >>> z_start_cpd = TabularCPD(('Z', 0), 2, [[0.5, 0.5]]) >>> x_i_cpd = TabularCPD(('X', 0), 2, [[0.6, 0.9], ... [0.4, 0.1]], ... evidence=[('Z', 0)],
model = hc.estimate(start=start, tabu_length=5, max_indegree=2) # model = hc.estimate(tabu_length=5, max_indegree=2) print 'Learning parameters' model.fit(data) # model.fit(data, estimator=BayesianEstimator) model.initialize_initial_state() print "Model learned successfully: ", model.check_model() print model.edges() for cpd in model.get_cpds(): print cpd # DRAWS RESULTING NETWORK nx.drawing.nx_pydot.write_dot(model, "../output/network.dot") dbn_infer = DBNInference(model) t = 0 robot = 0 while True: print 'Robot (prompt=0, fail=1, reward=2): ', robot obs = input('Observation (prompt=0, fail=1, reward=2): ') q = dbn_infer.query(variables=[('A', t)], evidence={('O', t): obs, ('R', t): robot}) action = sample(q[('A', t)].values) print 'Action (resp=0, no resp=1): ', action q = dbn_infer.query(variables=[('R', t + 1)], evidence={('O', t): obs, ('A', t): action, ('R', t): robot}) robot = sample(q[('R', t + 1)].values)
model = hc.estimate(start=start, tabu_length=10, max_indegree=2) # model = hc.estimate(tabu_length=5, max_indegree=2) print 'Learning parameters' model.fit(data) # model.fit(data, estimator=BayesianEstimator) model.initialize_initial_state() print "Model learned successfully: ", model.check_model() print model.edges() for cpd in model.get_cpds(): print cpd # DRAWS RESULTING NETWORK nx.drawing.nx_pydot.write_dot(model, "../output/network.dot") dbn_infer = DBNInference(model) t = 0 prompt = 0 reward = 0 failure = 0 while True: print prompt, reward, failure q = dbn_infer.query(variables=[('P', t + 1), ('R', t + 1), ('A', t + 1)], evidence={ ('P', t): prompt, ('R', t): reward, ('A', t): failure }) print q.values() obs = input('Observation (prompt=0, fail=1, reward=2): ')