Exemple #1
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()
Exemple #2
0
def test_generator():
    import timeit
    start_time = timeit.default_timer()
    from config.config import TransEConfig
    config = TransEConfig()
    config.set_dataset("Freebase15k")

    gen = Generator(config=GeneratorConfig(data='train', algo='tucker'),
                    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))
        # import pdb
        # pdb.set_trace()
        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)# time.sleep(0.05)
        # print("hr_hr:", hr_hr)
        # print("hr_t:", hr_t)
        # print("tr_tr:", tr_tr)
        # print("tr_h:", tr_h)
    print("total time:", timeit.default_timer() - start_time)
    gen.stop()
Exemple #3
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()
Exemple #4
0
    def tune_model(self):
        """Function to tune the model."""
        acc = 0

        generator_config = GeneratorConfig(
            data=self.trainon,
            algo=self.model.model_name,
            batch_size=self.model.config.batch_size)
        self.gen_train = Generator(config=generator_config,
                                   model_config=self.model.config)

        self.evaluator = Evaluation(model=self.model,
                                    data_type=self.teston,
                                    debug=self.debug,
                                    tuning=True,
                                    session=self.sess)

        for n_iter in range(self.config.epochs):
            self.train_model_epoch(n_iter)

        self.gen_train.stop()
        self.evaluator.test_batch(n_iter)
        acc = self.evaluator.output_queue.get()
        self.evaluator.stop()
        self.sess.close()
        tf.reset_default_graph(
        )  # clean the tensorflow for the next training task.

        return acc
Exemple #5
0
    def train_model(self):
        """Function to train the model."""
        loss = 0

        if self.config.loadFromData:
            self.load_model()
        else:
            generator_config = GeneratorConfig(
                data=self.trainon,
                algo=self.model.model_name,
                batch_size=self.model.config.batch_size)
            self.gen_train = Generator(config=generator_config,
                                       model_config=self.model.config)

            if not self.tuning:
                self.evaluator = Evaluation(model=self.model,
                                            data_type=self.teston,
                                            debug=self.debug,
                                            session=self.sess)

            for n_iter in range(self.config.epochs):
                loss = self.train_model_epoch(n_iter)
                if not self.tuning:
                    self.test(n_iter)

            self.gen_train.stop()

            if not self.tuning:
                self.evaluator.save_training_result(self.training_results)
                self.evaluator.stop()

            if self.config.save_model:
                self.save_model()

        if self.config.disp_result:
            self.display()

        if self.config.disp_summary:
            self.summary()
            self.summary_hyperparameter()

        if not os.path.exists("./tmp"):
            os.mkdir("./tmp")

        save_path = self.saver.save(self.sess, "./tmp/model.ckpt")
        self.sess.close()
        tf.reset_default_graph(
        )  # clean the tensorflow for the next training task.

        return loss
Exemple #6
0
    def build_model(self):
        """function to build the model"""
        self.model.def_inputs()
        self.model.def_parameters()
        if getattr(self.model, "def_layer", None):
            self.model.def_layer()
        self.model.def_loss()

        if not self.debug:
            self.sess = tf.Session(config=self.config.gpu_config)
        else:
            self.sess = tf.InteractiveSession()
        self.global_step = tf.Variable(0, name="global_step", trainable=False)

        if self.config.optimizer == 'sgd':
            optimizer = tf.train.GradientDescentOptimizer(
                learning_rate=self.config.learning_rate)
        elif self.config.optimizer == 'rms':
            optimizer = tf.train.RMSPropOptimizer(
                learning_rate=self.config.learning_rate)
        elif self.config.optimizer == 'adam':
            optimizer = tf.train.AdamOptimizer(
                learning_rate=self.config.learning_rate)
        elif self.config.optimizer == 'adagrad':
            optimizer = tf.train.AdagradOptimizer(
                learning_rate=self.config.learning_rate)
        elif self.config.optimizer == 'adadelta':
            optimizer = tf.train.AdadeltaOptimizer(
                learning_rate=self.config.learning_rate)
        else:
            raise NotImplementedError("No support for %s optimizer" %
                                      self.config.optimizer)

        grads = optimizer.compute_gradients(self.model.loss)
        self.op_train = optimizer.apply_gradients(grads,
                                                  global_step=self.global_step)
        self.sess.run(tf.global_variables_initializer())
        self.saver = tf.train.Saver()
        if not self.tuning:
            self.summary()
            self.summary_hyperparameter()

        self.generator_config = GeneratorConfig(
            data=self.trainon,
            algo=self.model.model_name,
            batch_size=self.model.config.batch_size,
            process_num=self.model.config.num_process_gen,
            neg_rate=self.config.neg_rate)
Exemple #7
0
def test_generator_trans():

    gen = Generator(config=GeneratorConfig(data='test', algo='TransE'))

    for i in range(1000):
        data = list(next(gen))
        print("----batch:", i)
        ph = data[0]
        pr = data[1]
        pt = data[2]
        # nh = data[3]
        # nr = data[4]
        # nt = data[5]
        print("ph:", ph)
        print("pr:", pr)
        print("pt:", pt)
        # print("nh:", nh)
        # print("nr:", nr)
        # print("nt:", nt)
    gen.stop()
Exemple #8
0
def test_generator_proje():
    from config.config import ProjE_pointwiseConfig
    config = ProjE_pointwiseConfig()
    config.set_dataset("Freebase15k")
    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)