Ejemplo n.º 1
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
Ejemplo n.º 2
0
    def _get_loss(self, params):
        """Function that defines and acquires the loss"""

        # copy the hyperparameters to trainer config and hyperparameter set.
        for key, value in params.items():
            self.config_local.__dict__[key] = value
        self.config_local.__dict__['device'] = self.kge_args.device
        model = self.model_obj(**self.config_local.__dict__)

        self.trainer = Trainer(model, self.config_local)

        # configure common setting for a tuning training.
        self.config_local.disp_result = False
        self.config_local.disp_summary = False
        self.config_local.save_model = False

        # do not overwrite test numbers if set
        if self.config_local.test_num is None:
            self.config_local.test_num = 1000

        if self.kge_args.debug:
            self.config_local.epochs = 1

        # start the trial.
        self.trainer.build_model()
        loss = self.trainer.tune_model()

        return {'loss': loss, 'status': STATUS_OK}
Ejemplo n.º 3
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()
Ejemplo n.º 4
0
    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
Ejemplo n.º 5
0
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
Ejemplo n.º 6
0
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
Ejemplo n.º 7
0
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)
Ejemplo n.º 8
0
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)
Ejemplo n.º 9
0
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)
Ejemplo n.º 10
0
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)
Ejemplo n.º 11
0
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
Ejemplo n.º 12
0
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()
Ejemplo n.º 13
0
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()
Ejemplo n.º 14
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()
Ejemplo n.º 15
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()
Ejemplo n.º 16
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()
Ejemplo n.º 17
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()
Ejemplo n.º 18
0
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()
Ejemplo n.º 19
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()
Ejemplo n.º 20
0
    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()
Ejemplo n.º 21
0
    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()
Ejemplo n.º 22
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.__dict__)

    # Create, Compile and Train the model. While training, several evaluation will be performed.
    trainer = Trainer(model, config)
    trainer.build_model()
    trainer.train_model()
Ejemplo n.º 23
0
    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()
Ejemplo n.º 24
0
    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()
Ejemplo n.º 25
0
    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()
Ejemplo n.º 26
0
    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()
Ejemplo n.º 27
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,
                                     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()
Ejemplo n.º 28
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()
Ejemplo n.º 29
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()
Ejemplo n.º 30
0
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))