示例#1
0
    def train(self):
        if self._restore:
            return self.restore_weights()

        self._model.initialize()

        print(f"Transactions: {self._data.transactions}")
        best_metric_value = 0

        print("Computing recommendations..")
        recs = self.get_recommendations(
            self.evaluator.get_needed_recommendations())
        result_dict = self.evaluator.eval(recs)
        self._results.append(result_dict)
        print(f'Finished')

        if self._results[-1][self._validation_k]["val_results"][
                self._validation_metric] > best_metric_value:
            print("******************************************")
            if self._save_weights:
                with open(self._saving_filepath, "wb") as f:
                    pickle.dump(self._model.get_model_state(), f)
            if self._save_recs:
                store_recommendation(
                    recs,
                    self._config.path_output_rec_result + f"{self.name}.tsv")
示例#2
0
    def train(self):
        if self._restore:
            return self.restore_weights()

        print(f"Transactions: {self._data.transactions}")
        best_metric_value = -np.inf
        for it in range(self._epochs):
            print(f"\n********** Iteration: {it + 1}")
            self._iteration = it

            self.train_step()

            if not (it + 1) % self._validation_rate:
                recs = self.get_recommendations(
                    self.evaluator.get_needed_recommendations())
                result_dict = self.evaluator.eval(recs)
                self._results.append(result_dict)

                if self._results[-1][self._validation_k]["val_results"][
                        self._validation_metric] > best_metric_value:
                    print("******************************************")
                    best_metric_value = self._results[-1][self._validation_k][
                        "val_results"][self._validation_metric]
                    if self._save_weights:
                        with open(self._saving_filepath, "wb") as f:
                            pickle.dump(self._model.get_model_state(), f)
                    if self._save_recs:
                        store_recommendation(
                            recs, self._config.path_output_rec_result +
                            f"{self.name}-it:{it + 1}.tsv")
示例#3
0
    def train(self):

        if self._restore:
            is_restored = self.restore_weights()
            if is_restored:
                return True
            else:
                print('This Model will start the training!')

        best_metric_value = 0
        loss = 0
        steps = 0
        it = 0
        early_stopping = 5

        with tqdm(total=int(self._data.transactions // self._batch_size),
                  disable=not self._verbose) as t:
            for batch in self._next_batch:
                steps += 1
                loss += self._model.train_step(batch)
                t.set_postfix({'loss': f'{loss.numpy() / steps:.5f}'})
                t.update()

                # epoch is over
                if steps == self._data.transactions // self._batch_size:
                    t.reset()
                    if not (it + 1) % self._validation_rate:
                        recs = self.get_recommendations(
                            self.evaluator.get_needed_recommendations())
                        result_dict = self.evaluator.eval(recs)
                        self._results.append(result_dict)

                        self.logger.info(
                            f'Epoch {(it + 1)}/{self._epochs} loss {loss / steps:.3f}'
                        )

                        if self._results[-1][self._validation_k]["val_results"][
                                self._validation_metric] > best_metric_value:
                            best_metric_value = self._results[-1][
                                self._validation_k]["val_results"][
                                    self._validation_metric]
                            early_stopping = 5
                            if self._save_weights:
                                self._model.save_weights(self._saving_filepath)
                            if self._save_recs:
                                # store_recommendation(recs,
                                #                      self._config.path_output_rec_result + f"{self.name}-it:{it + 1}.tsv")
                                store_recommendation(
                                    recs, self._config.path_output_rec_result +
                                    f"{self.name}.tsv")
                        else:
                            early_stopping -= 1
                            if early_stopping == 0:
                                print(
                                    'Reached Early Stopping Condition at Epoch {0}\n\tEXIT'
                                    .format(it + 1))
                                break
                    it += 1
                    steps = 0
                    loss = 0
示例#4
0
    def restore_weights(self):
        try:
            self._model.load_weights(self._saving_filepath)
            print(f"Model correctly Restored")

            try:
                print('Try to restore rec lists')
                recs = self.restore_recommendation(
                    path=self._config.path_output_rec_result +
                    f"{self.name}.tsv")
            except Exception as error:
                print(f'** Error in Try to restore rec lists\n\t{error}\n')
                print('Evaluate rec lists')
                recs = self.get_recommendations(
                    self.evaluator.get_needed_recommendations())

            result_dict = self.evaluator.eval(recs)
            self._results.append(result_dict)

            print("******************************************")
            if self._save_recs:
                store_recommendation(
                    recs,
                    self._config.path_output_rec_result + f"{self.name}.tsv")
            return True

        except Exception as ex:
            print(f"Error in model restoring operation! {ex}")
            return False
    def train(self):
        recs = self.get_recommendations(self.evaluator.get_needed_recommendations())
        result_dict = self.evaluator.eval(recs)
        self._results.append(result_dict)

        if self._save_recs:
            store_recommendation(recs, self._config.path_output_rec_result + f"{self.name}.tsv")
示例#6
0
    def train(self):
        if self._restore:
            return self.restore_weights()

        best_metric_value = 0

        for it in range(self._epochs):
            dis_loss, gen_loss = 0, 0
            steps = 0
            with tqdm(total=int(self._data.transactions // self._batch_size), disable=not self._verbose) as t:
                for batch in self._sampler.step(self._data.transactions, self._batch_size):
                    steps += 1
                    update_dis_loss, update_gen_loss = self._model.train_step(batch)
                    dis_loss += update_dis_loss
                    gen_loss += update_gen_loss
                    t.set_postfix({'Dis loss': f'{dis_loss.numpy() / steps:.5f}', 'Gen loss': f'{gen_loss.numpy() / steps:.5f}'})
                    t.update()

            if not (it + 1) % self._validation_rate:
                recs = self.get_recommendations(self.evaluator.get_needed_recommendations())
                result_dict = self.evaluator.eval(recs)
                self._results.append(result_dict)

                print(f'Epoch {(it + 1)}/{self._epochs} Dis loss: {dis_loss.numpy() / steps:.5f}, Gen loss: {gen_loss.numpy() / steps:.5f}')

                if self._results[-1][self._validation_k]["val_results"][self._validation_metric] > best_metric_value:
                    print("******************************************")
                    best_metric_value = self._results[-1][self._validation_k]["val_results"][self._validation_metric]
                    if self._save_weights:
                        self._model.save_weights(self._saving_filepath)
                    if self._save_recs:
                        store_recommendation(recs, self._config.path_output_rec_result + f"{self.name}-it:{it + 1}.tsv")
示例#7
0
    def evaluate(self, it = None, loss = 0):
        if (it is None) or (not (it + 1) % self._validation_rate):
            recs = self.get_recommendations(self.evaluator.get_needed_recommendations())
            result_dict = self.evaluator.eval(recs)

            self._losses.append(loss)

            self._results.append(result_dict)

            if it is not None:
                self.logger.info(f'Epoch {(it + 1)}/{self._epochs} loss {loss/(it + 1):.5f}')
            else:
                self.logger.info(f'Finished')

            if (len(self._results) - 1) == self.get_best_arg():
                self.logger.info("******************************************")
                self.best_metric_value = self._results[-1][self._validation_k]["val_results"][self._validation_metric]
                if self._save_weights:
                    if hasattr(self, "_model"):
                        self._model.save_weights(self._saving_filepath)
                    else:
                        self.logger.warning("Saving weights FAILED. No model to save.")
                if self._save_recs:
                    if it is not None:
                        store_recommendation(recs[1], self._config.path_output_rec_result + f"{self.name}-it:{it + 1}.tsv")
                    else:
                        store_recommendation(recs[1], self._config.path_output_rec_result + f"{self.name}.tsv")
    def train(self):
        if self._restore:
            return self.restore_weights()

        best_metric_value = 0
        for it in range(1, self._epochs+1):
            user_adv_train = (self._epochs - it) < self._adversarial_epochs
            loss = 0
            steps = 0
            with tqdm(total=int(self._data.transactions // self._batch_size), disable=not self._verbose) as t:
                for batch in self._sampler.step(self._data.transactions, self._batch_size):
                    steps += 1
                    loss += self._model.train_step(batch, user_adv_train)
                    # t.set_postfix({'loss': f'{loss.numpy() / steps:.5f}'})
                    t.set_postfix({'(APR)-loss' if user_adv_train else '(BPR)-loss': f'{loss.numpy() / steps:.5f}'})
                    t.update()

            if not (it + 1) % self._validation_rate:
                recs = self.get_recommendations(self.evaluator.get_needed_recommendations())
                result_dict = self.evaluator.eval(recs)
                self._results.append(result_dict)

                print(f'Epoch {(it + 1)}/{self._epochs} loss {loss  / steps:.3f}')

                if self._results[-1][self._validation_k]["val_results"][self._validation_metric] > best_metric_value:
                    print("******************************************")
                    best_metric_value = self._results[-1][self._validation_k]["val_results"][self._validation_metric]
                    if self._save_weights:
                        self._model.save_weights(self._saving_filepath)
                    if self._save_recs:
                        store_recommendation(recs, self._config.path_output_rec_result + f"{self.name}-it:{it + 1}.tsv")
示例#9
0
    def train(self):
        if self._restore:
            return self.restore_weights()

        best_metric_value = 0
        for it in range(self._epochs):

            loss = 0
            steps = 0
            with tqdm(total=int(self._data.transactions * 2 //
                                self._batch_size),
                      disable=not self._verbose) as t:
                # update items and fix users
                for batch in self._sampler.step(self._data.transactions,
                                                self._batch_size):
                    steps += 1
                    self._model.set_update_user(False)
                    loss += self._model.train_step(batch)
                    t.set_postfix({'loss': f'{loss.numpy() / steps:.5f}'})
                    t.update()

                # update users and fix items
                for batch in self._sampler.step(self._data.transactions,
                                                self._batch_size):
                    steps += 1
                    self._model.set_update_user(True)
                    loss += self._model.train_step(batch)
                    t.set_postfix({'loss': f'{loss.numpy() / steps:.5f}'})
                    t.update()

            if not (it + 1) % self._validation_rate:
                recs = self.get_recommendations(
                    self.evaluator.get_needed_recommendations())
                result_dict = self.evaluator.eval(recs)
                self._results.append(result_dict)

                print(f'Epoch {(it + 1)}/{self._epochs} loss {loss/steps:.5f}')

                if self._results[-1][self._validation_k]["val_results"][
                        self._validation_metric] > best_metric_value:
                    print("******************************************")
                    best_metric_value = self._results[-1][self._validation_k][
                        "val_results"][self._validation_metric]
                    if self._save_weights:
                        self._model.save_weights(self._saving_filepath)
                    if self._save_recs:
                        store_recommendation(
                            recs, self._config.path_output_rec_result +
                            f"{self.name}-it:{it + 1}.tsv")
    def train(self):
        if self._restore:
            return self.restore_weights()

        self._model.train(self._verbose)

        recs = self.get_recommendations(self.evaluator.get_needed_recommendations())
        result_dict = self.evaluator.eval(recs)
        self._results.append(result_dict)

        print("******************************************")
        if self._save_weights:
            with open(self._saving_filepath, "wb") as f:
                pickle.dump(self._model.get_model_state(), f)
        if self._save_recs:
            store_recommendation(recs, self._config.path_output_rec_result + f"{self.name}.tsv")
示例#11
0
    def train(self):
        if self._restore:
            return self.restore_weights()

        best_metric_value = 0
        self._update_count = 0

        for it in range(self._epochs):
            loss = 0
            steps = 0
            with tqdm(total=int(self._num_users // self._batch_size),
                      disable=not self._verbose) as t:
                for batch in self._sampler.step(self._num_users,
                                                self._batch_size):
                    steps += 1

                    if self._total_anneal_steps > 0:
                        anneal = min(
                            self._anneal_cap,
                            1. * self._update_count / self._total_anneal_steps)
                    else:
                        anneal = self._anneal_cap

                    loss += self._model.train_step(batch, anneal)
                    t.set_postfix({'loss': f'{loss.numpy()/steps:.5f}'})
                    t.update()
                    self._update_count += 1

            if not (it + 1) % self._validation_rate:
                recs = self.get_recommendations(
                    self.evaluator.get_needed_recommendations())
                result_dict = self.evaluator.eval(recs)
                self._results.append(result_dict)

                print(f'Epoch {(it + 1)}/{self._epochs} loss {loss/steps:.5f}')

                if self._results[-1][self._validation_k]["val_results"][
                        self._validation_metric] > best_metric_value:
                    print("******************************************")
                    best_metric_value = self._results[-1][self._validation_k][
                        "val_results"][self._validation_metric]
                    if self._save_weights:
                        self._model.save_weights(self._saving_filepath)
                    if self._save_recs:
                        store_recommendation(
                            recs, self._config.path_output_rec_result +
                            f"{self.name}-it:{it + 1}.tsv")
示例#12
0
    def train(self):
        if self._restore:
            return self.restore_weights()

        best_metric_value = 0
        early_stopping = 5
        for it in range(self._epochs):
            loss = 0
            steps = 0
            with tqdm(total=int(self._data.transactions // self._batch_size),
                      disable=not self._verbose) as t:
                for batch in self._sampler.step(self._data.transactions,
                                                self._batch_size):
                    steps += 1
                    loss += self._model.train_step(batch)
                    t.set_postfix({'loss': f'{loss.numpy() / steps:.5f}'})
                    t.update()

            if not (it + 1) % self._validation_rate:
                recs = self.get_recommendations(
                    self.evaluator.get_needed_recommendations())
                result_dict = self.evaluator.eval(recs)
                self._results.append(result_dict)

                print(
                    f'Epoch {(it + 1)}/{self._epochs} loss {loss  / steps:.3f}'
                )

                if self._results[-1][self._validation_k]["val_results"][
                        self._validation_metric] > best_metric_value:
                    print("******************************************")
                    best_metric_value = self._results[-1][self._validation_k][
                        "val_results"][self._validation_metric]
                    early_stopping = 5
                    if self._save_weights:
                        self._model.save_weights(self._saving_filepath)
                    if self._save_recs:
                        store_recommendation(
                            recs, self._config.path_output_rec_result +
                            f"{self.name}-it:{it + 1}.tsv")
                else:
                    early_stopping -= 1
                    if early_stopping == 0:
                        print(
                            'Reached Early Stopping Condition at Epoch {0}\n\tEXIT'
                            .format(it + 1))
                        break
示例#13
0
文件: DVBPR.py 项目: kiminh/elliot
    def train(self):
        if self._restore:
            return self.restore_weights()

        best_metric_value = 0
        loss = 0
        steps = 0
        it = 0

        with tqdm(total=int(self._data.transactions // self._batch_size),
                  disable=not self._verbose) as t:
            for batch in self._next_batch:
                steps += 1
                loss += self._model.train_step(batch)
                t.set_postfix({'loss': f'{loss.numpy() / steps:.5f}'})
                t.update()

                # epoch is over
                if steps == self._data.transactions // self._batch_size:
                    t.reset()
                    if not (it + 1) % self._validation_rate:
                        recs = self.get_recommendations(
                            self.evaluator.get_needed_recommendations())
                        result_dict = self.evaluator.eval(recs)
                        self._results.append(result_dict)

                        self.logger.info(
                            f'Epoch {(it + 1)}/{self._epochs} loss {loss / steps:.3f}'
                        )

                        if self._results[-1][self._validation_k]["val_results"][
                                self._validation_metric] > best_metric_value:
                            best_metric_value = self._results[-1][
                                self._validation_k]["val_results"][
                                    self._validation_metric]
                            if self._save_weights:
                                self._model.save_weights(self._saving_filepath)
                            if self._save_recs:
                                store_recommendation(
                                    recs, self._config.path_output_rec_result +
                                    f"{self.name}-it:{it + 1}.tsv")
                    it += 1
                    steps = 0
                    loss = 0
    def restore_weights(self):
        try:
            with open(self._saving_filepath, "rb") as f:
                self._model.set_model_state(pickle.load(f))
            print(f"Model correctly Restored")

            recs = self.get_recommendations(self.evaluator.get_needed_recommendations())
            result_dict = self.evaluator.eval(recs)
            self._results.append(result_dict)

            print("******************************************")
            if self._save_recs:
                store_recommendation(recs, self._config.path_output_rec_result + f"{self.name}.tsv")
            return True

        except Exception as ex:
            print(f"Error in model restoring operation! {ex}")

        return False
示例#15
0
    def train(self):
        if self._restore:
            return self.restore_weights()

        best_metric_value = 0
        for it in range(self._epochs):
            loss = 0
            steps = 0
            with tqdm(total=int(self._data.transactions // self._batch_size),
                      disable=not self._verbose) as t:
                for batch in self._sampler.step(self._data.transactions,
                                                self._batch_size):
                    steps += 1
                    loss += self._model.train_step(batch)
                    t.set_postfix({'loss': f'{loss / steps:.5f}'})
                    t.update()

            if not (it + 1) % self._validation_rate:

                print("Computing recommendations..")
                recs = self.get_recommendations(
                    self.evaluator.get_needed_recommendations())
                result_dict = self.evaluator.eval(recs)
                self._results.append(result_dict)
                print(f'Finished')

                print(
                    f'Epoch {(it + 1)}/{self._epochs} loss {loss  / steps:.3f}'
                )

                if self._results[-1][self._validation_k]["val_results"][
                        self._validation_metric] > best_metric_value:
                    print("******************************************")
                    best_metric_value = self._results[-1][self._validation_k][
                        "val_results"][self._validation_metric]
                    if self._save_weights:
                        with open(self._saving_filepath, "wb") as f:
                            pickle.dump(self._model.get_model_state(), f)
                    if self._save_recs:
                        store_recommendation(
                            recs, self._config.path_output_rec_result +
                            f"{self.name}-it:{it + 1}.tsv")