Пример #1
0
    def train(self):
        """
        Method for:
        1. training the embedding.
        2. logging.
        This method is inherited by GEMSEC and DeepWalk variants without an override.
        """
        self.current_step = 0
        self.current_gamma = self.args.initial_gamma
        self.log = log_setup(self.args)
        with tf.Session(graph=self.computation_graph) as session:
            self.init.run()
            print("Model Initialized.")
            for repetition in range(self.args.num_of_walks):

                random.shuffle(self.nodes)
                self.optimization_time = 0
                self.average_loss = 0

                epoch_printer(repetition)

                for node in tqdm(self.nodes):
                    self.current_step = self.current_step + 1
                    self.current_gamma = gamma_incrementer(
                        self.current_step, self.args.initial_gamma,
                        self.args.final_gamma, self.current_gamma,
                        self.true_step_size)

                    feed_dict = self.feed_dict_generator(
                        self.walks[self.current_step - 1], self.current_step,
                        self.current_gamma)
                    start = time.time()
                    _, loss = session.run([self.train_op, self.loss],
                                          feed_dict=feed_dict)
                    end = time.time()
                    self.optimization_time = self.optimization_time + (end -
                                                                       start)
                    self.average_loss = self.average_loss + loss

                print("")
                self.average_loss = self.average_loss / self.vocab_size
                self.final_embeddings = self.walker_layer.embedding_matrix.eval(
                )
                if "GEMSEC" in self.args.model:
                    self.c_means = self.cluster_layer.cluster_means.eval()
                    self.modularity_score, assignments = neural_modularity_calculator(
                        self.graph, self.final_embeddings, self.c_means)
                else:
                    self.modularity_score, assignments = classical_modularity_calculator(
                        self.graph, self.final_embeddings, self.args)
                self.log = log_updater(self.log, repetition, self.average_loss,
                                       self.optimization_time,
                                       self.modularity_score)
                tab_printer(self.log)
        if "GEMSEC" in self.args.model:
            initiate_dump_gemsec(self.log, assignments, self.args,
                                 self.final_embeddings, self.c_means)
        else:
            initiate_dump_dw(self.log, assignments, self.args,
                             self.final_embeddings)
Пример #2
0
    def train(self):
        """
        Method for training the embedding, logging.
        """
        self.current_step = 0
        self.log = log_setup(self.args)

        with tf.Session(graph=self.computation_graph) as session:
            self.init.run()
            print("Model Initialized.")
            for repetition in range(0, self.args.epochs):

                random.shuffle(self.nodes)
                self.optimization_time = 0
                self.average_loss = 0

                epoch_printer(repetition)
                for i in tqdm(range(0,
                                    len(self.nodes) / self.args.batch_size)):
                    self.current_step = self.current_step + 1
                    feed_dict = self.feed_dict_generator(
                        self.nodes[i * self.args.batch_size:(i + 1) *
                                   self.args.batch_size], self.current_step)
                    start = time.time()
                    _, loss = session.run([self.train_op, self.loss],
                                          feed_dict=feed_dict)
                    end = time.time()
                    self.optimization_time = self.optimization_time + (end -
                                                                       start)
                    self.average_loss = self.average_loss + loss

                print("")
                self.average_loss = (self.average_loss / i)
                self.log = log_updater(self.log, repetition, self.average_loss,
                                       self.optimization_time)
                tab_printer(self.log)
            self.features = self.factorization_layer.embedding_node.eval()
            data_saver(self.features, self.args.embedding_output)