forked from mtiezzi/torch_gnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet.py
139 lines (118 loc) · 4.56 KB
/
net.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import typing
class MLP(nn.Module):
def __init__(self, input_dim, hidden_sizes: typing.Iterable[int], out_dim, activation_function=nn.Sigmoid(),
activation_out=None):
super(MLP, self).__init__()
i_h_sizes = [input_dim] + hidden_sizes # add input dim to the iterable
self.mlp = nn.Sequential()
for idx in range(len(i_h_sizes) - 1):
self.mlp.add_module("layer_{}".format(idx),
nn.Linear(in_features=i_h_sizes[idx], out_features=i_h_sizes[idx + 1]))
self.mlp.add_module("act", activation_function)
self.mlp.add_module("out_layer", nn.Linear(i_h_sizes[-1], out_dim))
if activation_out is not None:
self.mlp.add_module("out_layer_activation", activation_out)
def init(self):
for i, l in enumerate(self.mlp):
if type(l) == nn.Linear:
nn.init.xavier_normal_(l.weight)
def forward(self, x):
return self.mlp(x)
# code from Pedro H. Avelar
class StateTransition(nn.Module):
def __init__(self,
node_state_dim: int,
node_label_dim: int,
mlp_hidden_dim: typing.Iterable[int],
activation_function=nn.Tanh()
):
super(type(self), self).__init__()
d_i = node_state_dim + 2 * node_label_dim # arc state computation f(l_v, l_n, x_n)
d_o = node_state_dim
d_h = list(mlp_hidden_dim) # if already a list, no change
self.mlp = MLP(input_dim=d_i, hidden_sizes=d_h, out_dim=d_o, activation_function=activation_function,
activation_out=activation_function) # state transition function, non-linearity also in output
def forward(
self,
node_states,
node_labels,
edges,
agg_matrix,
):
src_label = node_labels[edges[:, 0]]
tgt_label = node_labels[edges[:, 1]]
tgt_state = node_states[edges[:, 1]]
edge_states = self.mlp(
torch.cat(
[src_label, tgt_label, tgt_state],
-1
)
)
new_state = torch.matmul(agg_matrix, edge_states)
return new_state
# Integrated from layers.py 25.12.20 Faezeh
class GINTransition(nn.Module):
def __init__(self,
node_state_dim: int,
node_label_dim: int,
mlp_hidden_dim: typing.Iterable[int],
activation_function=nn.Tanh()
):
super(type(self), self).__init__()
d_i = node_state_dim + node_label_dim
d_o = node_state_dim
d_h = list(mlp_hidden_dim)
self.mlp = MLP(input_dim=d_i, hidden_sizes=d_h, out_dim=d_o, activation_function=activation_function,
activation_out=activation_function) # non-linearity also in output
def forward(
self,
node_states,
node_labels,
edges,
agg_matrix,
):
state_and_label = torch.cat(
[node_states, node_labels],
-1
)
#print(edges[0])
#print(state_and_label.size())
aggregated_neighbourhood = torch.matmul(agg_matrix, state_and_label[edges[:, 1]])
#print(aggregated_neighbourhood.size())
node_plus_neighbourhood = state_and_label + aggregated_neighbourhood
new_state = self.mlp(node_plus_neighbourhood)
return new_state
class GINPreTransition(nn.Module):
def __init__(self,
node_state_dim: int,
node_label_dim: int,
mlp_hidden_dim: typing.Iterable[int],
activation_function=nn.Tanh()
):
super(type(self), self).__init__()
d_i = node_state_dim + node_label_dim
d_o = node_state_dim
d_h = list(mlp_hidden_dim)
self.mlp = MLP(input_dim=d_i, hidden_sizes=d_h, out_dim=d_o, activation_function=activation_function,
activation_out=activation_function) # non-linearity also in output
def forward(
self,
node_states,
node_labels,
edges,
agg_matrix,
):
intermediate_states = self.mlp(
torch.cat(
[node_states, node_labels],
-1
)
)
new_state = (
torch.matmul(agg_matrix, intermediate_states[edges[:, 0]])
+ torch.matmul(agg_matrix, intermediate_states[edges[:, 1]])
)
return new_state