from typing import List, \ Union, \ Callable from .data import Data, \ RelationFamily from .trainprep import PreparedData, \ PreparedRelationFamily import torch from .weights import init_glorot from .normalize import _sparse_coo_tensor import types def _sparse_diag_cat(matrices: List[torch.Tensor]): if len(matrices) == 0: raise ValueError('The list of matrices must be non-empty') if not all(m.is_sparse for m in matrices): raise ValueError('All matrices must be sparse') if not all(len(m.shape) == 2 for m in matrices): raise ValueError('All matrices must be 2D') indices = [] values = [] row_offset = 0 col_offset = 0 for m in matrices: ind = m._indices().clone() ind[0] += row_offset ind[1] += col_offset indices.append(ind) values.append(m._values()) row_offset += m.shape[0] col_offset += m.shape[1] indices = torch.cat(indices, dim=1) values = torch.cat(values) return _sparse_coo_tensor(indices, values, size=(row_offset, col_offset)) def _cat(matrices: List[torch.Tensor]): if len(matrices) == 0: raise ValueError('Empty list passed to _cat()') n = sum(a.is_sparse for a in matrices) if n != 0 and n != len(matrices): raise ValueError('All matrices must have the same layout (dense or sparse)') if not all(a.shape[1:] == matrices[0].shape[1:] for a in matrices): raise ValueError('All matrices must have the same dimensions apart from dimension 0') if not matrices[0].is_sparse: return torch.cat(matrices) total_rows = sum(a.shape[0] for a in matrices) indices = [] values = [] row_offset = 0 for a in matrices: ind = a._indices().clone() val = a._values() ind[0] += row_offset ind = ind.transpose(0, 1) indices.append(ind) values.append(val) row_offset += a.shape[0] indices = torch.cat(indices).transpose(0, 1) values = torch.cat(values) res = _sparse_coo_tensor(indices, values, size=(row_offset, matrices[0].shape[1])) return res class FastGraphConv(torch.nn.Module): def __init__(self, in_channels: int, out_channels: int, adjacency_matrices: List[torch.Tensor], keep_prob: float = 1., activation: Callable[[torch.Tensor], torch.Tensor] = lambda x: x, **kwargs) -> None: super().__init__(**kwargs) in_channels = int(in_channels) out_channels = int(out_channels) if not isinstance(adjacency_matrices, list): raise TypeError('adjacency_matrices must be a list') if len(adjacency_matrices) == 0: raise ValueError('adjacency_matrices must not be empty') if not all(isinstance(m, torch.Tensor) for m in adjacency_matrices): raise TypeError('adjacency_matrices elements must be of class torch.Tensor') if not all(m.is_sparse for m in adjacency_matrices): raise ValueError('adjacency_matrices elements must be sparse') keep_prob = float(keep_prob) if not isinstance(activation, types.FunctionType): raise TypeError('activation must be a function') self.in_channels = in_channels self.out_channels = out_channels self.adjacency_matrices = adjacency_matrices self.keep_prob = keep_prob self.activation = activation self.num_row_nodes = len(adjacency_matrices[0]) self.num_relation_types = len(adjacency_matrices) self.adjacency_matrices = _sparse_diag_cat(adjacency_matrices) self.weights = torch.cat([ init_glorot(in_channels, out_channels) \ for _ in range(self.num_relation_types) ], dim=1) def forward(self, x) -> torch.Tensor: if self.keep_prob < 1.: x = dropout(x, self.keep_prob) res = torch.sparse.mm(x, self.weights) \ if x.is_sparse \ else torch.mm(x, self.weights) res = torch.split(res, res.shape[1] // self.num_relation_types, dim=1) res = torch.cat(res) res = torch.sparse.mm(self.adjacency_matrices, res) \ if self.adjacency_matrices.is_sparse \ else torch.mm(self.adjacency_matrices, res) res = res.view(self.num_relation_types, self.num_row_nodes, self.out_channels) if self.activation is not None: res = self.activation(res) return res class FastConvLayer(torch.nn.Module): adjacency_matrix: List[torch.Tensor] adjacency_matrix_backward: List[torch.Tensor] weight: List[torch.Tensor] weight_backward: List[torch.Tensor] def __init__(self, input_dim: List[int], output_dim: List[int], data: Union[Data, PreparedData], keep_prob: float = 1., rel_activation: Callable[[torch.Tensor], torch.Tensor] = lambda x: x, layer_activation: Callable[[torch.Tensor], torch.Tensor] = torch.nn.functional.relu, **kwargs): super().__init__(**kwargs) self._check_params(input_dim, output_dim, data, keep_prob, rel_activation, layer_activation) self.input_dim = input_dim self.output_dim = output_dim self.data = data self.keep_prob = keep_prob self.rel_activation = rel_activation self.layer_activation = layer_activation self.adjacency_matrix = None self.adjacency_matrix_backward = None self.weight = None self.weight_backward = None self.build() def build(self): self.adjacency_matrix = [] self.adjacency_matrix_backward = [] self.weight = [] self.weight_backward = [] for fam in self.data.relation_families: adj_mat = [ rel.adjacency_matrix \ for rel in fam.relation_types \ if rel.adjacency_matrix is not None ] adj_mat_back = [ rel.adjacency_matrix_backward \ for rel in fam.relation_types \ if rel.adjacency_matrix_backward is not None ] weight = [ init_glorot(self.input_dim[fam.node_type_column], self.output_dim[fam.node_type_row]) \ for _ in range(len(adj_mat)) ] weight_back = [ init_glorot(self.input_dim[fam.node_type_column], self.output_dim[fam.node_type_row]) \ for _ in range(len(adj_mat_back)) ] adj_mat = torch.cat(adj_mat) \ if len(adj_mat) > 0 \ else None adj_mat_back = torch.cat(adj_mat_back) \ if len(adj_mat_back) > 0 \ else None self.adjacency_matrix.append(adj_mat) self.adjacency_matrix_backward.append(adj_mat_back) self.weight.append(weight) self.weight_backward.append(weight_back) def forward(self, prev_layer_repr): for i, fam in enumerate(self.data.relation_families): repr_row = prev_layer_repr[fam.node_type_row] repr_column = prev_layer_repr[fam.node_type_column] adj_mat = self.adjacency_matrix[i] adj_mat_back = self.adjacency_matrix_backward[i] if adj_mat is not None: x = dropout(repr_column, keep_prob=self.keep_prob) x = torch.sparse.mm(x, self.weight[i]) \ if x.is_sparse \ else torch.mm(x, self.weight[i]) x = torch.sparse.mm(adj_mat, x) \ if adj_mat.is_sparse \ else torch.mm(adj_mat, x) x = self.rel_activation(x) x = x.view(len(fam.relation_types), len(repr_row), -1) if adj_mat_back is not None: x = dropout(repr_column, keep_prob=self.keep_prob) x = torch.sparse.mm(x, self.weight_backward[i]) \ if x.is_sparse \ else torch.mm(x, self.weight_backward[i]) x = torch.sparse.mm(adj_mat_back, x) \ if adj_mat_back.is_sparse \ else torch.mm(adj_mat_back, x) x = self.rel_activation(x) x = x.view(len(fam.relation_types), len(repr_row), -1) @staticmethod def _check_params(input_dim, output_dim, data, keep_prob, rel_activation, layer_activation): if not isinstance(input_dim, list): raise ValueError('input_dim must be a list') if not output_dim: raise ValueError('output_dim must be specified') if not isinstance(output_dim, list): output_dim = [output_dim] * len(data.node_types) if not isinstance(data, Data) and not isinstance(data, PreparedData): raise ValueError('data must be of type Data or PreparedData')