-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.py
166 lines (133 loc) · 4.57 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
''' Credit: https://github.com/fanyun-sun/InfoGraph '''
import torch
import torch as th
import torch.nn as nn
import torch.nn.functional as F
import math
def log_sum_exp(x, axis=None):
"""Log sum exp function
Args:
x: Input.
axis: Axis over which to perform sum.
Returns:
torch.Tensor: log sum exp
"""
x_max = torch.max(x, axis)[0]
y = torch.log((torch.exp(x - x_max)).sum(axis)) + x_max
return y
def raise_measure_error(measure):
supported_measures = ['GAN', 'JSD', 'X2', 'KL', 'RKL', 'DV', 'H2', 'W1']
raise NotImplementedError(
'Measure `{}` not supported. Supported: {}'.format(measure,
supported_measures))
def get_positive_expectation(p_samples, measure, average=True):
"""Computes the positive part of a divergence / difference.
Args:
p_samples: Positive samples.
measure: Measure to compute for.
average: Average the result over samples.
Returns:
torch.Tensor
"""
log_2 = math.log(2.)
if measure == 'GAN':
Ep = - F.softplus(-p_samples)
elif measure == 'JSD':
Ep = log_2 - F.softplus(- p_samples)
elif measure == 'X2':
Ep = p_samples ** 2
elif measure == 'KL':
Ep = p_samples + 1.
elif measure == 'RKL':
Ep = -torch.exp(-p_samples)
elif measure == 'DV':
Ep = p_samples
elif measure == 'H2':
Ep = 1. - torch.exp(-p_samples)
elif measure == 'W1':
Ep = p_samples
else:
raise_measure_error(measure)
if average:
return Ep.mean()
else:
return Ep
def get_negative_expectation(q_samples, measure, average=True):
"""Computes the negative part of a divergence / difference.
Args:
q_samples: Negative samples.
measure: Measure to compute for.
average: Average the result over samples.
Returns:
torch.Tensor
"""
log_2 = math.log(2.)
if measure == 'GAN':
Eq = F.softplus(-q_samples) + q_samples
elif measure == 'JSD':
Eq = F.softplus(-q_samples) + q_samples - log_2
elif measure == 'X2':
Eq = -0.5 * ((torch.sqrt(q_samples ** 2) + 1.) ** 2)
elif measure == 'KL':
Eq = torch.exp(q_samples)
elif measure == 'RKL':
Eq = q_samples - 1.
elif measure == 'DV':
Eq = log_sum_exp(q_samples, 0) - math.log(q_samples.size(0))
elif measure == 'H2':
Eq = torch.exp(q_samples) - 1.
elif measure == 'W1':
Eq = q_samples
else:
raise_measure_error(measure)
if average:
return Eq.mean()
else:
return Eq
def local_global_loss_(l_enc, g_enc, graph_id, measure):
num_graphs = g_enc.shape[0]
num_nodes = l_enc.shape[0]
device = g_enc.device
pos_mask = th.zeros((num_nodes, num_graphs)).to(device)
neg_mask = th.ones((num_nodes, num_graphs)).to(device)
for nodeidx, graphidx in enumerate(graph_id):
pos_mask[nodeidx][graphidx] = 1.
neg_mask[nodeidx][graphidx] = 0.
res = th.mm(l_enc, g_enc.t())
E_pos = get_positive_expectation(res * pos_mask, measure, average=False).sum()
E_pos = E_pos / num_nodes
E_neg = get_negative_expectation(res * neg_mask, measure, average=False).sum()
E_neg = E_neg / (num_nodes * (num_graphs - 1))
return E_neg - E_pos
def global_global_loss_(sup_enc, unsup_enc, measure):
'''
Args:
g: Global features
g1: Global features.
measure: Type of f-divergence. For use with mode `fd`
mode: Loss mode. Fenchel-dual `fd`, NCE `nce`, or Donsker-Vadadhan `dv`.
Returns:
torch.Tensor: Loss.
'''
num_graphs = sup_enc.shape[0]
device = sup_enc.device
pos_mask = th.eye(num_graphs).to(device)
neg_mask = 1 - pos_mask
res = th.mm(sup_enc, unsup_enc.t())
E_pos = get_positive_expectation(res * pos_mask, measure, average=False)
E_pos = (E_pos * pos_mask).sum() / pos_mask.sum()
E_neg = get_negative_expectation(res * neg_mask, measure, average=False)
E_neg = (E_neg * neg_mask).sum() / neg_mask.sum()
return E_neg - E_pos
def adj_loss_(l_enc, g_enc, edge_index, batch):
num_graphs = g_enc.shape[0]
num_nodes = l_enc.shape[0]
adj = torch.zeros((num_nodes, num_nodes)).cuda()
mask = torch.eye(num_nodes).cuda()
for node1, node2 in zip(edge_index[0], edge_index[1]):
adj[node1.item()][node2.item()] = 1.
adj[node2.item()][node1.item()] = 1.
res = torch.sigmoid((torch.mm(l_enc, l_enc.t())))
res = (1 - mask) * res
loss = nn.BCELoss()(res, adj)
return loss