-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
189 lines (144 loc) · 7.49 KB
/
models.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# -*- coding: utf-8 -*-
"""
# @time : 05.05.22 14:11
# @author : zhouzy
# @file : models.py
"""
import torch
import torch.nn as nn
from torch_geometric.nn import GCNConv, SAGEConv, GATConv, Linear, GraphNorm, BatchNorm, LayerNorm, GraphSizeNorm, InstanceNorm, TopKPooling
import torch.nn.functional as F
from torch_geometric.utils import dense_to_sparse, to_dense_adj
torch.Generator().manual_seed(0)
import numpy as np
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
class NodeClsGCN(nn.Module):
def __init__(self, in_channels, hidden_dims, out_channels, dropout):
super(NodeClsGCN, self).__init__()
self.gc1 = GCNConv(in_channels, hidden_dims[0])
self.gc2 = GCNConv(hidden_dims[0], hidden_dims[1])
self.gc3 = GCNConv(hidden_dims[1], out_channels)
self.dropout = dropout
def forward(self, x, edge_index):
x = F.relu(self.gc1(x, edge_index))
x = F.dropout(x, self.dropout, training=self.training)
x = F.relu(self.gc2(x, edge_index))
x = F.dropout(x, self.dropout, training=self.training)
x = self.gc3(x, edge_index)
return F.log_softmax(x, dim=1)
class NodeClsGraphSAGE(nn.Module):
def __init__(self, in_channels, hidden_dims, out_channels, dropout=None, task=None):
super(NodeClsGraphSAGE, self).__init__()
self.sage1 = SAGEConv(in_channels, hidden_dims[0], aggr='min')
self.rm_norm1 = BatchNorm(hidden_dims[0])
self.sage2 = SAGEConv(hidden_dims[0], hidden_dims[1], aggr='min')
self.rm_norm2 = BatchNorm(hidden_dims[1])
self.sage4 = SAGEConv(hidden_dims[1], out_channels, aggr='min')
self.dropout = dropout
self.task = task
def forward(self, x, edge_index):
x = F.relu(self.rm_norm1(self.sage1(x, edge_index)))
x = F.relu(self.rm_norm2(self.sage2(x, edge_index)))
x = self.sage4(x, edge_index)
return F.log_softmax(x, dim=1)
class NodeClsGAT(nn.Module):
def __init__(self, in_channels, hidden_dims, out_channels, dropout):
super(NodeClsGAT, self).__init__()
self.dropout = dropout
self.gat1 = GATConv(in_channels, hidden_dims[0])
self.gat2 = GATConv(hidden_dims[0], hidden_dims[1])
self.gat3 = GATConv(hidden_dims[1], out_channels)
def forward(self, x, edge_index):
x = F.relu(self.gat1(x, edge_index))
x = F.dropout(x, self.dropout, training=self.training)
x = F.relu(self.gat2(x, edge_index))
x = F.dropout(x, self.dropout, training=self.training)
x = self.gat3(x, edge_index)
return F.log_softmax(x, dim=1)
class NodeRegGraphSAGE(nn.Module):
def __init__(self, in_channels, hidden_dims, out_channels, dropout=None, task=None):
super(NodeRegGraphSAGE, self).__init__()
self.sage1 = SAGEConv(in_channels, hidden_dims[0], aggr='min')
self.rm_norm1 = BatchNorm(hidden_dims[0])
self.sage2 = SAGEConv(hidden_dims[0], hidden_dims[1], aggr='min')
self.rm_norm2 = BatchNorm(hidden_dims[1])
self.fc1 = Linear(hidden_dims[1], out_channels)
self.dropout = dropout
self.task = task
def forward(self, x, edge_index):
x = F.relu(self.rm_norm1(self.sage1(x, edge_index)))
x = F.relu(self.rm_norm2(self.sage2(x, edge_index)))
x = self.fc1(x)
return torch.flatten(x)
# normalization layer
class BuildingGenModel(nn.Module):
def __init__(self, in_channels, hidden_dims, dropout=None, model='GraphSAGE'):
super(BuildingGenModel, self).__init__()
# self.dropout = dropout
if model == 'GraphSAGE':
self.rm_l1 = SAGEConv(in_channels, hidden_dims[0], aggr='min')
self.rm_norm1 = BatchNorm(hidden_dims[0])
self.rm_l2 = SAGEConv(hidden_dims[0], hidden_dims[1], aggr='min')
self.rm_norm2 = BatchNorm(hidden_dims[1])
self.rm_l4 = SAGEConv(hidden_dims[1], 3, aggr='min')
# self.rm_l4 = Linear(hidden_dims[1], 3)
self.shared_l1 = SAGEConv(in_channels + 3, hidden_dims[0], aggr='min')
self.shared_norm1 = BatchNorm(hidden_dims[0])
self.premove_l1 = SAGEConv(hidden_dims[0], hidden_dims[1], aggr='min')
self.premove_norm1 = BatchNorm(hidden_dims[1])
self.premove_l3 = Linear(hidden_dims[1], 1)
# self.rtang_l3 = SAGEConv(hidden_dims[1], 1, aggr='sum')
self.sucmove_l1 = SAGEConv(hidden_dims[0], hidden_dims[1], aggr='min')
self.sucmove_norm1 = BatchNorm(hidden_dims[1])
self.sucmove_l3 = Linear(hidden_dims[1], 1)
# self.movedis_l3 = SAGEConv(hidden_dims[1], 1, aggr='sum')
self.weights = torch.nn.Parameter(torch.ones(4).float())
elif model == 'GCN':
self.rm_l1 = GCNConv(in_channels, hidden_dims[0], aggr='min')
self.rm_norm1 = BatchNorm(hidden_dims[0])
self.rm_l2 = GCNConv(hidden_dims[0], hidden_dims[1], aggr='min')
self.rm_norm2 = BatchNorm(hidden_dims[1])
self.rm_l4 = GCNConv(hidden_dims[1], 3, aggr='min')
self.shared_l1 = GCNConv(in_channels + 3, hidden_dims[0], aggr='min')
self.shared_norm1 = BatchNorm(hidden_dims[0])
self.premove_l1 = GCNConv(hidden_dims[0], hidden_dims[1], aggr='min')
self.premove_norm1 = BatchNorm(hidden_dims[1])
self.premove_l3 = Linear(hidden_dims[1], 1)
self.sucmove_l1 = GCNConv(hidden_dims[0], hidden_dims[1], aggr='min')
self.sucmove_norm1 = BatchNorm(hidden_dims[1])
self.sucmove_l3 = Linear(hidden_dims[1], 1)
self.weights = torch.nn.Parameter(torch.ones(4).float())
elif model == 'GAT':
self.rm_l1 = GATConv(in_channels, hidden_dims[0], aggr='min')
self.rm_norm1 = BatchNorm(hidden_dims[0])
self.rm_l2 = GATConv(hidden_dims[0], hidden_dims[1], aggr='min')
self.rm_norm2 = BatchNorm(hidden_dims[1])
self.rm_l4 = GATConv(hidden_dims[1], 3, aggr='min')
self.shared_l1 = GATConv(in_channels + 3, hidden_dims[0], aggr='min')
self.shared_norm1 = BatchNorm(hidden_dims[0])
self.premove_l1 = GATConv(hidden_dims[0], hidden_dims[1], aggr='min')
self.premove_norm1 = BatchNorm(hidden_dims[1])
self.premove_l3 = Linear(hidden_dims[1], 1)
self.sucmove_l1 = GATConv(hidden_dims[0], hidden_dims[1], aggr='min')
self.sucmove_norm1 = BatchNorm(hidden_dims[1])
self.sucmove_l3 = Linear(hidden_dims[1], 1)
self.weights = torch.nn.Parameter(torch.ones(4).float())
else:
print("Invalid graph convlutional operation.")
def forward(self, x, edge_index):
rm = F.relu(self.rm_norm1(self.rm_l1(x, edge_index)))
rm = F.relu(self.rm_norm2(self.rm_l2(rm, edge_index)))
rm = self.rm_l4(rm, edge_index)
x = F.relu(self.shared_norm1(self.shared_l1(torch.cat((x, rm), 1), edge_index)))
rm = F.log_softmax(rm, dim=1)
rm_labels = rm.max(1)[1]
rm_labels = torch.where(rm_labels == 2, 1, 0)
preMove = F.relu(self.premove_norm1(self.premove_l1(x, edge_index)))
preMove = self.premove_l3(preMove)
preMove = torch.flatten(preMove)
preMove = torch.mul(preMove, rm_labels)
sucMove = F.relu(self.sucmove_norm1(self.sucmove_l1(x, edge_index)))
sucMove = self.sucmove_l3(sucMove)
sucMove = torch.flatten(sucMove)
sucMove = torch.mul(sucMove, rm_labels)
return rm, preMove, sucMove