r"""Message aggretation step of ``vertices to hyperedges``. Args: ``X`` (``torch.Tensor``): Vertex feature matrix. Size :math:`(|\mathcal{V}|, C)`. ``aggr`` (``str``): The aggregation method. Can be ``'mean'``, ``'sum'`` and ``'softmax_then_sum'``. ``v2e_
(
self,
X: torch.Tensor,
aggr: str = "mean",
v2e_weight: Optional[torch.Tensor] = None,
drop_rate: float = 0.0,
)
| 1288 | # spatial-based convolution/message-passing |
| 1289 | # general message passing functions |
| 1290 | def v2e_aggregation( |
| 1291 | self, |
| 1292 | X: torch.Tensor, |
| 1293 | aggr: str = "mean", |
| 1294 | v2e_weight: Optional[torch.Tensor] = None, |
| 1295 | drop_rate: float = 0.0, |
| 1296 | ): |
| 1297 | r"""Message aggretation step of ``vertices to hyperedges``. |
| 1298 | |
| 1299 | Args: |
| 1300 | ``X`` (``torch.Tensor``): Vertex feature matrix. Size :math:`(|\mathcal{V}|, C)`. |
| 1301 | ``aggr`` (``str``): The aggregation method. Can be ``'mean'``, ``'sum'`` and ``'softmax_then_sum'``. |
| 1302 | ``v2e_weight`` (``torch.Tensor``, optional): The weight vector attached to connections (vertices point to hyepredges). If not specified, the function will use the weights specified in hypergraph construction. Defaults to ``None``. |
| 1303 | ``drop_rate`` (``float``): Dropout rate. Randomly dropout the connections in incidence matrix with probability ``drop_rate``. Default: ``0.0``. |
| 1304 | """ |
| 1305 | assert aggr in ["mean", "sum", "softmax_then_sum"] |
| 1306 | if self.device != X.device: |
| 1307 | self.to(X.device) |
| 1308 | if v2e_weight is None: |
| 1309 | if drop_rate > 0.0: |
| 1310 | P = sparse_dropout(self.H_T, drop_rate) |
| 1311 | else: |
| 1312 | P = self.H_T |
| 1313 | if aggr == "mean": |
| 1314 | X = torch.sparse.mm(P, X) |
| 1315 | X = torch.sparse.mm(self.D_e_neg_1, X) |
| 1316 | elif aggr == "sum": |
| 1317 | X = torch.sparse.mm(P, X) |
| 1318 | elif aggr == "softmax_then_sum": |
| 1319 | P = torch.sparse.softmax(P, dim=1) |
| 1320 | X = torch.sparse.mm(P, X) |
| 1321 | else: |
| 1322 | raise ValueError(f"Unknown aggregation method {aggr}.") |
| 1323 | else: |
| 1324 | # init message path |
| 1325 | assert ( |
| 1326 | v2e_weight.shape[0] == self.v2e_weight.shape[0] |
| 1327 | ), "The size of v2e_weight must be equal to the size of self.v2e_weight." |
| 1328 | P = torch.sparse_coo_tensor( |
| 1329 | self.H_T._indices(), v2e_weight, self.H_T.shape, device=self.device |
| 1330 | ) |
| 1331 | if drop_rate > 0.0: |
| 1332 | P = sparse_dropout(P, drop_rate) |
| 1333 | # message passing |
| 1334 | if aggr == "mean": |
| 1335 | X = torch.sparse.mm(P, X) |
| 1336 | D_e_neg_1 = torch.sparse.sum(P, dim=1).to_dense().view(-1, 1) |
| 1337 | D_e_neg_1[torch.isinf(D_e_neg_1)] = 0 |
| 1338 | X = D_e_neg_1 * X |
| 1339 | elif aggr == "sum": |
| 1340 | X = torch.sparse.mm(P, X) |
| 1341 | elif aggr == "softmax_then_sum": |
| 1342 | P = torch.sparse.softmax(P, dim=1) |
| 1343 | X = torch.sparse.mm(P, X) |
| 1344 | else: |
| 1345 | raise ValueError(f"Unknown aggregation method {aggr}.") |
| 1346 | return X |
| 1347 |
no test coverage detected