Exemplo n.º 1
0
    def process_step(self):
        graph = self.graph
        adj_matrix = self.adj_transform(graph.adj_matrix)
        attr_matrix = self.attr_transform(graph.attr_matrix)

        feature_inputs, structure_inputs = T.astensors(attr_matrix,
                                                       adj_matrix,
                                                       device=self.device)

        if self.kind == "T":
            # To avoid this tensorflow error in large dataset:
            # InvalidArgumentError: Cannot use GPU when output.shape[1] * nnz(a) > 2^31 [Op:SparseTensorDenseMatMul]
            if self.graph.n_attrs * adj_matrix.nnz > 2**31:
                device = "CPU"
            else:
                device = self.device

            with tf.device(device):
                feature_inputs = tfSGConvolution(order=self.order)(
                    [feature_inputs, structure_inputs])

            with tf.device(self.device):
                self.feature_inputs, self.structure_inputs = feature_inputs, structure_inputs
        else:
            feature_inputs = pySGConvolution(order=self.order)(
                [feature_inputs, structure_inputs])
            self.feature_inputs, self.structure_inputs = feature_inputs, structure_inputs
Exemplo n.º 2
0
    def process_step(self):
        graph = self.graph
        adj_matrix = self.adj_transform(graph.adj_matrix)
        attr_matrix = self.attr_transform(graph.attr_matrix)

        self.feature_inputs, self.structure_inputs = T.astensors(
            attr_matrix, adj_matrix, device=self.device)
Exemplo n.º 3
0
    def process_step(self):
        graph = self.graph
        adj_matrix = self.adj_transform(graph.adj_matrix)
        attr_matrix = self.attr_transform(graph.attr_matrix)
        edge_index, edge_weight = T.sparse_adj_to_sparse_edges(adj_matrix)

        self.feature_inputs, self.structure_inputs = T.astensors(
            attr_matrix, (edge_index.T, edge_weight), device=self.device)
Exemplo n.º 4
0
    def process_step(self):
        graph = self.graph
        attr_matrix = self.attr_transform(graph.attr_matrix)

        batch_adj, batch_x, self.cluster_member = T.graph_partition(
            graph.adj_matrix, attr_matrix, n_clusters=self.n_clusters)

        batch_adj = self.adj_transform(*batch_adj)

        (self.batch_adj, self.batch_x) = T.astensors(batch_adj,
                                                     batch_x,
                                                     device=self.device)
Exemplo n.º 5
0
    def process_step(self):
        graph = self.graph
        # Dense matrix, shape [n_nodes, max_degree]
        adj_matrix = self.adj_transform(graph.adj_matrix)
        attr_matrix = self.attr_transform(graph.attr_matrix)

        # pad with a dummy zero vector
        attr_matrix = np.vstack(
            [attr_matrix, np.zeros(attr_matrix.shape[1], dtype=self.floatx)])

        self.feature_inputs, self.structure_inputs = T.astensors(
            attr_matrix, device=self.device), adj_matrix
Exemplo n.º 6
0
    def predict(self, index):

        mask = T.indices2mask(index, self.graph.n_nodes)

        orders_dict = {idx: order for order, idx in enumerate(index)}
        batch_idx, orders = [], []
        batch_x, batch_adj = [], []
        for cluster in range(self.n_clusters):
            nodes = self.cluster_member[cluster]
            mini_mask = mask[nodes]
            batch_nodes = np.asarray(nodes)[mini_mask]
            if batch_nodes.size == 0:
                continue
            batch_x.append(self.batch_x[cluster])
            batch_adj.append(self.batch_adj[cluster])
            batch_idx.append(np.where(mini_mask)[0])
            orders.append([orders_dict[n] for n in batch_nodes])

        batch_data = tuple(zip(batch_x, batch_adj, batch_idx))

        logit = np.zeros((index.size, self.graph.n_classes), dtype=self.floatx)
        batch_data = T.astensors(batch_data, device=self.device)

        model = self.model
        if self.kind == "P":
            model.eval()
            with torch.no_grad():
                for order, inputs in zip(orders, batch_data):
                    output = model(inputs).detach().cpu().numpy()
                    logit[order] = output
        else:
            with tf.device(self.device):
                for order, inputs in zip(orders, batch_data):
                    output = model.predict_on_batch(inputs)
                    logit[order] = output

        return logit
Exemplo n.º 7
0
    def train(self,
              idx_train,
              idx_val=None,
              pre_train_epochs=100,
              epochs=100,
              early_stopping=None,
              verbose=None,
              save_best=True,
              weight_path=None,
              as_model=False,
              monitor='val_acc',
              early_stop_metric='val_loss'):

        histories = []
        index_all = tf.range(self.graph.n_nodes, dtype=self.intx)

        # pre train model_q
        self.model = self.model_q
        history = super().train(idx_train,
                                idx_val,
                                epochs=pre_train_epochs,
                                early_stopping=early_stopping,
                                verbose=verbose,
                                save_best=save_best,
                                weight_path=weight_path,
                                as_model=True,
                                monitor=monitor,
                                early_stop_metric=early_stop_metric)
        histories.append(history)

        label_predict = self.predict(index_all).argmax(1)
        label_predict[idx_train] = self.graph.labels[idx_train]
        label_predict = tf.one_hot(label_predict, depth=self.graph.n_classes)
        # train model_p fitst
        train_sequence = FullBatchNodeSequence(
            [label_predict, self.structure_inputs, index_all],
            label_predict,
            device=self.device)
        if idx_val is not None:
            val_sequence = FullBatchNodeSequence(
                [label_predict, self.structure_inputs, idx_val],
                self.labels_onehot[idx_val],
                device=self.device)
        else:
            val_sequence = None

        self.model = self.model_p
        history = super().train(train_sequence,
                                val_sequence,
                                epochs=epochs,
                                early_stopping=early_stopping,
                                verbose=verbose,
                                save_best=save_best,
                                weight_path=weight_path,
                                as_model=as_model,
                                monitor=monitor,
                                early_stop_metric=early_stop_metric)
        histories.append(history)

        # then train model_q again
        label_predict = self.model.predict_on_batch(
            T.astensors(label_predict,
                        self.structure_inputs,
                        index_all,
                        device=self.device))

        label_predict = softmax(label_predict)
        if tf.is_tensor(label_predict):
            label_predict = label_predict.numpy()

        label_predict[idx_train] = self.labels_onehot[idx_train]

        self.model = self.model_q
        train_sequence = FullBatchNodeSequence(
            [self.feature_inputs, self.structure_inputs, index_all],
            label_predict,
            device=self.device)
        history = super().train(train_sequence,
                                idx_val,
                                epochs=epochs,
                                early_stopping=early_stopping,
                                verbose=verbose,
                                save_best=save_best,
                                weight_path=weight_path,
                                as_model=as_model,
                                monitor=monitor,
                                early_stop_metric=early_stop_metric)

        histories.append(history)

        return histories