Beispiel #1
0
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()
Beispiel #2
0
    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()
Beispiel #3
0
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()
Beispiel #4
0
def test_generator_proje():
    """Function to test the generator for ProjE algorithm."""
    knowledge_graph = KnowledgeGraph(dataset="freebase15k", negative_sample="uniform")
    knowledge_graph.force_prepare_data()

    args = KGEArgParser().get_args([])

    config = ProjE_pointwiseConfig(args=args)

    gen = iter(Generator(config=GeneratorConfig(data='train', algo='ProjE'), model_config=config))
    
    for i in range(1000):
        data = list(next(gen))
        print("----batch:", i)
        
        hr_hr = data[0]
        hr_t = data[1]
        tr_tr = data[2]
        tr_h = data[3]

        print("hr_hr:", hr_hr)
        print("hr_t:", hr_t)
        print("tr_tr:", tr_tr)
        print("tr_h:", tr_h)
    gen.stop()
Beispiel #5
0
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()
Beispiel #6
0
def test_generator_trane():
    """Function to test the generator for Translation distance based algorithm."""
    knowledge_graph = KnowledgeGraph(dataset="freebase15k", negative_sample="uniform")
    knowledge_graph.force_prepare_data()
    
    args = KGEArgParser().get_args([])
    
    start_time = timeit.default_timer()
    
    config = TransEConfig(args)

    gen = Generator(config=GeneratorConfig(data='train', algo='transe'), model_config=config)

    print("----init time:", timeit.default_timer() - start_time)
    
    for i in range(10):
        start_time_batch = timeit.default_timer()
        data = list(next(gen))
        h = data[0]
        r = data[1]
        t = data[2]
        # hr_t = data[3]
        # tr_h = data[4]
        print("----batch:", i, "----time:",timeit.default_timer() - start_time_batch)
        print(h,r,t)

    print("total time:", timeit.default_timer() - start_time)
    
    gen.stop()
Beispiel #7
0
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()
Beispiel #8
0
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()
Beispiel #9
0
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()
Beispiel #10
0
    def __init__(self, args=None):
        """store the information of database"""
        if args.model.lower() in ["tucker", "tucker_v2", "conve", "convkb", "proje_pointwise"]:
          raise Exception("Model %s has not been supported in tuning hyperparameters!" % args.model)

        model_name = args.model.lower()
        self.args = args
        self.knowledge_graph = KnowledgeGraph(dataset=args.dataset_name, custom_dataset_path=args.dataset_path)
        hyper_params = None
        try:
            self.model_obj = getattr(importlib.import_module(model_path + ".%s" % moduleMap[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:
            self._logger.error("%s not implemented! Select from: %s" % \
                               (model_name, ' '.join(map(str, modelMap.values()))))
        
        from pykg2vec.config.config import KGEArgParser
        kge_args = KGEArgParser().get_args([])
        kge_args.dataset_name = args.dataset_name
        kge_args.debug = self.args.debug
        config = self.config_obj(kge_args)
        model =  self.model_obj(config)
        
        self.trainer = Trainer(model)
        
        self.search_space = hyper_params.search_space
        self.max_evals = self.args.max_number_trials if not self.args.debug else 1
    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
Beispiel #12
0
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()
Beispiel #13
0
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)
Beispiel #14
0
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 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()
Beispiel #16
0
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()
Beispiel #17
0
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 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()
Beispiel #19
0
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()
Beispiel #20
0
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 get_knowledge_graph(self):
        dataset_name = self.data_dir.stem if '.' not in self.data_dir.stem else self.data_dir.stem.split(
            '.')[0]
        custom_dataset_path = self.data_dir.parent / dataset_name
        if not custom_dataset_path.exists():
            Path(custom_dataset_path).mkdir(parents=True, exist_ok=True)

        # Means directory is created, but split files don't exist
        if not custom_dataset_path.is_file():
            self.__load()
            self.__split(write_path=custom_dataset_path, name=dataset_name)
        return KnowledgeGraph(dataset=dataset_name,
                              negative_sample=self.negative_sampling,
                              custom_dataset_path=custom_dataset_path)
Beispiel #22
0
def test_fb15k_manipulate():
    """Function to test the the knowledge graph parse for Freebase and basic operations."""
    knowledge_graph = KnowledgeGraph(dataset="freebase15k",
                                     negative_sample="bern")
    knowledge_graph.force_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')
Beispiel #23
0
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", 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
Beispiel #24
0
def test_dl50a():
    """Function to test the the knowledge graph parse for Deep learning knowledge base."""
    knowledge_graph = KnowledgeGraph(dataset="deeplearning50a", negative_sample="uniform")
    knowledge_graph.force_prepare_data()
    knowledge_graph.dump()
Beispiel #25
0
def test_fb15k():
    """Function to test the the knowledge graph parse for Freebase."""
    knowledge_graph = KnowledgeGraph(dataset="freebase15k", negative_sample="uniform")
    knowledge_graph.force_prepare_data()
    knowledge_graph.dump()
Beispiel #26
0
def test_yago():
    """Function to test the the knowledge graph parse for Yago Dataset."""
    knowledge_graph = KnowledgeGraph(dataset="yago3_10", negative_sample="uniform")
    knowledge_graph.force_prepare_data()
    knowledge_graph.dump()
Beispiel #27
0
def test_wn18rr():
    """Function to test the the knowledge graph parse for Wordnet Dataset."""
    knowledge_graph = KnowledgeGraph(dataset="wordnet18_rr", negative_sample="uniform")
    knowledge_graph.force_prepare_data()
    knowledge_graph.dump()
Beispiel #28
0
def test_benchmarks(dataset_name):
    """Function to test the the knowledge graph parse for Freebase."""
    print("testing downloading from online sources of benchmarks and KG controller's handling.")
    knowledge_graph = KnowledgeGraph(dataset=dataset_name)
    knowledge_graph.force_prepare_data()
    knowledge_graph.dump()