def test_inference_on_pretrained_model(): args = KGEArgParser().get_args([]) config_def, model_def = Importer().import_model_config("transe") config = config_def(args) config.load_from_data = os.path.join(os.path.dirname(__file__), "resource", "pretrained", "TransE", Trainer.TRAINED_MODEL_FILE_NAME) model = model_def(**config.__dict__) # Create the model and load the trained weights. trainer = Trainer(model, config) trainer.build_model() #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
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(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 testing_function(name): """Function to test the models with arguments.""" # getting the customized configurations from the command-line arguments. args = KGEArgParser().get_args(['-exp', 'True', '-mn', name]) # 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.save_model = False config.debug = True config.ent_hidden_size = 10 config.rel_hidden_size = 10 config.channels = 2 model = model_def(**config.__dict__) # Create, Compile and Train the model. While training, several evaluation will be performed. trainer = Trainer(model, config) trainer.build_model() trainer.train_model()
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 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 test_full_epochs(tmpdir, config_key): result_path_dir = tmpdir.mkdir("result_path") configured_epochs = 10 model, config = get_model(result_path_dir, configured_epochs, -1, config_key) trainer = Trainer(model, config) trainer.build_model() actual_epochs = trainer.train_model() assert actual_epochs == configured_epochs - 1
def test_early_stopping_on_ranks(tmpdir, monitor): result_path_dir = tmpdir.mkdir("result_path") configured_epochs = 10 model, config = get_model(result_path_dir, configured_epochs, 0, "complex") trainer = Trainer(model, config) trainer.build_model(monitor=monitor) actual_epochs = trainer.train_model() assert actual_epochs < configured_epochs - 1
def test_error_on_building_pretrained_model(): with pytest.raises(ValueError) as e: args = KGEArgParser().get_args([]) config_def, model_def = Importer().import_model_config("transe") config = config_def(args) config.load_from_data = "pretrained-model-does-not-exist" model = model_def(**config.__dict__) trainer = Trainer(model, config) trainer.build_model() assert "Cannot load model from %s" % config.load_from_data in str(e)
def main(): args = KGEArgParser().get_args(sys.argv[1:]) config_def, model_def = Importer().import_model_config(args.model_name.lower()) config = config_def(args) model = model_def(**config.__dict__) trainer = Trainer(model, config) trainer.build_model() trainer.infer_tails(1, 10, topk=5) trainer.infer_heads(10, 20, topk=5) trainer.infer_rels(1, 20, topk=5)
def main(): args = KGEArgParser().get_args(sys.argv[1:]) config_def, model_def = Importer().import_model_config( args.model_name.lower()) config = config_def(args) model = model_def(**config.__dict__) trainer = Trainer(model, config) trainer.build_model() trainer.model.eval() trainer.evaluator.full_test(0)
def main(): args = KGEArgParser().get_args(sys.argv[1:]) knowledge_graph = KnowledgeGraph(dataset=args.dataset_name, custom_dataset_path=args.dataset_path) knowledge_graph.prepare_data() config_def, model_def = Importer().import_model_config(args.model_name.lower()) config = config_def(args) model = model_def(**config.__dict__) trainer = Trainer(model, config) trainer.build_model() trainer.train_model()
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 test_DistMult(self): config = DistMultConfig(batch_size=512, epochs=1) config.set_dataset("Freebase15k") config.test_step = 1 config.test_num = 10 config.gpu_fraction = 0.4 config.save_model = False config.disp_result = False model = DistMult(config) trainer = Trainer(model=model, debug=True) trainer.build_model() trainer.train_model()
def test_SMEB(self): config = SMEConfig(batch_size=512, epochs=1, hidden_size=8) config.set_dataset("Freebase15k") config.test_step = 1 config.test_num = 10 config.gpu_fraction = 0.4 config.save_model = True config.disp_result = False config.bilinear = True model = SME(config) trainer = Trainer(model=model, debug=True) 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, 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.__dict__) # Create, Compile and Train the model. While training, several evaluation will be performed. trainer = Trainer(model, config) trainer.build_model() trainer.train_model()
def main(): # getting the customized configurations from the command-line arguments. args = KGEArgParser().get_args(sys.argv[1:]) # 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.__dict__) # Create the model and load the trained weights. trainer = Trainer(model, config) trainer.build_model() trainer.infer_tails(1, 10, topk=5) trainer.infer_heads(10, 20, topk=5) trainer.infer_rels(1, 20, topk=5)
def test_ProjE(self): config = ProjE_pointwiseConfig(learning_rate=0.01, batch_size=512, epochs=1) config.set_dataset("Freebase15k") config.test_step = 1 config.test_num = 10 config.gpu_fraction = 0.4 config.save_model = False config.disp_result = False model = ProjE_pointwise(config) trainer = Trainer(model=model, debug=True) trainer.build_model() trainer.train_model()
def test_transH(self): config = TransHConfig(batch_size=512, epochs=1, hidden_size=16) config.set_dataset("Freebase15k") config.test_step = 1 config.test_num = 10 config.gpu_fraction = 0.4 config.save_model = False config.disp_result = False config.C = 0.125 config.sampling = "bern" model = TransH(config) trainer = Trainer(model=model, debug=True) trainer.build_model() trainer.train_model()
def test_KG2E_EL(self): config = KG2EConfig(batch_size=512, epochs=1, distance_measure="expected_likelihood") config.set_dataset("Freebase15k") config.test_step = 1 config.test_num = 10 config.gpu_fraction = 0.4 config.save_model = False config.disp_result = False model = KG2E(config) trainer = Trainer(model=model, debug=True) 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, 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) model = model_def(**config.__dict__) # Create, Compile and Train the model. While training, several evaluation will be performed. trainer = Trainer(model, config) trainer.build_model() trainer.train_model()
def test_TransD(self): config = TransDConfig(batch_size=512, epochs=1, ent_hidden_size=8, rel_hidden_size=8) config.set_dataset("Freebase15k") config.test_step = 1 config.test_num = 10 config.gpu_fraction = 0.4 config.save_model = False config.disp_result = False model = TransD(config) trainer = Trainer(model=model, debug=True) trainer.build_model() trainer.train_model()
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 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 test_visualization(tmpdir): result_path_dir = tmpdir.mkdir("result_path") args = KGEArgParser().get_args([]) knowledge_graph = KnowledgeGraph(dataset="Freebase15k") knowledge_graph.prepare_data() config_def, model_def = Importer().import_model_config("analogy") config = config_def(args=args) config.epochs = 5 config.test_step = 1 config.test_num = 1 config.disp_result = True config.save_model = False config.debug = True config.patience = -1 config.plot_embedding = True config.plot_training_result = True config.plot_testing_result = True config.path_figures = result_path_dir config.path_result = result_path_dir trainer = Trainer(model_def(**config.__dict__), config) trainer.build_model() trainer.train_model() files = [f for f in listdir(result_path_dir)] assert any(map(lambda f: "_entity_plot" in f, files)) assert any(map(lambda f: "_rel_plot" in f, files)) assert any(map(lambda f: "_ent_n_rel_plot" in f, files)) assert any(map(lambda f: "_training_loss_plot_" in f, files)) assert any(map(lambda f: "_testing_hits_plot" in f, files)) assert any(map(lambda f: "_testing_latex_table_" in f, files)) assert any(map(lambda f: "_testing_table_" in f, files)) assert any(map(lambda f: "_testing_rank_plot_" in f, files)) assert any(map(lambda f: "_testing_hits_plot_" in f, files))
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()
def main(): model_name = "transe" dataset_name = "Freebase15k" # 1. Tune the hyper-parameters for the selected model and dataset. # p.s. this is using training and validation set. args = KGETuneArgParser().get_args( ['-mn', model_name, '-ds', dataset_name]) # initializing bayesian optimizer and prepare data. bays_opt = BaysOptimizer(args=args) # perform the golden hyperparameter tuning. bays_opt.optimize() best = bays_opt.return_best() # 2. Evaluate final model using the found best hyperparameters on testing set. args = KGEArgParser().get_args(['-mn', model_name, '-ds', dataset_name]) # 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) # Update the config params with the golden hyperparameter for k, v in best.items(): config.__dict__[k] = v model = model_def(**config.__dict__) # Create, Compile and Train the model. trainer = Trainer(model, config) trainer.build_model() trainer.train_model()
class BaysOptimizer(object): """Bayesian optimizer class for tuning hyperparameter. This class implements the Bayesian Optimizer for tuning the hyper-parameter. Args: args (object): The Argument Parser object providing arguments. name_dataset (str): The name of the dataset. sampling (str): sampling to be used for generating negative triples Examples: >>> from pykg2vec.config.hyperparams import KGETuneArgParser >>> from pykg2vec.utils.bayesian_optimizer import BaysOptimizer >>> model = Complex() >>> args = KGETuneArgParser().get_args(sys.argv[1:]) >>> bays_opt = BaysOptimizer(args=args) >>> bays_opt.optimize() """ def __init__(self, args=None): """store the information of database""" model_name = args.model.lower() self.args = args self.knowledge_graph = KnowledgeGraph(dataset=args.dataset_name, negative_sample=args.sampling) hyper_params = None try: self.model_obj = getattr( importlib.import_module(model_path + ".%s" % modelMap[model_name]), modelMap[model_name]) self.config_obj = getattr(importlib.import_module(config_path), configMap[model_name]) hyper_params = getattr(importlib.import_module(hyper_param_path), hypMap[model_name])() except ModuleNotFoundError: print("%s not implemented! Select from: %s" % (model_name, ' '.join(map(str, modelMap.values())))) config = self.config_obj() config.data = args.dataset_name self.trainer = Trainer(model=self.model_obj(config), debug=self.args.debug, tuning=True) self.search_space = self.define_search_space(hyper_params) self.max_evals = self.args.max_number_trials if not self.args.debug else 1 def define_search_space(self, hyper_params): """Function to perform search space addition""" space = { k: hp.choice(k, v) for k, v in hyper_params.__dict__.items() if not k.startswith('__') and not callable(k) } return space def optimize(self): """Function that performs bayesian optimization""" space = self.search_space trials = Trials() best_result = fmin(fn=self.get_loss, space=space, algo=tpe.suggest, max_evals=self.max_evals, trials=trials) columns = list(space.keys()) results = pd.DataFrame(columns=['iteration'] + columns + ['loss']) for idx, trial in enumerate(trials.trials): row = [] row.append(idx) translated_eval = space_eval( self.search_space, {k: v[0] for k, v in trial['misc']['vals'].items()}) for k in columns: row.append(translated_eval[k]) row.append(trial['result']['loss']) results.loc[idx] = row path = self.trainer.config.result / self.trainer.model.model_name path.mkdir(parents=True, exist_ok=True) results.to_csv(str(path / "trials.csv"), index=False) print(results) print('Found Golden Setting:') pprint(space_eval(space, best_result)) def get_loss(self, params): """Function that defines and acquires the loss""" self.trainer.config.L1_flag = params['L1_flag'] self.trainer.config.batch_size = params['batch_size'] self.trainer.config.epochs = params['epochs'] if 'hidden_size' in params: self.trainer.config.hidden_size = params['hidden_size'] if 'ent_hidden_size' in params: self.trainer.config.ent_hidden_size = params['ent_hidden_size'] if 'rel_hidden_size' in params: self.trainer.config.rel_hidden_size = params['rel_hidden_size'] self.trainer.config.learning_rate = params['learning_rate'] self.trainer.config.margin = params['margin'] self.trainer.config.disp_result = False self.trainer.config.disp_summary = False self.trainer.config.save_model = False self.trainer.config.debug = True self.trainer.config.test_num = 1000 self.trainer.build_model() self.trainer.summary_hyperparameter() loss = self.trainer.tune_model() # loss = self.trainer.train_model(tuning=True) return {'loss': loss, 'status': STATUS_OK}