| 78 | |
| 79 | |
| 80 | class PAG(nn.Module): |
| 81 | def __init__(self, a_sparse, seq=12, kcnn=2, k=6, m=2): |
| 82 | super(PAG, self).__init__() |
| 83 | self.feature = seq |
| 84 | self.seq = seq-kcnn+1 |
| 85 | self.alpha = 0.5 |
| 86 | self.m = m |
| 87 | self.a_sparse = a_sparse |
| 88 | self.nodes = a_sparse.shape[0] |
| 89 | |
| 90 | # GAT |
| 91 | self.conv2d = nn.Conv2d(1, 1, (kcnn, 2)) # input.shape = [batch, channel, width, height] |
| 92 | self.gat_lyr = MultiHeadsGATLayer(a_sparse, self.seq, self.seq, 4, 0, 0.2) |
| 93 | self.gcn = nn.Linear(in_features=self.seq, out_features=self.seq) |
| 94 | |
| 95 | # TPA |
| 96 | self.lstm = nn.LSTM(m, m, num_layers=2, batch_first=True) |
| 97 | self.fc1 = nn.Linear(in_features=self.seq - 1, out_features=k) |
| 98 | self.fc2 = nn.Linear(in_features=k, out_features=m) |
| 99 | self.fc3 = nn.Linear(in_features=k + m, out_features=1) |
| 100 | self.decoder = nn.Linear(self.seq, 1) |
| 101 | |
| 102 | # Activation |
| 103 | self.dropout = nn.Dropout(p=0.5) |
| 104 | self.LeakyReLU = nn.LeakyReLU() |
| 105 | |
| 106 | # |
| 107 | adj1 = copy.deepcopy(self.a_sparse.to_dense()) |
| 108 | adj2 = copy.deepcopy(self.a_sparse.to_dense()) |
| 109 | for i in range(self.nodes): |
| 110 | adj1[i, i] = 0.000000001 |
| 111 | adj2[i, i] = 0 |
| 112 | degree = 1.0 / (torch.sum(adj1, dim=0)) |
| 113 | degree_matrix = torch.zeros((self.nodes, self.feature), device=device) |
| 114 | for i in range(12): |
| 115 | degree_matrix[:, i] = degree |
| 116 | self.degree_matrix = degree_matrix |
| 117 | self.adj2 = adj2 |
| 118 | |
| 119 | def forward(self, occ, prc): # occ.shape = [batch,node, seq] |
| 120 | b, n, s = occ.shape |
| 121 | data = torch.stack([occ, prc], dim=3).reshape(b*n, s, -1).unsqueeze(1) |
| 122 | data = self.conv2d(data) |
| 123 | data = data.squeeze().reshape(b, n, -1) |
| 124 | |
| 125 | # first layer |
| 126 | atts_mat = self.gat_lyr(data) # attention matrix, dense(nodes, nodes) |
| 127 | occ_conv1 = torch.matmul(atts_mat, data) # (b, n, s) |
| 128 | occ_conv1 = self.dropout(self.LeakyReLU(self.gcn(occ_conv1))) |
| 129 | |
| 130 | # second layer |
| 131 | atts_mat2 = self.gat_lyr(occ_conv1) # attention matrix, dense(nodes, nodes) |
| 132 | occ_conv2 = torch.matmul(atts_mat2, occ_conv1) # (b, n, s) |
| 133 | occ_conv2 = self.dropout(self.LeakyReLU(self.gcn(occ_conv2))) |
| 134 | |
| 135 | occ_conv1 = (1 - self.alpha) * occ_conv1 + self.alpha * data |
| 136 | occ_conv2 = (1 - self.alpha) * occ_conv2 + self.alpha * occ_conv1 |
| 137 | occ_conv1 = occ_conv1.view(b * n, self.seq) |
nothing calls this directly
no outgoing calls
no test coverage detected