-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModels
145 lines (102 loc) · 4.22 KB
/
Models
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
import torch.nn as nn
from torch_geometric.nn import Linear, GCNConv, GATv2Conv, GINEConv, GraphSAGE, SAGEConv, global_mean_pool
import torch.nn.functional as F
import torch
class GCN(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim, dropout_rate):
super(GCN, self).__init__()
self.dropout_rate = dropout_rate
self.gcn1 = GCNConv(input_dim, hidden_dim)
self.gcn2 = GCNConv(hidden_dim, hidden_dim)
self.lin = Linear(hidden_dim, output_dim)
self.init_weights()
def init_weights(self):
for m in self.modules():
if isinstance(m, GCNConv) or isinstance(m, Linear):
m.reset_parameters()
def forward(self, data):
x, edge_index = data.x, data.edge_index
x = self.gcn1(x, edge_index)
x = F.relu(x)
x = self.gcn2(x, edge_index)
x = F.relu(x)
x = global_mean_pool(x, data.batch)
x = F.dropout(x, self.dropout_rate)
x = self.lin(x)
return x.log_softmax(dim=-1)
class GAT(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim, heads, dropout_rate):
super(GAT, self).__init__()
self.dropout_rate = dropout_rate
self.conv1 = GATv2Conv(input_dim, hidden_dim, heads=heads)
self.conv2 = GATv2Conv(hidden_dim * heads, hidden_dim, heads=heads)
self.lin = nn.Linear(hidden_dim * heads, output_dim)
self.init_weights()
def init_weights(self):
for m in self.modules():
if isinstance(m, GCNConv) or isinstance(m, Linear):
m.reset_parameters()
def forward(self, data):
x, edge_index = data.x, data.edge_index
x = self.conv1(x, edge_index)
x = F.relu(x)
x = self.conv2(x, edge_index)
x = F.relu(x)
x = global_mean_pool(x, data.batch)
x = F.dropout(x, self.dropout_rate)
x = self.lin(x)
return x.log_softmax(dim=-1)
class GraphSAGE(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim, dropout_rate):
super(GraphSAGE, self).__init__()
self.dropout_rate = dropout_rate
self.sage1 = SAGEConv(input_dim, hidden_dim)
self.sage2 = SAGEConv(hidden_dim, hidden_dim)
self.lin = Linear(hidden_dim, output_dim)
self.init_weights()
def init_weights(self):
for m in self.modules():
if isinstance(m, GCNConv) or isinstance(m, Linear):
m.reset_parameters()
def forward(self, data):
x, edge_index = data.x, data.edge_index
x = self.sage1(x, edge_index)
x = F.relu(x)
x = self.sage2(x, edge_index)
x = F.relu(x)
x = global_mean_pool(x, data.batch)
x = F.dropout(x, self.dropout_rate)
x = self.lin(x)
return x.log_softmax(dim=-1)
class LSTM(nn.Module):
def __init__(self, embedding_dim, hidden_dim, num_layers=1):
super(LSTM, self).__init__()
self.num_layers = num_layers
self.hidden_dim = hidden_dim
self.lstm = nn.LSTM(embedding_dim, hidden_dim, num_layers, batch_first=True)
# x -> (batch_size, seq, input_size)
# self.fc = nn.Linear(hidden_dim, output_dim)
# self.fc = nn.Linear(hidden_dim*sequence_length, output_dim)
def forward(self, x):
h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_dim)
c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_dim) # hidden state
out, _ = self.lstm(x, (h0, c0))
out = out[:, -1, :]
# out: batch_size, seq_length, hidden_size
# out = self.fc(out)
return out
class GRU(nn.Module):
def __init__(self, embedding_dim, hidden_dim, num_layers=1):
super(GRU, self).__init__()
self.num_layers = num_layers
self.hidden_dim = hidden_dim
self.gru = nn.GRU(embedding_dim, hidden_dim, num_layers=num_layers, batch_first=True)
# x -> (batch_size, seq, input_size)
# self.fc = nn.Linear(hidden_dim, output_dim)
def forward(self, x):
h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_dim)
out, _ = self.gru(x, h0)
out = out[:, -1, :]
# out: batch_size, seq_length, hidden_size
# out = self.fc(out)
return out