def setup_trainer(self, sz, sampler, ev, existing_model=None):
     model = HolE(sz,
                  self.args.ncomp,
                  rparam=self.args.rparam,
                  af=afs[self.args.afs],
                  inite=self.args.inite,
                  initr=self.args.initr,
                  ev=ev,
                  model=existing_model)
     if self.args.no_pairwise:
         trainer = StochasticTrainer(model,
                                     nbatches=self.args.nb,
                                     max_epochs=self.args.me,
                                     post_epoch=[self.callback],
                                     learning_rate=self.args.lr,
                                     samplef=sampler.sample)
     else:
         trainer = PairwiseStochasticTrainer(model,
                                             nbatches=self.args.nb,
                                             margin=self.args.margin,
                                             max_epochs=self.args.me,
                                             learning_rate=self.args.lr,
                                             samplef=sampler.sample,
                                             post_epoch=[self.callback])
     return trainer
Beispiel #2
0
def train(triples):
    # Load knowledge graph
    # N = number of entities
    # M = number of relations
    # xs = list of (subject, object, predicte) triples
    # ys = list of truth values for triples (1 = true, -1 = false)


    #N, M = 2,1
    #xs = [(0, 1, 0)]
    #ys = np.ones(len(xs))

    N, M, xs, ys = generate_kg_for_training(triples)

    # instantiate HolE with an embedding space of size 100
    model = HolE((N, N, M), 10)

    # instantiate trainer
    trainer = StochasticTrainer(model)

    # fit model to knowledge graph
    trainer.fit(xs, ys)

    model.E
    model.R
    print('finish')
Beispiel #3
0
    def learn_model(self,
                    X,
                    types=None,
                    type_hierarchy=None,
                    domains=None,
                    ranges=None):
        self.X = X
        N = self.X[0].shape[1]
        self.sz = (N, N, len(self.X))
        if self.model == "hole":
            embedding_model = HolE(self.sz,
                                   self.n_dim,
                                   rparam=self.rparam,
                                   af=afs[self.activation_function],
                                   init=self.init)
        if self.model == "transe":
            embedding_model = TransE(self.sz, self.n_dim, init=self.init)

        if self.model == "rescal":
            embedding_model = RESCAL(self.sz, self.n_dim, init=self.init)

        xs = []
        for r, slice in enumerate(self.X):
            h = slice.row
            t = slice.col
            xs = xs + zip(h, t, [r] * len(h))

        ys = np.ones(len(xs))
        if self.sample_mode == 'corrupted':
            ti = type_index(xs)
            sampler = CorruptedSampler(self.negative_examples, xs, ti)
        elif self.sample_mode == 'random':
            sampler = RandomModeSampler(self.negative_examples, [0, 1], xs,
                                        self.sz)
        elif self.sample_mode == 'lcwa':
            sampler = LCWASampler(self.negative_examples, [0, 1, 2], xs,
                                  self.sz)

        self.trainer = PairwiseStochasticTrainer(
            embedding_model,
            nbatches=self.n_batches,
            max_epochs=self.max_epochs,
            post_epoch=[callback],
            learning_rate=self.learning_rate,
            margin=self.margin,
            samplef=sampler.sample)
        self.trainer.fit(xs, ys)
        del xs, ys
Beispiel #4
0
 def setup_trainer(self, sz, sampler):
     model = HolE(sz,
                  self.args.ncomp,
                  rparam=self.args.rparam,
                  af=afs[self.args.afs],
                  init=self.args.init)
     if self.args.no_pairwise:
         trainer = StochasticTrainer(model,
                                     nbatches=self.args.nb,
                                     max_epochs=self.args.me,
                                     post_epoch=[self.callback],
                                     learning_rate=self.args.lr,
                                     samplef=sampler.sample)
     else:
         #print ("UNM$$$ Running Pairwise stochastic trainer")
         trainer = PairwiseStochasticTrainer(model,
                                             nbatches=self.args.nb,
                                             max_epochs=self.args.me,
                                             post_epoch=[self.callback],
                                             learning_rate=self.args.lr,
                                             margin=self.args.margin,
                                             samplef=sampler.sample)
     return trainer
Beispiel #5
0
def train_model(method='complex',
                mode='single',
                dimension=200,
                number_of_epochs=300,
                batch_size=128,
                learning_rate=0.05,
                margin=1.0,
                number_negative_samples=10,
                optimzer='adagrad',
                l2_regularization=0.0001,
                gradient_clipping=5,
                epoch_setp_for_saving=100,
                ratio_complex_dimension=0.5):

    if method == 'hole':
        model = HolE(self.shape,
                     self.args.ncomp,
                     init=self.args.init,
                     rparam=self.args.rparam)
    elif method == 'rescal':
        model = HolE(self.shape,
                     self.args.ncomp,
                     init=self.args.init,
                     rparam=self.args.rparam)
    elif method == 'transe':
        model = HolE(self.shape,
                     self.args.ncomp,
                     init=self.args.init,
                     rparam=self.args.rparam)
    else:
        raise NotImplementedError

    if optimzer == 'sgd':
        opt = SGD
    elif optimzer == 'adagrad':
        opt = AdaGrad
    else:
        raise NotImplementedError

    if mode == 'pairwise':
        trainer = PairwiseStochasticTrainer(
            model,
            nbatches=batch_size,
            max_epochs=number_of_epochs,
            #post_epoch=[self.callback],
            learning_rate=learning_rate,
            margin=margin,
            af=af.Sigmoid)
    elif mode == 'single':
        trainer = StochasticTrainer(model,
                                    nbatches=100,
                                    max_epochs=500,
                                    post_epoch=[self.callback],
                                    learning_rate=0.1)

    pass

    if l2_regularization > 0:
        opt.set_l2_reg(l2_regularization)
    if gradient_clipping > 0:
        opt.set_gradclip(gradient_clipping)

    from .kge.transe import TransE
    model = TransE(n_entity=n_entity,
                   n_relation=n_relation,
                   margin=margin,
                   dim=dimension,
                   mode=mode)

    if mode == 'pairwise':
        trainer = PairwiseTrainer(model=model,
                                  opt=opt,
                                  save_step=args.save_step,
                                  batchsize=args.batch,
                                  logger=logger,
                                  evaluator=evaluator,
                                  valid_dat=valid_dat,
                                  n_negative=args.negative,
                                  epoch=args.epoch,
                                  model_dir=args.log)
    elif mode == 'single':
        trainer = SingleTrainer(model=model,
                                opt=opt,
                                save_step=args.save_step,
                                batchsize=args.batch,
                                logger=logger,
                                evaluator=evaluator,
                                valid_dat=valid_dat,
                                n_negative=args.negative,
                                epoch=args.epoch,
                                model_dir=args.log)

    pass
triples = pd.read_csv(b_dir + 'triples.txt', sep='\t', header=None)
triples.columns = ['relation_id', 'entity_id1', 'entity_id2']

# https://github.com/mnick/scikit-kge
from skge import HolE, StochasticTrainer

# Load knowledge graph
# N = number of entities
# M = number of relations
# xs = list of (subject, object, predicte) triples
# ys = list of truth values for triples (1 = true, -1 = false)
N, M, xs, ys = len(entities), len(relations), np.array(triples[[
    'entity_id1', 'entity_id2', 'relation_id']]), np.ones(len(triples), dtype=np.int)

# instantiate HolE with an embedding space of size 100
model = HolE((N, N, M), 50)

# instantiate trainer
trainer = StochasticTrainer(model, nbatches=10,
                            max_epochs=50)

# fit model to knowledge graph
trainer.fit(xs, ys)

model.params['E'].shape
model.params['R'].shape

from scipy.spatial import distance


E = model.params['E']