Example #1
0
    def pretrain_begin(self, begin, end):
        TrainFlow.clear(self)
        TrainFlow.start_training(self, begin, end)

        Sampler.pretrain_begin(self, begin, end)

        self._sequence.extend([None] * self.pretrain_size)
Example #2
0
    def __init__(self,
                 ds,
                 pretrain_size=10,
                 embdim=16,
                 beta=None,
                 lr=0.1,
                 batchsize=None,
                 sampling_args=None):
        if beta is None:
            beta = [0.1, 0.1]
        if sampling_args is None:
            sampling_args = {}

        self.__dataset = ds

        TrainFlow.__init__(self,
                           embdim=embdim,
                           beta=beta,
                           trainmod=self.name,
                           datasetmod=ds.name)
        Sampler.__init__(self, **sampling_args)

        self.pretrain_size = pretrain_size
        self.lr = lr
        self.batchsize = batchsize

        self.__pretrain = None
        self.__online = None
Example #3
0
    def online_begin(self, begin, end):
        TrainFlow.start_training(self, begin, end)

        Sampler.online_begin(self, begin, end)
        initv = np.random.uniform(
            0, 1,
            (self.dataset.nsize, self.flowargs['embdim'])).astype('float32')
        K.set_value(self.online['vars'][0], initv)
        self._sequence.append(None)
Example #4
0
    def pretrain_begin_iteration(self):
        Sampler.pretrain_begin_iteration(self)

        if self.cur_train_end > self.init_train_begin + self.pretrain_size:  # online phase
            return

        # compute EM coefficients here
        neg1_int, neg1_float = self.__emcoef(self._neg[1])

        self._neg = self._neg[:1] + [neg1_int, neg1_float]
Example #5
0
    def make_online_input(self, batch):
        ret = Sampler.make_online_input(self, batch)

        for d in ret[0]:  # data
            d[0] -= self.init_train_begin

        ret.append(self._sequence[self.cur_train_begin - 1])
        return ret
Example #6
0
 def make_pretrain_input(self, batch):
     ret = Sampler.make_pretrain_input(self, batch)
     # (data, weight, triad)
     # because embedding variable starts from index 0
     for d in ret[0]:  # data
         d[0] -= self.init_train_begin
     for d in ret[2]:
         d[0] -= self.init_train_begin
     return ret
Example #7
0
 def online_end(self):
     Sampler.online_end(self)
     assert self.cur_train_end == self.cur_train_begin + 1, "{} {}".format(
         self.cur_train_end, self.cur_train_begin)
     self.save_model()
     TrainFlow.stop_training(self)
Example #8
0
 def pretrain_end(self):
     Sampler.pretrain_end(self)
     TrainFlow.stop_training(self)
Example #9
0
 def pretrain_end_iteration(self):
     Sampler.pretrain_end_iteration(self)
     self.save_model()