-
Notifications
You must be signed in to change notification settings - Fork 0
/
layers.py
157 lines (120 loc) · 4.8 KB
/
layers.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import pandas as pd
import random
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, f1_score, roc_curve, roc_auc_score, precision_score, recall_score
from torch_geometric.data import Data
from torch.utils.data import Dataset
# Graph Convolutional Layer:
class GCNLayer(nn.Module):
def __init__(self, in_dim, out_dim):
super().__init__()
self.dense = nn.Linear(in_dim, out_dim)
def forward(self, adj, X):
adj = adj + torch.eye(adj.size(0)).to(adj.device)
h = self.dense(X)
norm = adj.sum(1)**(-1/2)
h = norm[None, :] * adj * norm[:, None] @ h
return h
# A = ReLu(W)
class Graph_ReLu_W(nn.Module):
def __init__(self, num_nodes, k, device):
super(Graph_ReLu_W, self).__init__()
self.num_nodes = num_nodes
self.k = k
self.device = device
self.A = nn.Parameter(torch.randn(num_nodes, num_nodes).to(device), requires_grad=True).to(device)
def forward(self, idx):
adj = F.leaky_relu(self.A, negative_slope=0.2)
if self.k:
mask = torch.zeros(idx.size(0), idx.size(0)).to(self.device)
mask.fill_(float('0'))
s1,t1 = (adj + torch.rand_like(adj)*0.01).topk(self.k,1)
mask.scatter_(1,t1,s1.fill_(1))
adj = adj*mask
return adj
# A for Directed graphs:
class Graph_Directed_A(nn.Module):
def __init__(self, num_nodes, window_size, alpha, k, device):
super(Graph_Directed_A, self).__init__()
self.alpha = alpha
self.k = k
self.device = device
self.e1 = nn.Embedding(num_nodes, window_size)
self.e2 = nn.Embedding(num_nodes, window_size)
self.l1 = nn.Linear(window_size,window_size)
self.l2 = nn.Linear(window_size,window_size)
def forward(self, idx):
m1 = torch.tanh(self.alpha*self.l1(self.e1(idx)))
m2 = torch.tanh(self.alpha*self.l2(self.e2(idx)))
adj = F.relu(torch.tanh(self.alpha*torch.mm(m1, m2.transpose(1,0))))
if self.k:
mask = torch.zeros(idx.size(0), idx.size(0)).to(self.device)
mask.fill_(float('0'))
s1,t1 = (adj + torch.rand_like(adj)*0.01).topk(self.k,1)
mask.scatter_(1,t1,s1.fill_(1))
adj = adj*mask
return adj
class SWat_dataset(Dataset):
def __init__(self, dataframe: pd.DataFrame, target: pd.DataFrame, window_size, device):
self.data = dataframe
self.window_size = window_size
self.device = device
def __len__(self):
return len(self.data) - self.window_size
def __getitem__(self, idx):
window = self.data[idx: idx + self.window_size]
features = torch.tensor(window.iloc[:,:].values).float().to(self.device)
return features
def ROC(y_test,y_pred):
fpr,tpr,tr=roc_curve(y_test,y_pred)
auc=roc_auc_score(y_test,y_pred)
idx=np.argwhere(np.diff(np.sign(tpr-(1-fpr)))).flatten()
plt.xlabel("FPR")
plt.ylabel("TPR")
plt.plot(fpr,tpr,label="AUC="+str(auc))
plt.plot(fpr,1-fpr,'r:')
plt.plot(fpr[idx],tpr[idx], 'ro')
plt.legend(loc=4)
plt.grid()
plt.show()
return tr[idx]
class SWat_dataset_window_last(Dataset):
def __init__(self, dataframe: pd.DataFrame, target: pd.DataFrame, window_size, device):
self.data = dataframe
self.window_size = window_size
self.device = device
def __len__(self):
return len(self.data) - self.window_size
def __getitem__(self, idx):
window = self.data[idx: idx + self.window_size]
features = torch.tensor(window.iloc[:,:].values).float().to(self.device)
return torch.transpose(features, 0, 1)
def get_edges(adj):
device = adj.device
row, col = adj.nonzero(as_tuple=True)
edge_index = torch.stack([row, col], dim=0).float().to(device)
return edge_index
class SWat_dataset_GAT(Dataset):
def __init__(self, dataframe: pd.DataFrame, target: pd.DataFrame, window_size, edge_index, device):
self.data = dataframe
self.window_size = window_size
self.device = device
self.edge_index = edge_index
def __len__(self):
return len(self.data) - self.window_size
def __getitem__(self, idx):
window = self.data[idx: idx + self.window_size]
features = torch.tensor(window.iloc[:,:].values).float().to(self.device)
features = torch.transpose(features, 0, 1)
return features
def set_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False