def test_userdefined_dataset(): custom_dataset_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), "resource/custom_dataset") knowledge_graph = KnowledgeGraph(dataset="userdefineddataset", negative_sample="uniform", custom_dataset_path=custom_dataset_path) knowledge_graph.prepare_data() knowledge_graph.dump() knowledge_graph.read_cache_data('triplets_train') knowledge_graph.read_cache_data('triplets_test') knowledge_graph.read_cache_data('triplets_valid') knowledge_graph.read_cache_data('hr_t') knowledge_graph.read_cache_data('tr_h') knowledge_graph.read_cache_data('idx2entity') knowledge_graph.read_cache_data('idx2relation') knowledge_graph.read_cache_data('entity2idx') knowledge_graph.read_cache_data('relation2idx') knowledge_graph.dataset.read_metadata() knowledge_graph.dataset.dump() assert knowledge_graph.kg_meta.tot_train_triples == 1 assert knowledge_graph.kg_meta.tot_test_triples == 1 assert knowledge_graph.kg_meta.tot_valid_triples == 1 assert knowledge_graph.kg_meta.tot_entity == 6 assert knowledge_graph.kg_meta.tot_relation == 3
def main(): # getting the customized configurations from the command-line arguments. args = KGEArgParser().get_args(sys.argv[1:]) # Preparing data and cache the data for later usage knowledge_graph = KnowledgeGraph(dataset=args.dataset_name, negative_sample=args.sampling, custom_dataset_path=args.dataset_path) knowledge_graph.prepare_data() # Extracting the corresponding model config and definition from Importer(). config_def, model_def = Importer().import_model_config( args.model_name.lower()) config = config_def(args=args) model = model_def(config) # Create, Compile and Train the model. While training, several evaluation will be performed. trainer = Trainer(model=model, debug=args.debug) trainer.build_model() trainer.train_model() #can perform all the inference here after training the model trainer.enter_interactive_mode() code.interact(local=locals()) trainer.exit_interactive_mode()
def testing_function_with_args(name, distance_measure=None, bilinear=None, display=False): """Function to test the models with arguments.""" # getting the customized configurations from the command-line arguments. args = KGEArgParser().get_args([]) # Preparing data and cache the data for later usage knowledge_graph = KnowledgeGraph(dataset=args.dataset_name, negative_sample=args.sampling) knowledge_graph.prepare_data() # Extracting the corresponding model config and definition from Importer(). config_def, model_def = Importer().import_model_config(name) config = config_def(args=args) config.epochs = 1 config.test_step = 1 config.test_num = 10 config.disp_result = display config.save_model = False model = model_def(config) # Create, Compile and Train the model. While training, several evaluation will be performed. trainer = Trainer(model=model, debug=True) trainer.build_model() trainer.train_model() tf.reset_default_graph()
def test(self): """Function to evaluate final model on testing set while training the model using best hyper-paramters on merged training and validation set.""" args = KGEArgParser().get_args([]) args.model = self.model args.dataset_name = self.dataset args.debug = self.debug # Preparing data and cache the data for later usage knowledge_graph = KnowledgeGraph(dataset=args.dataset_name) knowledge_graph.prepare_data() # Extracting the corresponding model config and definition from Importer(). config_def, model_def = Importer().import_model_config( args.model_name.lower()) config = config_def(args=args) # Update the config params with the golden hyperparameter for k, v in self.best.items(): config.__dict__[k] = v model = model_def(config) if self.debug: config.epochs = 1 # Create, Compile and Train the model. While training, several evaluation will be performed. trainer = Trainer(model=model) trainer.build_model() trainer.train_model()
def testing_function(name, distance_measure=None, bilinear=None, display=False): """Function to test the models.""" knowledge_graph = KnowledgeGraph(dataset="freebase15k", negative_sample="uniform") knowledge_graph.prepare_data() # Extracting the corresponding model config and definition from Importer(). config_def, model_def = Importer().import_model_config(name) config = config_def() config.epochs = 1 config.test_step = 1 config.test_num = 10 config.disp_result = display config.save_model = False if distance_measure is not None: config.distance_measure = distance_measure if bilinear is not None: config.bilinear = bilinear model = model_def(config) # Create, Compile and Train the model. While training, several evaluation will be performed. trainer = Trainer(model=model, debug=True) trainer.build_model() trainer.train_model() tf.reset_default_graph()
def main(): args = KGEArgParser().get_args(sys.argv[1:]) if Path(args.dataset_path).exists(): kdl = KnowledgeDataLoader(data_dir=args.dataset_path, negative_sampling=args.sampling) kg = kdl.get_knowledge_graph() print('Successfully loaded {} triples from {}.'.format( len(kdl.triples), kdl.data_dir)) else: print('Unable to find dataset from path:', args.dataset_path) print( 'Default loading Freebase15k dataset with default hyperparameters...' ) kg = KnowledgeGraph() kg.prepare_data() kg.dump() # TODO: Not sure why new dataset isn't cached on subsequent hits... args.dataset_path = './data/' + kg.dataset_name args.dataset_name = kg.dataset_name # Add new model configurations to run. models = [TransE(transe_config(args=args))] for model in models: print('---- Training Model: {} ----'.format(model.model_name)) trainer = Trainer(model=model, debug=args.debug) trainer.build_model() trainer.train_model() tf.reset_default_graph()
def test_kgpipeline(): """Function to test the KGPipeline function.""" # Preparing data and cache the data for later usage knowledge_graph = KnowledgeGraph(dataset="Freebase15k") knowledge_graph.prepare_data() kg_pipeline = KGPipeline(model="transe", dataset="Freebase15k", debug=True) kg_pipeline.tune() kg_pipeline.test()
def test_fb15k_meta(): """Function to test the the knowledge graph parse for Freebase and basic operations.""" knowledge_graph = KnowledgeGraph(dataset="freebase15k") knowledge_graph.force_prepare_data() knowledge_graph.dump() assert knowledge_graph.is_cache_exists() knowledge_graph.prepare_data() knowledge_graph.dataset.read_metadata() knowledge_graph.dataset.dump()
def testing_function_with_args(name, l1_flag, distance_measure=None, bilinear=None, display=False): """Function to test the models with arguments.""" tf.reset_default_graph() # getting the customized configurations from the command-line arguments. args = KGEArgParser().get_args([]) # Preparing data and cache the data for later usage knowledge_graph = KnowledgeGraph(dataset=args.dataset_name, negative_sample=args.sampling) knowledge_graph.prepare_data() # Extracting the corresponding model config and definition from Importer(). config_def, model_def = Importer().import_model_config(name) config = config_def(args=args) config.epochs = 1 config.test_step = 1 config.test_num = 10 config.disp_result = display config.save_model = True config.L1_flag = l1_flag model = model_def(config) # Create, Compile and Train the model. While training, several evaluation will be performed. trainer = Trainer(model=model, debug=True) trainer.build_model() trainer.train_model() #can perform all the inference here after training the model trainer.enter_interactive_mode() #takes head, relation tails = trainer.infer_tails(1, 10, topk=5) assert len(tails) == 5 #takes relation, tail heads = trainer.infer_heads(10, 20, topk=5) assert len(heads) == 5 #takes head, tail relations = trainer.infer_rels(1, 20, topk=5) assert len(relations) == 5 trainer.exit_interactive_mode()
def tunning_function(name): """Function to test the tuning of the models.""" knowledge_graph = KnowledgeGraph(dataset="freebase15k") knowledge_graph.prepare_data() # getting the customized configurations from the command-line arguments. args = KGETuneArgParser().get_args([]) # initializing bayesian optimizer and prepare data. args.debug = True args.model = name bays_opt = BaysOptimizer(args=args) bays_opt.trainer.config.test_num = 10 # perform the golden hyperparameter tuning. bays_opt.optimize()
def main(): # getting the customized configurations from the command-line arguments. args = KGEArgParser().get_args(sys.argv[1:]) knowledge_graph = KnowledgeGraph(dataset=args.dataset_name, negative_sample=args.sampling) knowledge_graph.prepare_data() # Extracting the corresponding model config and definition from Importer(). config_def, model_def = Importer().import_model_config( args.model_name.lower()) config = config_def(args=args) model = model_def(config) # Create, Compile and Train the model. While training, several evaluation will be performed. trainer = Trainer(model=model, debug=args.debug) trainer.build_model() trainer.load_model() trainer.export_embeddings()
def get_model(result_path_dir, configured_epochs, patience, config_key): args = KGEArgParser().get_args([]) knowledge_graph = KnowledgeGraph(dataset="Freebase15k") knowledge_graph.prepare_data() config_def, model_def = Importer().import_model_config(config_key) config = config_def(args=args) config.epochs = configured_epochs config.test_step = 1 config.test_num = 1 config.disp_result = False config.save_model = False config.path_result = result_path_dir config.debug = True config.patience = patience return model_def(config)
def main(): # getting the customized configurations from the command-line arguments. args = KGEArgParser().get_args(sys.argv[1:]) # Preparing data and cache the data for later usage knowledge_graph = KnowledgeGraph(dataset=args.dataset_name, custom_dataset_path=args.dataset_path) knowledge_graph.prepare_data() # Extracting the corresponding model config and definition from Importer(). config_def, model_def = Importer().import_model_config( args.model_name.lower()) config = config_def(args) model = model_def(config) # Create, Compile and Train the model. While training, several evaluation will be performed. trainer = Trainer(model=model) trainer.build_model() trainer.train_model()
def experiment(model_name): args = KGEArgParser().get_args([]) args.exp = True args.dataset_name = "fb15k" # Preparing data and cache the data for later usage knowledge_graph = KnowledgeGraph(dataset=args.dataset_name, negative_sample=args.sampling, custom_dataset_path=args.dataset_path) knowledge_graph.prepare_data() # Extracting the corresponding model config and definition from Importer(). config_def, model_def = Importer().import_model_config(model_name) config = config_def(args=args) model = model_def(config) # Create, Compile and Train the model. While training, several evaluation will be performed. trainer = Trainer(model=model) trainer.build_model() trainer.train_model()
def run_pykg2vec(): # getting the customized configurations from the command-line arguments. args = PyKG2VecArgParser().get_args(sys.argv[1:]) args.dataset_path = preprocess(args.triples_path, args.dataset_name) # Preparing data and cache the data for later usage knowledge_graph = KnowledgeGraph(dataset=args.dataset_name, negative_sample=args.sampling, custom_dataset_path=args.dataset_path) knowledge_graph.prepare_data() # Extracting the corresponding model config and definition from Importer(). config_def, model_def = Importer().import_model_config( args.model_name.lower()) config = config_def(args=args) model = model_def(config) # Create, Compile and Train the model. While training, several evaluation will be performed. trainer = Trainer(model=model, debug=args.debug) trainer.build_model() trainer.train_model()
def testing_function(name, distance_measure=None, bilinear=None, display=False, ent_hidden_size=None, rel_hidden_size=None, channels=None): """Function to test the models with arguments.""" # getting the customized configurations from the command-line arguments. args = KGEArgParser().get_args(['-exp', 'True']) # Preparing data and cache the data for later usage knowledge_graph = KnowledgeGraph(dataset=args.dataset_name) knowledge_graph.prepare_data() # Extracting the corresponding model config and definition from Importer(). config_def, model_def = Importer().import_model_config(name) config = config_def(args) config.epochs = 1 config.test_step = 1 config.test_num = 10 config.disp_result = display config.save_model = False config.debug = True if ent_hidden_size: config.ent_hidden_size = ent_hidden_size if rel_hidden_size: config.rel_hidden_size = rel_hidden_size if channels: config.channels = channels model = model_def(config) # Create, Compile and Train the model. While training, several evaluation will be performed. trainer = Trainer(model=model) trainer.build_model() trainer.train_model()
def main(): # getting the customized configurations from the command-line arguments. args = KGEArgParser().get_args(sys.argv[1:]) # Preparing data and cache the data for later usage knowledge_graph = KnowledgeGraph(dataset=args.dataset_name, negative_sample=args.sampling) knowledge_graph.prepare_data() sess_infer = tf.InteractiveSession() # Extracting the corresponding model config and definition from Importer(). config_def, model_def = Importer().import_model_config(args.model_name.lower()) config = config_def(args=args) model = model_def(config) # Create, Compile and Train the model. While training, several evaluation will be performed. trainer = Trainer(model=model, debug=args.debug) trainer.build_model() trainer.train_model() #can perform all the inference here after training the model #takes head, relation trainer.infer_tails(1,10,sess_infer,topk=5) #takes relation, tails trainer.infer_heads(10,20,sess_infer,topk=5) sess_infer.close()