Beispiel #1
0
def validate(z, pos_pred, neg_pred, pos_edge_index, neg_edge_index):
    r"""Given latent variables :obj:`z`, positive edges
    :obj:`pos_edge_index` and negative edges :obj:`neg_edge_index`,
    computes area under the ROC curve (AUC) and average precision (AP)
    scores.

    Args:
        z (Tensor): The latent space :math:`\mathbf{Z}`.
        pos_edge_index (LongTensor): The positive edges to evaluate
            against.
        neg_edge_index (LongTensor): The negative edges to evaluate
            against.
    """
    decoder = InnerProductDecoder()

    pos_y = z.new_ones(pos_edge_index.size(1))
    neg_y = z.new_zeros(neg_edge_index.size(1))
    y = torch.cat([pos_y, neg_y], dim=0)

    pos_pred = decoder(z, pos_edge_index, sigmoid=True)
    neg_pred = decoder(z, neg_edge_index, sigmoid=True)
    pred = torch.cat([pos_pred, neg_pred], dim=0)

    y, pred = y.detach().cpu().numpy(), pred.detach().cpu().numpy()

    return roc_auc_score(y, pred), average_precision_score(y, pred)
Beispiel #2
0
def recon_loss(z, pos_edge_index, neg_edge_index=None):
    r"""Given latent variables :obj:`z`, computes the binary cross
    entropy loss for positive edges :obj:`pos_edge_index` and negative
    sampled edges.
    Args:
        z (Tensor): The latent space :math:`\mathbf{Z}`.
        pos_edge_index (LongTensor): The positive edges to train against.
        neg_edge_index (LongTensor, optional): The negative edges to train
            against. If not given, uses negative sampling to calculate
            negative edges. (default: :obj:`None`)
    """
    EPS = 1e-15
    decoder = InnerProductDecoder()

    pos_loss = -torch.log(decoder(z, pos_edge_index, sigmoid=True) +
                          EPS).mean()

    # Do not include self-loops in negative samples
    pos_edge_index, _ = remove_self_loops(pos_edge_index)
    pos_edge_index, _ = add_self_loops(pos_edge_index)
    if neg_edge_index is None:
        neg_edge_index = negative_sampling(pos_edge_index, z.size(0))
    neg_loss = -torch.log(1 - decoder(z, neg_edge_index, sigmoid=True) +
                          EPS).mean()

    return pos_loss + neg_loss
Beispiel #3
0
    def fit_model_once(self):

        #GCN
        if self.model_type == "GCN":
            encoder = EncoderGCN(in_channels=self.dataset.num_features,
                                 out_channels=32)

        #SAGE
        if self.model_type == "SAGE":
            encoder = EncoderSAGE(in_channels=self.dataset.num_features,
                                  out_channels=32)

        #GIN
        if self.model_type == "GIN":
            encoder = EncoderGIN(in_channels=self.dataset.num_features,
                                 out_channels=32)

        #GAT
        if self.model_type == "GAT":
            encoder = EncoderGAT(in_channels=self.dataset.num_features,
                                 out_channels=16,
                                 heads=8)

        #AGNN
        if self.model_type == "AGNN":
            encoder = EncoderAGNN(in_channels=self.dataset.num_features,
                                  out_channels=16)

        #GraphUNet
        if self.model_type == "GraphUNet":
            encoder = EncoderGraphUNet(in_channels=self.dataset.num_features,
                                       hidden_channels=32,
                                       out_channels=16)

        model = GAE(encoder=encoder, decoder=InnerProductDecoder())

        optimizer = torch.optim.Adam(model.parameters(), lr=0.01)

        trainer = TrainerGae(model,
                             self.device,
                             self.data,
                             writer_path='runs/{}/{}/'.format(
                                 self.model_type, self.text_encoding) +
                             self.time_mark())
        model = trainer.fit(optimizer,
                            patience=self.patience,
                            num_epochs=self.num_epochs)

        auc, ap = trainer.evaluate(validation=False, test=True)
        return model, auc, ap
Beispiel #4
0
    def fit_model_once(self):
        gcn_model = GAE(encoder=EncoderGCN(
            in_channels=self.dataset.num_features, out_channels=32),
                        decoder=InnerProductDecoder())

        optimizer = torch.optim.Adam(gcn_model.parameters(), lr=0.01)

        trainer_gcn = TrainerGae(
            gcn_model,
            self.device,
            self.data,
            writer_path='runs/gae_gcn/{}/'.format(self.feature) +
            self.time_mark())
        gcn_model = trainer_gcn.fit(optimizer,
                                    patience=self.patience,
                                    num_epochs=self.num_epochs)

        auc, ap = trainer_gcn.evaluate(validation=False, test=True)
        return auc, ap
Beispiel #5
0
 def __init__(self, encoder, decoder=None):
     super(MyGAE, self).__init__()
     self.encoder = encoder
     self.decoder = InnerProductDecoder() if decoder is None else decoder
Beispiel #6
0
 def __init__(self, args):
     super(DeepVGAE, self).__init__(encoder=GCNEncoder(args.enc_in_channels,
                                                       args.enc_hidden_channels,
                                                       args.enc_out_channels),
                                    decoder=InnerProductDecoder())
Beispiel #7
0
 def __init__(self, encoder, decoder=None):
     super(GAEwithK, self).__init__()
     self.encoder = encoder
     self.decoder = InnerProductDecoder() if decoder is None else decoder
     GAEwithK.reset_parameters(self)
Beispiel #8
0
 def reset_parameters(self):
     self.encoder.reset_parameters()
     # print('type(self.decoder)',type(self.decoder))
     if type(self.decoder) != type(InnerProductDecoder()):
         self.decoder.reset_parameters()