Skip to content

Commit

Permalink
Merge pull request #119 from Hongbo-Miao/fix
Browse files Browse the repository at this point in the history
fix: add missing raise
  • Loading branch information
weihua916 authored Mar 19, 2021
2 parents fd9df92 + 96dbd2c commit 9c28d0e
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 49 deletions.
18 changes: 9 additions & 9 deletions examples/graphproppred/code2/conv.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ def __init__(self, emb_dim):

self.mlp = torch.nn.Sequential(torch.nn.Linear(emb_dim, 2*emb_dim), torch.nn.BatchNorm1d(2*emb_dim), torch.nn.ReLU(), torch.nn.Linear(2*emb_dim, emb_dim))
self.eps = torch.nn.Parameter(torch.Tensor([0]))

# edge_attr is two dimensional after augment_edge transformation
self.edge_encoder = torch.nn.Linear(2, emb_dim)

def forward(self, x, edge_index, edge_attr):
edge_embedding = self.edge_encoder(edge_attr)
edge_embedding = self.edge_encoder(edge_attr)
out = self.mlp((1 + self.eps) *x + self.propagate(edge_index, x=x, edge_attr=edge_embedding))

return out

def message(self, x_j, edge_attr):
return F.relu(x_j + edge_attr)

def update(self, aggr_out):
return aggr_out

Expand Down Expand Up @@ -102,8 +102,8 @@ def __init__(self, num_layer, emb_dim, node_encoder, drop_ratio = 0.5, JK = "las
elif gnn_type == 'gcn':
self.convs.append(GCNConv(emb_dim))
else:
ValueError('Undefined GNN type called {}'.format(gnn_type))
raise ValueError('Undefined GNN type called {}'.format(gnn_type))

self.batch_norms.append(torch.nn.BatchNorm1d(emb_dim))

def forward(self, batched_data):
Expand Down Expand Up @@ -181,7 +181,7 @@ def __init__(self, num_layer, emb_dim, node_encoder, drop_ratio = 0.5, JK = "las
elif gnn_type == 'gcn':
self.convs.append(GCNConv(emb_dim))
else:
ValueError('Undefined GNN type called {}'.format(gnn_type))
raise ValueError('Undefined GNN type called {}'.format(gnn_type))

self.batch_norms.append(torch.nn.BatchNorm1d(emb_dim))

Expand Down Expand Up @@ -235,9 +235,9 @@ def forward(self, batched_data):
node_representation = 0
for layer in range(self.num_layer):
node_representation += h_list[layer]

return node_representation


if __name__ == "__main__":
pass
pass
18 changes: 9 additions & 9 deletions examples/graphproppred/mol/conv.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ def __init__(self, emb_dim):

self.mlp = torch.nn.Sequential(torch.nn.Linear(emb_dim, 2*emb_dim), torch.nn.BatchNorm1d(2*emb_dim), torch.nn.ReLU(), torch.nn.Linear(2*emb_dim, emb_dim))
self.eps = torch.nn.Parameter(torch.Tensor([0]))

self.bond_encoder = BondEncoder(emb_dim = emb_dim)

def forward(self, x, edge_index, edge_attr):
edge_embedding = self.bond_encoder(edge_attr)
edge_embedding = self.bond_encoder(edge_attr)
out = self.mlp((1 + self.eps) *x + self.propagate(edge_index, x=x, edge_attr=edge_embedding))

return out

def message(self, x_j, edge_attr):
return F.relu(x_j + edge_attr)

def update(self, aggr_out):
return aggr_out

Expand Down Expand Up @@ -99,8 +99,8 @@ def __init__(self, num_layer, emb_dim, drop_ratio = 0.5, JK = "last", residual =
elif gnn_type == 'gcn':
self.convs.append(GCNConv(emb_dim))
else:
ValueError('Undefined GNN type called {}'.format(gnn_type))
raise ValueError('Undefined GNN type called {}'.format(gnn_type))

self.batch_norms.append(torch.nn.BatchNorm1d(emb_dim))

def forward(self, batched_data):
Expand Down Expand Up @@ -177,7 +177,7 @@ def __init__(self, num_layer, emb_dim, drop_ratio = 0.5, JK = "last", residual =
elif gnn_type == 'gcn':
self.convs.append(GCNConv(emb_dim))
else:
ValueError('Undefined GNN type called {}'.format(gnn_type))
raise ValueError('Undefined GNN type called {}'.format(gnn_type))

self.batch_norms.append(torch.nn.BatchNorm1d(emb_dim))

Expand Down Expand Up @@ -231,9 +231,9 @@ def forward(self, batched_data):
node_representation = 0
for layer in range(self.num_layer):
node_representation += h_list[layer]

return node_representation


if __name__ == "__main__":
pass
pass
18 changes: 9 additions & 9 deletions examples/graphproppred/ppa/conv.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ def __init__(self, emb_dim):

self.mlp = torch.nn.Sequential(torch.nn.Linear(emb_dim, 2*emb_dim), torch.nn.BatchNorm1d(2*emb_dim), torch.nn.ReLU(), torch.nn.Linear(2*emb_dim, emb_dim))
self.eps = torch.nn.Parameter(torch.Tensor([0]))

self.edge_encoder = torch.nn.Linear(7, emb_dim)

def forward(self, x, edge_index, edge_attr):
edge_embedding = self.edge_encoder(edge_attr)
edge_embedding = self.edge_encoder(edge_attr)
out = self.mlp((1 + self.eps) *x + self.propagate(edge_index, x=x, edge_attr=edge_embedding))

return out

def message(self, x_j, edge_attr):
return F.relu(x_j + edge_attr)

def update(self, aggr_out):
return aggr_out

Expand Down Expand Up @@ -98,8 +98,8 @@ def __init__(self, num_layer, emb_dim, drop_ratio = 0.5, JK = "last", residual =
elif gnn_type == 'gcn':
self.convs.append(GCNConv(emb_dim))
else:
ValueError('Undefined GNN type called {}'.format(gnn_type))
raise ValueError('Undefined GNN type called {}'.format(gnn_type))

self.batch_norms.append(torch.nn.BatchNorm1d(emb_dim))

def forward(self, batched_data):
Expand Down Expand Up @@ -177,7 +177,7 @@ def __init__(self, num_layer, emb_dim, drop_ratio = 0.5, JK = "last", residual =
elif gnn_type == 'gcn':
self.convs.append(GCNConv(emb_dim))
else:
ValueError('Undefined GNN type called {}'.format(gnn_type))
raise ValueError('Undefined GNN type called {}'.format(gnn_type))

self.batch_norms.append(torch.nn.BatchNorm1d(emb_dim))

Expand Down Expand Up @@ -231,9 +231,9 @@ def forward(self, batched_data):
node_representation = 0
for layer in range(self.num_layer):
node_representation += h_list[layer]

return node_representation


if __name__ == "__main__":
pass
pass
18 changes: 9 additions & 9 deletions ogb/graphproppred/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ def __init__(self, name):
def _parse_and_check_input(self, input_dict):
if self.eval_metric == 'rocauc' or self.eval_metric == 'ap' or self.eval_metric == 'rmse' or self.eval_metric == 'acc':
if not 'y_true' in input_dict:
RuntimeError('Missing key of y_true')
raise RuntimeError('Missing key of y_true')
if not 'y_pred' in input_dict:
RuntimeError('Missing key of y_pred')
raise RuntimeError('Missing key of y_pred')

y_true, y_pred = input_dict['y_true'], input_dict['y_pred']

Expand Down Expand Up @@ -64,9 +64,9 @@ def _parse_and_check_input(self, input_dict):

elif self.eval_metric == 'F1':
if not 'seq_ref' in input_dict:
RuntimeError('Missing key of seq_ref')
raise RuntimeError('Missing key of seq_ref')
if not 'seq_pred' in input_dict:
RuntimeError('Missing key of seq_pred')
raise RuntimeError('Missing key of seq_pred')

seq_ref, seq_pred = input_dict['seq_ref'], input_dict['seq_pred']

Expand All @@ -75,7 +75,7 @@ def _parse_and_check_input(self, input_dict):

if not isinstance(seq_pred, list):
raise RuntimeError('seq_pred must be of type list')

if len(seq_ref) != len(seq_pred):
raise RuntimeError('Length of seq_true and seq_pred should be the same')

Expand Down Expand Up @@ -236,7 +236,7 @@ def _eval_F1(self, seq_ref, seq_pred):
precision_list = []
recall_list = []
f1_list = []

for l, p in zip(seq_ref, seq_pred):
label = set(l)
prediction = set(p)
Expand All @@ -248,7 +248,7 @@ def _eval_F1(self, seq_ref, seq_pred):
precision = true_positive / (true_positive + false_positive)
else:
precision = 0

if true_positive + false_negative > 0:
recall = true_positive / (true_positive + false_negative)
else:
Expand All @@ -257,11 +257,11 @@ def _eval_F1(self, seq_ref, seq_pred):
f1 = 2 * precision * recall / (precision + recall)
else:
f1 = 0

precision_list.append(precision)
recall_list.append(recall)
f1_list.append(f1)

return {'precision': np.average(precision_list),
'recall': np.average(recall_list),
'F1': np.average(f1_list)}
Expand Down
22 changes: 11 additions & 11 deletions ogb/linkproppred/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ def __init__(self, name):
def _parse_and_check_input(self, input_dict):
if 'hits@' in self.eval_metric:
if not 'y_pred_pos' in input_dict:
RuntimeError('Missing key of y_pred_pos')
raise RuntimeError('Missing key of y_pred_pos')
if not 'y_pred_neg' in input_dict:
RuntimeError('Missing key of y_pred_neg')
raise RuntimeError('Missing key of y_pred_neg')

y_pred_pos, y_pred_neg = input_dict['y_pred_pos'], input_dict['y_pred_neg']

Expand Down Expand Up @@ -86,9 +86,9 @@ def _parse_and_check_input(self, input_dict):
elif 'mrr' == self.eval_metric:

if not 'y_pred_pos' in input_dict:
RuntimeError('Missing key of y_pred_pos')
raise RuntimeError('Missing key of y_pred_pos')
if not 'y_pred_neg' in input_dict:
RuntimeError('Missing key of y_pred_neg')
raise RuntimeError('Missing key of y_pred_neg')

y_pred_pos, y_pred_neg = input_dict['y_pred_pos'], input_dict['y_pred_neg']

Expand Down Expand Up @@ -151,7 +151,7 @@ def eval(self, input_dict):
elif self.eval_metric == 'mrr':
y_pred_pos, y_pred_neg, type_info = self._parse_and_check_input(input_dict)
return self._eval_mrr(y_pred_pos, y_pred_neg, type_info)

else:
raise ValueError('Undefined eval metric %s' % (self.eval_metric))

Expand Down Expand Up @@ -187,10 +187,10 @@ def expected_output_format(self):
elif self.eval_metric == 'mrr':
desc += '{' + '\'hits@1_list\': hits@1_list, \'hits@3_list\': hits@3_list, \n\'hits@10_list\': hits@10_list, \'mrr_list\': mrr_list}\n'
desc += '- mrr_list (list of float): list of scores for calculating MRR \n'
desc += '- hits@1_list (list of float): list of scores for calculating Hits@1 \n'
desc += '- hits@1_list (list of float): list of scores for calculating Hits@1 \n'
desc += '- hits@3_list (list of float): list of scores to calculating Hits@3\n'
desc += '- hits@10_list (list of float): list of scores to calculating Hits@10\n'
desc += 'Note: i-th element corresponds to the prediction score for the i-th edge.\n'
desc += '- hits@10_list (list of float): list of scores to calculating Hits@10\n'
desc += 'Note: i-th element corresponds to the prediction score for the i-th edge.\n'
desc += 'Note: To obtain the final score, you need to concatenate the lists of scores and take average over the concatenated list.'
else:
raise ValueError('Undefined eval metric %s' % (self.eval_metric))
Expand Down Expand Up @@ -220,7 +220,7 @@ def _eval_hits(self, y_pred_pos, y_pred_neg, type_info):
hitsK = float(np.sum(y_pred_pos > kth_score_in_negative_edges)) / len(y_pred_pos)

return {'hits@{}'.format(self.K): hitsK}

def _eval_mrr(self, y_pred_pos, y_pred_neg, type_info):
'''
compute mrr
Expand All @@ -239,7 +239,7 @@ def _eval_mrr(self, y_pred_pos, y_pred_neg, type_info):
hits10_list = (ranking_list <= 10).to(torch.float)
mrr_list = 1./ranking_list.to(torch.float)

return {'hits@1_list': hits1_list,
return {'hits@1_list': hits1_list,
'hits@3_list': hits3_list,
'hits@10_list': hits10_list,
'mrr_list': mrr_list}
Expand All @@ -254,7 +254,7 @@ def _eval_mrr(self, y_pred_pos, y_pred_neg, type_info):
hits10_list = (ranking_list <= 10).astype(np.float32)
mrr_list = 1./ranking_list.astype(np.float32)

return {'hits@1_list': hits1_list,
return {'hits@1_list': hits1_list,
'hits@3_list': hits3_list,
'hits@10_list': hits10_list,
'mrr_list': mrr_list}
Expand Down
4 changes: 2 additions & 2 deletions ogb/nodeproppred/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ def __init__(self, name):
def _parse_and_check_input(self, input_dict):
if self.eval_metric == 'rocauc' or self.eval_metric == 'acc':
if not 'y_true' in input_dict:
RuntimeError('Missing key of y_true')
raise RuntimeError('Missing key of y_true')
if not 'y_pred' in input_dict:
RuntimeError('Missing key of y_pred')
raise RuntimeError('Missing key of y_pred')

y_true, y_pred = input_dict['y_true'], input_dict['y_pred']

Expand Down

0 comments on commit 9c28d0e

Please sign in to comment.