def kb_acecard(self, state, move): # type: (State, move) -> bool # each time we check for consistency we initialise a new knowledge-base kb = KB() # Add general information about the game load.general_information(kb) # Add the necessary knowledge about the strategy load.strategy_knowledge(kb) # This line stores the index of the card in the deck. # If this doesn't make sense, refer to _deck.py for the card index mapping index = move[0] # This creates the string which is used to make the strategy_variable. # Note that as far as kb.py is concerned, two objects created with the same # string in the constructor are equivalent, and are seen as the same symbol. # Here we use "pj" to indicate that the card with index "index" should be played with the # PlayJack heuristics that was defined in class. Initialise a different variable if # you want to apply a different strategy (that you will have to define in load.py) variable_string = "pa" + str(index) strategy_variable = Boolean(variable_string) # Add the relevant clause to the loaded knowledge base kb.add_clause(~strategy_variable) # If the knowledge base is not satisfiable, the strategy variable is # entailed (proof by refutation) return kb.satisfiable()
def kb_trump(self, state, move): # type: (State,move) -> bool kb = KB() load.general_information(kb) load.strategy_knowledge(kb) p_card = move[0] p_card_suit = "" if p_card < 5: p_card_suit = "C" elif p_card < 10: p_card_suit = "D" elif p_card < 15: p_card_suit = "H" elif p_card < 20: p_card_suit = "S" # p_card_suit = Deck.get_suit(p_card) trump_suit = state.get_trump_suit() variable_string = "wtt" + str(p_card_suit) + str(trump_suit) strategy_variable = Boolean(variable_string) kb.add_clause(~strategy_variable) return kb.satisfiable()
def kb_consistent_trumpmarriage(self, state, move): # type: (State, move) -> bool # each time we check for consistency we initialise a new knowledge-base kb = KB() # Add general information about the game suit = State.get_trump_suit(state) if suit == "C": card1 = 2 card2 = 3 variable_string = "m" + str(card1) + str(card2) strategy_variable = Boolean(variable_string) kb.add_clause(strategy_variable) elif suit == "D": card1 = 7 card2 = 8 variable_string = "m" + str(card1) + str(card2) strategy_variable = Boolean(variable_string) kb.add_clause(strategy_variable) elif suit == "H": card1 = 12 card2 = 13 variable_string = "m" + str(card1) + str(card2) strategy_variable = Boolean(variable_string) kb.add_clause(strategy_variable) elif suit == "S": card1 = 17 card2 = 18 variable_string = "m" + str(card1) + str(card2) strategy_variable = Boolean(variable_string) kb.add_clause(strategy_variable) load.general_information(kb) # Add the necessary knowledge about the strategy load.strategy_knowledge(kb) # This line stores the index of the card in the deck. # If this doesn't make sense, refer to _deck.py for the card index mapping index = move[0] variable_string = "pm" + str(index) strategy_variable = Boolean(variable_string) # Add the relevant clause to the loaded knowledge base kb.add_clause(~strategy_variable) # If the knowledge base is not satisfiable, the strategy variable is # entailed (proof by refutation) return kb.satisfiable()
def __init__(self): """ Configuration """ # Camera settings self.FRAME_WIDTH = 341 self.FRAME_HEIGHT = 256 self.flip_camera = True # Mirror image self.camera = cv2.VideoCapture(1) # ...you can also use a test video for input #video = "/Users/matthiasendler/Code/snippets/python/tracker/final/assets/test_video/10.mov" #self.camera = cv2.VideoCapture(video) #self.skip_input(400) # Skip to an interesting part of the video if not self.camera.isOpened(): print "couldn't load webcam" return #self.camera.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, self.FRAME_WIDTH) #self.camera.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, self.FRAME_HEIGHT) self.filters_dir = "filters/" # Filter settings in trackbar self.filters_file = "filters_default" # Load filter settings current_config = self.filters_dir + self.filters_file self.filters = Filters(current_config) # No actions will be triggered in test mode # (can be used to adjust settings at runtime) self.test_mode = False # Create a hand detector # In fact, this is a wrapper for many detectors # to increase detection confidence self.detector = Detector(self.filters.config) # Knowledge base for all detectors self.kb = KB() # Create gesture recognizer. # A gesture consists of a motion and a hand state. self.gesture = Gesture() # The action module executes keyboard and mouse commands self.action = Action() # Show output of detectors self.output = Output() self.run()
def prepareKB(self, state): readyKB = KB() for card in state.get_perspective(self): index = -1 if card == "P1H" or card == "P2H" or card == "P1W" or card == "P2W": index = state.get_perspective(self).index(card) if index != -1: tempString = self.__RANKS[index % 5] tempString += str(index % 5) readyKB.add_clause(Boolean(tempString)) return readyKB
def kb_consistent_marriage(self, state, move): # type: (State, move) -> bool kb = KB() load.general_information(kb) load.strategy_knowledge(kb) card1 = move[0] card2 = move[1] variable_string = "m" + str(card1) + str(card2) strategy_variable = Boolean(variable_string) kb.add_clause(~strategy_variable) return kb.satisfiable()
def kb_consistent(self, state, depth, alpha, beta): # type: (State, int, float, float) -> bool """ Check whether the current state contradicts our knowledge. (The knowledge base describes what properties a state should satisfy to be explored) """ v = Integer("v") # the heuristic value when the maximu m depth is reached a = Integer("a") # alpha b = Integer("b") # beta m = Integer("m") # how many ships player one will have (when the heuristic is computed) h = Integer("h") # how many ships player two will have kb = KB() # Add clauses ???
def kb_consistent_low_non_trump(self, state, move): # type: (State, move) -> bool kb = KB() load.general_information(kb) load.strategy_knowledge(kb) card = move[0] trump_suit = state.get_trump_suit() variable_string = "pc" + str(card) + str(trump_suit) strategy_variable = Boolean(variable_string) kb.add_clause(~strategy_variable) return kb.satisfiable()
def generic_test(self,test_num): base_addr = os.getcwd() input_addr = base_addr + '/test' + str(test_num) + '.input.txt' test_addr = base_addr + '/test' + str(test_num) + '.output.txt' resp_addr = base_addr + '/test' + str(test_num) + '.responses.txt' kb = KB(resp_addr) output_file = Shell(kb).start(input_addr,None,None,None,False,True) output_file.seek(0) with open(test_addr,'r') as t: student_output = output_file.readlines() test_output = t.readlines() print("testing") print(test_num) for (line1,line2) in zip(student_output,test_output): self.assertEqual(line1,line2) output_file.close()
def load_fb15k(dir, with_text=True, split_text=False, max_vocab=-1): train_file = os.path.join(dir, "train.txt") test_file = os.path.join(dir, "test.txt") text_file = os.path.join(dir, "text_emnlp.txt") valid_file = os.path.join(dir, "valid.txt") kb = KB() _load_triples(train_file, kb) _load_triples(valid_file, kb, typ="valid") _load_triples(test_file, kb, typ="test") if with_text: if split_text: _load_dep_paths(text_file, kb, typ="train_text") else: _load_triples(text_file, kb, typ="train_text") return kb
def kb_consistent_matching_win(self, state, move): # type: (State,move) -> bool kb = KB() load_justabout.general_information(kb) load_justabout.strategy_knowledge(kb) opp_card = state.get_opponents_played_card() opp_card_suit = Deck.get_suit(opp_card) opp_card_rank = opp_card % 5 p_card = move[0] p_card_suit = Deck.get_suit(p_card) p_card_rank = opp_card % 5 variable_string = "wt" + str(p_card_rank) + str(opp_card_rank) + str(p_card_suit) + str(opp_card_suit) strategy_variable = Boolean(variable_string) kb.add_clause(~strategy_variable) return kb.satisfiable()
def kb_consistent_trump_win(self,state,move): # type: (State,move) -> bool kb = KB() load_justabout.general_information(kb) load_justabout.strategy_knowledge(kb) opp_card = state.get_opponents_played_card() opp_card_suit = Deck.get_suit(opp_card) opp_card_rank = opp_card & 5 p_card = move[0] p_card_suit = Deck.get_suit(p_card) p_card_rank = p_card % 5 trump_suit = state.get_trump_suit() constraint_a = Integer('me') > Integer('op') constraint_b = Integer('op') > Integer('me') if opp_card_suit == trump_suit: if p_card_suit == trump_suit: if opp_card_rank < p_card_rank: strategy_variable = constraint_b else: strategy_variable = constraint_a else: strategy_variable = constraint_b else: variable_string = "wtt" + str(p_card_suit) + str(trump_suit) strategy_variable = Boolean(variable_string) kb.add_clause(~strategy_variable) return kb.satisfiable()
def __init__(self): # Access knowledge base to check hand state self.kb = KB() # Platform independent mouse support #self.m = PyMouse() # Remember which keys are pressed # in order to release them after the gesture self.pressed_keys = [] # Remember the first hand position for smoother mouse movement self.reference_point = None # Only move mouse if we recognize significant hand movement self.movement_threshold = 30 # If no action has been triggered for a long time, # the internal state becomes invalid. self.reset_duration = 5 self.last_command_time = time.time() self.KeyUp = 126 self.KeyDown = 125 self.KeyLeft = 123 self.KeyRight = 124 self.k = PyKeyboard()
import sys from kb import KB, Boolean, Integer, Constant # Define our symbols Q = Boolean('Q') P = Boolean('P') R = Boolean('R') # Create a new knowledge base kb = KB() # Add clauses kb.add_clause(P, Q) kb.add_clause(~Q, R) kb.add_clause(~R, ~P) kb.add_clause(Q, ~P) kb.add_clause(P, ~Q) # Print all models of the knowledge base for model in kb.models(): print(model) # Print out whether the KB is satisfiable (if there are no models, it is not satisfiable) print(kb.satisfiable())
import sys from kb import KB kb1 = KB() with open(sys.argv[1], encoding='utf-8') as file: code = file.read().replace(r'\n', '') kb1.load(code) kb1.forwardChaining()
def kb_consistent(self, state, move): # type: (State, move) -> bool # each time we check for consistency we initialise a new knowledge-base kb = KB() # Add general information about the game load.general_information(kb) # Add the necessary knowledge about the strategy load.strategy_knowledge(kb) # This line stores the index of the card in the deck. # If this doesn't make sense, refer to _deck.py for the card index mapping index = move[0] # This creates the string which is used to make the strategy_variable. # Note that as far as kb.py is concerned, two objects created with the same # string in the constructor are equivalent, and are seen as the same symbol. # Here we use "pj" to indicate that the card with index "index" should be played with the # PlayJack heuristics that was defined in class. Initialise a different variable if # you want to apply a different strategy (that you will have to define in load.py) # # Always play trump trump_suit = state.get_trump_suit() trump = trump_suit.lower() if trump == 'c': pj = Boolean('pj4') pq = Boolean('pq3') pk = Boolean('pk2') pt = Boolean('pt1') pa = Boolean('pa0') if trump == 'd': pj = Boolean('pj9') pq = Boolean('pq8') pk = Boolean('pk7') pt = Boolean('pt6') pa = Boolean('pa5') if trump == 'h': pj = Boolean('pj14') pq = Boolean('pq13') pk = Boolean('pk12') pt = Boolean('pt11') pa = Boolean('pa10') if trump == 's': pj = Boolean('pj19') pq = Boolean('pq18') pk = Boolean('pk17') pt = Boolean('pt16') pa = Boolean('pa15') kb.add_clause(~pj, ~pq, ~pk, ~pt, ~pa) # # always play Jack # variable_string = "pj" + str(index) # strategy_variable = Boolean(variable_string) # Add the relevant clause to the loaded knowledge base # kb.add_clause(~strategy_variable) # If the knowledge base is not satisfiable, the strategy variable is # entailed (proof by refutation) return kb.satisfiable()
'-p', type=str, default='', help='file name for loading a saved knowledge base') parser.add_argument('--save_kb', '-s', type=str, default='', help='file name to save the knowledge base to') parser.add_argument('--responses', '-r', type=str, default='', help='file name for stored agent responses') parser.add_argument('--output', '-o', type=str, default='', help='file name for output') args = parser.parse_args() kb = KB(args.responses) print( 'Input looks like:\n\tA(n) <word1> is a(n) <word2>.\n\tA(n) <word1> is not a(n) <word2>.\n' + '\t<proper noun> is a <word1>.\n\t<proper noun> is not a <word1>.\n\t' + 'Is a(n) <word1> a(n) <word2>?\n\tIs <proper noun> a <word1>?\n\tIs a <word1> <proper noun>?' ) print('Type \'quit\' or \'q\' to exit.\n') Shell(kb).start(args.input, args.prior_kb, args.save_kb, args.output)
def init(self, kb_host="localhost", kb_port=6969, embeddedkb=False, defaultontology=None, data_path=None): if not data_path: # try to guess the current prefix and then the data directory data_path = os.path.abspath(__file__).split('lib')[0].split( 'src')[0] + 'share/dialogs/' logger.debug("Assuming Dialogs data dir is <%s>" % data_path) try: from kb import KB, KbError except ImportError: logger.error("Python bindings to access the knowledge are not available." + \ "Please install 'pykb' and restart Dialogs.") try: self.ontology_server = KB(kb_host, kb_port, embeddedkb, defaultontology) except KbError: logger.error("Error while trying to connect to the knowledge base on %s:%s" % (kb_host, kb_port) + \ ". Continuing without knowledge base. Amongst others, resolution won't work.") self.ontology_server = DummyKnowledgeBase() for line in open(os.path.join(data_path, "adjectives")): if line.startswith("#") or not line.strip(): continue try: adj, cat = line.split() except ValueError: # for adjectives without category, set a generic "Feature" category adj = line.split()[0] cat = "Feature" self.adjectives[adj] = cat verbs = [ list(line.split()) for line in open(os.path.join(data_path, "verbs")) ] verbs = self.split_list(verbs) self.irregular_verbs_past = verbs[0] self.irregular_verbs_present = verbs[1] self.preposition_verbs = verbs[2] self.modal = [k[0] for k in verbs[3]] self.adjective_verb = [k[0] for k in verbs[4]] self.auxiliary = [k[0] for k in verbs[5]] self.direct_transitive = [k[0] for k in verbs[6]] self.indirect_transitive = [k[0] for k in verbs[7]] self.state = [k[0] for k in verbs[8]] self.verb_need_to = [k[0] for k in verbs[9]] self.special_verbs = [k[0] for k in verbs[12]] # Action verbs such as 'see', 'hear' with no active behaviour self.action_verb_with_passive_behaviour = dict([(k[0], k[1]) for k in verbs[10]]) self.goal_verbs = [k[0] for k in verbs[11]] self.sentence_starts = [ tuple(line.split()) for line in open(os.path.join(data_path, "sentence_starts")) ] nouns = [ list(line.split()) for line in open(os.path.join(data_path, "nouns")) ] nouns = self.split_list(nouns) self.special_nouns = [k[0] for k in nouns[0]] self.pronouns = [k[0] for k in nouns[1]] for i in nouns[1]: if i[1] == '1': self.complement_pronouns = self.complement_pronouns + [i[0]] self.demonstrative_det = [k[0] for k in nouns[2]] self.determinants = [k[0] for k in nouns[3]] self.nouns_end_s = [k[0] for k in nouns[4]] self.relatives = [k[0] for k in nouns[5]] self.composed_nouns = [k[0] for k in nouns[6]] self.plural_nouns = nouns[7] self.noun_not_composed = [k[0] for k in nouns[8]] self.days_list = nouns[9] self.months_list = nouns[10] self.unusable_words = [k[0] for k in nouns[11]] # List of diection words, E.g: LEFT, RIGHT, TOP, etc ... self.direction_words = [k[0] for k in nouns[12]] self.compound_nouns = nouns[13] ### ### ADVERBIALS adverbials = [ list(line.split()) for line in open(os.path.join(data_path, "adverbial")) ] adverbials = self.split_list(adverbials) self.adverbs = [k[0] for k in adverbials[0]] self.time_adverbs = adverbials[0] self.time_adverbs = [ k[0] for k in adverbials[0] if k[1] in ["day", "hour"] ] self.time_adverbs += [ k[0] for k in adverbials[1] if k[1] in ["day", "hour"] ] self.location_adverbs = [ k[0] for k in adverbials[0] if k[1] == "location" ] self.location_adverbs += [ k[0] for k in adverbials[1] if k[1] == "location" ] self.adverbs_at_end = [k[0] for k in adverbials[1]] for k in adverbials[2]: if k[1] == '1': self.compelement_proposals = self.compelement_proposals + [ k[0] ] self.proposals = [k[0] for k in adverbials[2]] #Preposition with an existing object_property # E.g: next+to => isNextTo self.preposition_rdf_object_property = dict([(k[0], k[3:]) for k in adverbials[2]]) self.time_proposals = adverbials[2] self.subsentences = [k[0] for k in adverbials[3]] for k in adverbials[3]: if k[1] == '1': self.adv_sub = self.adv_sub + [k[0]] self.prep_change_place = [k[0] for k in adverbials[4]] grammatical_rules = [ list(line.split()) for line in open(os.path.join(data_path, "grammatical_rules")) ] grammatical_rules = self.split_list(grammatical_rules) self.numbers = grammatical_rules[0] self.det_quantifiers = grammatical_rules[1] self.capital_letters = [k[0] for k in grammatical_rules[2]] self.adjective_rules = [k[0] for k in grammatical_rules[3]] self.concatenate_proposals = grammatical_rules[4] self.change_tuples = grammatical_rules[5] self.adjective_numbers_digit = grammatical_rules[6] self.adjective_numbers = [k[0] for k in grammatical_rules[6]] self.be_pronoun = [k[0] for k in grammatical_rules[7]] self.adj_quantifiers = [k[0] for k in grammatical_rules[8]] for k in grammatical_rules[9]: self.replace_tuples = self.replace_tuples + [[k[0], k[1:]]] desc = "" for line in open(os.path.join(data_path, "thematic_roles")): if line.startswith("#") or not line.strip(): continue desc += line if line.startswith("}"): #end of block self.thematic_roles.add_verb(desc) desc = "" #Add action verbs to the ontology if self.ontology_server: stmts = [ verb.capitalize() + " rdfs:subClassOf cyc:PurposefulAction" for verb in self.thematic_roles.verbs.keys() if not self.thematic_roles.verbs[verb].is_synonym() ] self.ontology_server.revise(stmts, {"method": "add"}) """ List of ontology classes that are used in the adjectives list """ self.adjectives_ontology_classes = [ self.adjectives[adj].lower() for adj in self.adjectives ] adj_s = [] for k in self.adjectives_ontology_classes: if not k in adj_s: adj_s.append(k) self.adjectives_ontology_classes = adj_s
def __init__(self, num_relations, num_entities, arities=[0.0, 1.0, 0.0], fb_densities=[0.0, 0.0, 0.0], arg_densities=[0., 0.1, 0.0], fact_prob=0.2, num_symm=2, num_impl=[0, 2, 0], num_impl_inv=2, num_impl_conj=[0, 2, 0], num_trans_single=2, num_trans_diff=2, seed=0, position_dependent_args=False, position_densities=[0., 0.5, 0.0]): """ :param num_relations: :param num_entities: number of distinct entities to generate :param arities: fraction of arities :param arg_densities: fraction of entity combinations that are observed :param fact_prob: :param num_inv: number of 'inv' formulae R(X0, X1) :- R(X1, X0) :param num_impl: :param num_impl_conj: :param num_trans: :param negated_head_prob: :param seed: :return: """ random.seed(seed) self.kb = KB(seed=seed) num_relations_per_arity = [int(x * num_relations) for x in arities] entities = list(map(lambda x: "e" + str(x), range(1, num_entities+1))) entities_arg1 = [] entities_arg2 = [] entities_arg3 = [] if position_dependent_args: arg1_boundary = int(len(entities)*position_densities[0]) arg2_boundary = arg1_boundary + int(len(entities)*position_densities[1]) entities_arg1 = entities[0:arg1_boundary] entities_arg2 = entities[arg1_boundary:arg2_boundary] entities_arg3 = entities[arg2_boundary:] else: entities_arg1 = entities entities_arg2 = entities entities_arg3 = entities pairs = [(x, y) for x in entities_arg1 for y in entities_arg2 if not x == y] triples = [(x, y, z) for x in entities_arg1 for y in entities_arg2 for z in entities_arg3 if not x == y and not y == z and not z == x] num_pair_samples = min(len(pairs), int(len(entities_arg1) * len(entities_arg2) * arg_densities[1])) num_triple_samples = min(len(triples), int(len(entities_arg1) * len(entities_arg2) * len(entities_arg3) * arg_densities[2])) entities_per_arity = { 1: entities_arg1, 2: random.sample(pairs, num_pair_samples), 3: random.sample(triples, num_triple_samples) } relations_per_arity = {} for arity in range(1, len(num_relations_per_arity) + 1): for i in range(1, num_relations_per_arity[arity - 1] + 1): fb_prefix = "" if fb_densities[arity-1] > random.uniform(0, 1.0): fb_prefix = "REL$" if arity == 1: rel = fb_prefix+"u" elif arity == 2: rel = fb_prefix+"b" else: rel = fb_prefix+"t" rel += str(i) if not arity in relations_per_arity: relations_per_arity[arity] = list() relations_per_arity[arity].append(rel) for args in random.sample(entities_per_arity[arity], int(len(entities_per_arity[arity]) * fact_prob)): self.kb.add_train(rel, args) inverse = [] # sample symmetric relations r(X,Y) => r(Y,X) if 2 in relations_per_arity: symm = random.sample([(x, x) for x in relations_per_arity[2]], num_symm) inverse += symm # sampling implication, reversed: r1(X,Y) => r2(Y,X) if 2 in relations_per_arity: inverse += random.sample([(x, y) for x in relations_per_arity[2] for y in relations_per_arity[2] if not x == y], num_impl_inv) if len(inverse) > 0: self.kb.add_formulae("inv", {2: inverse}) # sampling implications: # r1(X) => r2(X) # r1(X,Y) => r2(X,Y) implications_per_arity = {} for arity in range(1, len(num_relations_per_arity) + 1): if arity in relations_per_arity: implications_per_arity[arity] = \ random.sample([(x, y) for x in relations_per_arity[arity] for y in relations_per_arity[arity] if not x == y], num_impl[arity - 1]) self.kb.add_formulae("impl", implications_per_arity) # sampling implications with conjunction in body: # r1(X,Y) ^ r2(X,Y) => r3(X,Y) # r1(X) ^ r2(X) => r3(X) implications_with_conjunction_per_arity = {} for arity in range(1, len(num_relations_per_arity) + 1): if arity in relations_per_arity and len(relations_per_arity[arity]) >= 3: implications_with_conjunction_per_arity[arity] = \ random.sample([(x, y, z) for x in relations_per_arity[arity] for y in relations_per_arity[arity] for z in relations_per_arity[arity] if not x == y and not y == z and not z == x], num_impl_conj[arity - 1]) self.kb.add_formulae("impl_conj", implications_with_conjunction_per_arity) # sampling transitivities: transitivities = [] # (1) simple transitivities r(X,Y) ^ r(Y,Z) => r(X,Z) # (2) general transitivities r1(X,Y) ^ r2(Y,Z) => r3(X,Z) (r1, r2, r3 differ) if 2 in relations_per_arity: if num_trans_single > 0: transitivities += random.sample([(x, x, x) for x in relations_per_arity[2]], num_trans_single) if num_trans_diff > 0: transitivities += random.sample([(x, y, z) for x in relations_per_arity[2] for y in relations_per_arity[2] for z in relations_per_arity[2] if not x == y and not y == z and not z == x], num_trans_diff) if len(transitivities) > 0: self.kb.add_formulae("trans", {2: transitivities})
def main(args): if (not args.do_train) and (not args.do_valid) and (not args.do_test): raise ValueError('one of train/val/test mode must be choosed.') if args.init_checkpoint: override_config(args) elif args.data_path is None: raise ValueError('one of init_checkpoint/data_path must be choosed.') if args.do_train and args.save_path is None: raise ValueError('Where do you want to save your trained model?') if args.save_path and not os.path.exists(args.save_path): os.makedirs(args.save_path) # Write logs to checkpoint and console set_logger(args) with open(os.path.join(args.data_path, 'entities.dict')) as fin: entity2id = dict() id2entity = dict() for line in fin: eid, entity = line.strip().split('\t') entity2id[entity] = int(eid) id2entity[int(eid)] = entity with open(os.path.join(args.data_path, 'relations.dict')) as fin: relation2id = dict() id2relationship = dict() for line in fin: rid, relation = line.strip().split('\t') relation2id[relation] = int(rid) id2relationship[int(rid)] = relation # Read regions for Countries S* datasets if args.countries: regions = list() with open(os.path.join(args.data_path, 'regions.list')) as fin: for line in fin: region = line.strip() regions.append(entity2id[region]) args.regions = regions e_vocab = Dictionary(tok2ind=entity2id, ind2tok=id2entity) r_vocab = Dictionary(tok2ind=relation2id, ind2tok=id2relationship) ## TODO: add graph file graph = KB(os.path.join(args.data_path, 'train.txt'), e_vocab=e_vocab, r_vocab=r_vocab) nentity = len(entity2id) nrelation = len(relation2id) args.nentity = nentity args.nrelation = nrelation logging.info('Model: %s' % args.model) logging.info('Data Path: %s' % args.data_path) logging.info('#entity: %d' % nentity) logging.info('#relation: %d' % nrelation) train_triples = read_triple(os.path.join(args.data_path, 'train.txt'), entity2id, relation2id) logging.info('#train: %d' % len(train_triples)) valid_triples = read_triple(os.path.join(args.data_path, 'valid.txt'), entity2id, relation2id) logging.info('#valid: %d' % len(valid_triples)) test_triples = read_triple(os.path.join(args.data_path, 'test.txt'), entity2id, relation2id) logging.info('#test: %d' % len(test_triples)) candidate_entities = None if args.rerank_minerva: candidate_entities = defaultdict(set) with open( "/home/shdhulia//Limits-of-Path-Reasoner/outputs/FB15K-237/thisone_test/all_answers.txt" ) as candidate_file: # with open("/home/shdhulia/minerva_answers/fb.txt") as candidate_file: for line in candidate_file: pt = line.strip().split("\t") e1 = entity2id[pt[0]] r = relation2id[pt[1]] predicted_es = set( [entity2id[p] for p in pt[2:] if p in entity2id]) candidate_entities[(e1, r)] = set(predicted_es) #All true triples all_true_triples = train_triples + valid_triples + test_triples kge_model = KGEModel( model_name=args.model, nentity=nentity, nrelation=nrelation, hidden_dim=args.hidden_dim, gamma=args.gamma, double_entity_embedding=args.double_entity_embedding, double_relation_embedding=args.double_relation_embedding) logging.info('Model Parameter Configuration:') for name, param in kge_model.named_parameters(): logging.info('Parameter %s: %s, require_grad = %s' % (name, str(param.size()), str(param.requires_grad))) if args.cuda: kge_model = kge_model.cuda() if args.do_train: # Set training dataloader iterator # train_dataloader_head = DataLoader( # TrainDataset(train_triples, nentity, nrelation, args.negative_sample_size, 'head-batch'), # batch_size=args.batch_size, # shuffle=True, # num_workers=max(1, args.cpu_num//2), # collate_fn=TrainDataset.collate_fn # ) train_dataloader_tail = DataLoader( TrainDataset(train_triples, nentity, nrelation, args.negative_sample_size, 'tail-batch', KB=graph), batch_size=args.batch_size, shuffle=True, num_workers=max(1, args.cpu_num // 2), collate_fn=TrainDataset.collate_fn) # train_iterator = BidirectionalOneShotIterator(train_dataloader_head, train_dataloader_tail) train_iterator = OneShotIterator(train_dataloader_tail) # Set training configuration current_learning_rate = args.learning_rate optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad, kge_model.parameters()), lr=current_learning_rate) if args.warm_up_steps: warm_up_steps = args.warm_up_steps else: warm_up_steps = args.max_steps // 2 if args.init_checkpoint: # Restore model from checkpoint directory logging.info('Loading checkpoint %s...' % args.init_checkpoint) checkpoint = torch.load( os.path.join(args.init_checkpoint, 'checkpoint')) init_step = checkpoint['step'] kge_model.load_state_dict(checkpoint['model_state_dict']) if args.do_train: current_learning_rate = checkpoint['current_learning_rate'] warm_up_steps = checkpoint['warm_up_steps'] optimizer.load_state_dict(checkpoint['optimizer_state_dict']) else: logging.info('Ramdomly Initializing %s Model...' % args.model) init_step = 0 step = init_step logging.info('Start Training...') logging.info('init_step = %d' % init_step) logging.info('batch_size = %d' % args.batch_size) logging.info('negative_adversarial_sampling = %d' % args.negative_adversarial_sampling) logging.info('hidden_dim = %d' % args.hidden_dim) logging.info('gamma = %f' % args.gamma) logging.info('negative_adversarial_sampling = %s' % str(args.negative_adversarial_sampling)) if args.negative_adversarial_sampling: logging.info('adversarial_temperature = %f' % args.adversarial_temperature) # Set valid dataloader as it would be evaluated during training if args.do_train: logging.info('learning_rate = %d' % current_learning_rate) training_logs = [] #Training Loop for step in range(init_step, args.max_steps): log = kge_model.train_step(kge_model, optimizer, train_iterator, args) training_logs.append(log) if step >= warm_up_steps: current_learning_rate = current_learning_rate / 10 logging.info('Change learning_rate to %f at step %d' % (current_learning_rate, step)) optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad, kge_model.parameters()), lr=current_learning_rate) warm_up_steps = warm_up_steps * 3 if step % args.save_checkpoint_steps == 0: save_variable_list = { 'step': step, 'current_learning_rate': current_learning_rate, 'warm_up_steps': warm_up_steps } save_model(kge_model, optimizer, save_variable_list, args) if step % args.log_steps == 0: metrics = {} for metric in training_logs[0].keys(): metrics[metric] = sum( [log[metric] for log in training_logs]) / len(training_logs) log_metrics('Training average', step, metrics) training_logs = [] if args.do_valid and step % args.valid_steps == 0: logging.info('Evaluating on Valid Dataset...') metrics = kge_model.test_step(kge_model, valid_triples, all_true_triples, args) log_metrics('Valid', step, metrics) save_variable_list = { 'step': step, 'current_learning_rate': current_learning_rate, 'warm_up_steps': warm_up_steps } save_model(kge_model, optimizer, save_variable_list, args) if args.do_valid: logging.info('Evaluating on Valid Dataset...') metrics = kge_model.test_step(kge_model, valid_triples, all_true_triples, args, candidate_entities, id2e=id2entity, id2rel=id2relationship) log_metrics('Valid', step, metrics) # if args.do_test: # logging.info('Evaluating on Test Dataset...') # metrics = kge_model.test_step(kge_model, test_triples, all_true_triples, args) # log_metrics('Test', step, metrics) if args.do_test: logging.info('Evaluating on Test Dataset...') metrics = kge_model.test_step(kge_model, test_triples, all_true_triples, args, candidate_entities, id2e=id2entity, id2rel=id2relationship) log_metrics('Test', step, metrics) if args.evaluate_train: logging.info('Evaluating on Training Dataset...') metrics = kge_model.test_step(kge_model, train_triples, all_true_triples, args) log_metrics('Test', step, metrics)