Python torch_geometric.utils.add_self_loops() Examples
The following are 26
code examples of torch_geometric.utils.add_self_loops().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
torch_geometric.utils
, or try the search function
.
Example #1
Source File: cluster_gcn_conv.py From pytorch_geometric with MIT License | 6 votes |
def __init__(self, in_channels: int, out_channels: int, diag_lambda: float = 0., add_self_loops: bool = True, bias: bool = True, **kwargs): super(ClusterGCNConv, self).__init__(aggr='add', **kwargs) self.in_channels = in_channels self.out_channels = out_channels self.diag_lambda = diag_lambda self.add_self_loops = add_self_loops self.weight = Parameter(torch.Tensor(in_channels, out_channels)) self.root_weight = Parameter(torch.Tensor(in_channels, out_channels)) if bias: self.bias = Parameter(torch.Tensor(out_channels)) else: self.register_parameter('bias', None) self.reset_parameters()
Example #2
Source File: mf_conv.py From pytorch_geometric with MIT License | 6 votes |
def forward(self, x, edge_index): edge_index, _ = remove_self_loops(edge_index) deg = degree(edge_index[1 if self.flow == 'source_to_target' else 0], x.size(0), dtype=torch.long) deg.clamp_(max=self.max_degree) if not self.root_weight: edge_index, _ = add_self_loops(edge_index, num_nodes=x.size(self.node_dim)) h = self.propagate(edge_index, x=x) out = x.new_empty(list(x.size())[:-1] + [self.out_channels]) for i in deg.unique().tolist(): idx = (deg == i).nonzero().view(-1) r = self.rel_lins[i](h.index_select(self.node_dim, idx)) if self.root_weight: r = r + self.root_lins[i](x.index_select(self.node_dim, idx)) out.index_copy_(self.node_dim, idx, r) return out
Example #3
Source File: feast_conv.py From pytorch_geometric with MIT License | 6 votes |
def __init__(self, in_channels: int, out_channels: int, heads: int = 1, add_self_loops: bool = True, bias: bool = True, **kwargs): super(FeaStConv, self).__init__(aggr='mean', **kwargs) self.in_channels = in_channels self.out_channels = out_channels self.heads = heads self.add_self_loops = add_self_loops self.weight = Parameter(torch.Tensor(in_channels, heads * out_channels)) self.u = Parameter(torch.Tensor(in_channels, heads)) self.c = Parameter(torch.Tensor(heads)) if bias: self.bias = Parameter(torch.Tensor(out_channels)) else: self.register_parameter('bias', None) self.reset_parameters()
Example #4
Source File: feast_conv.py From pytorch_geometric with MIT License | 6 votes |
def forward(self, x: Union[Tensor, PairTensor], edge_index: Adj) -> Tensor: """""" if isinstance(x, Tensor): x: PairTensor = (x, x) if self.add_self_loops: if isinstance(edge_index, Tensor): edge_index, _ = remove_self_loops(edge_index) edge_index, _ = add_self_loops(edge_index, num_nodes=x[1].size(0)) elif isinstance(edge_index, SparseTensor): edge_index = set_diag(edge_index) # propagate_type: (x: PairTensor) out = self.propagate(edge_index, x=x, size=None) if self.bias is not None: out += self.bias return out
Example #5
Source File: le_conv.py From ASAP with Apache License 2.0 | 6 votes |
def forward(self, x, edge_index, edge_weight=None, size=None): """""" num_nodes = x.shape[0] h = torch.matmul(x, self.weight) if edge_weight is None: edge_weight = torch.ones((edge_index.size(1), ), dtype=x.dtype, device=edge_index.device) edge_index, edge_weight = remove_self_loops(edge_index=edge_index, edge_attr=edge_weight) deg = scatter_add(edge_weight, edge_index[0], dim=0, dim_size=num_nodes) #+ 1e-10 h_j = edge_weight.view(-1, 1) * h[edge_index[1]] aggr_out = scatter_add(h_j, edge_index[0], dim=0, dim_size=num_nodes) out = ( deg.view(-1, 1) * self.lin1(x) + aggr_out) + self.lin2(x) edge_index, edge_weight = add_self_loops(edge_index=edge_index, edge_weight=edge_weight, num_nodes=num_nodes) return out
Example #6
Source File: cheb_conv.py From pytorch_geometric with MIT License | 6 votes |
def __norm__(self, edge_index, num_nodes: Optional[int], edge_weight: OptTensor, normalization: Optional[str], lambda_max, dtype: Optional[int] = None, batch: OptTensor = None): edge_index, edge_weight = remove_self_loops(edge_index, edge_weight) edge_index, edge_weight = get_laplacian(edge_index, edge_weight, normalization, dtype, num_nodes) if batch is not None and lambda_max.numel() > 1: lambda_max = lambda_max[batch[edge_index[0]]] edge_weight = (2.0 * edge_weight) / lambda_max edge_weight.masked_fill_(edge_weight == float('inf'), 0) edge_index, edge_weight = add_self_loops(edge_index, edge_weight, fill_value=-1., num_nodes=num_nodes) assert edge_weight is not None return edge_index, edge_weight
Example #7
Source File: ppf_conv.py From pytorch_geometric with MIT License | 5 votes |
def __init__(self, local_nn: Optional[Callable] = None, global_nn: Optional[Callable] = None, add_self_loops: bool = True, **kwargs): super(PPFConv, self).__init__(aggr='max', **kwargs) self.local_nn = local_nn self.global_nn = global_nn self.add_self_loops = add_self_loops self.reset_parameters()
Example #8
Source File: torch_vertex.py From sgas with MIT License | 5 votes |
def forward(self, x, edge_index, size=None): """""" if size is None: edge_index, _ = remove_self_loops(edge_index) edge_index, _ = add_self_loops(edge_index, num_nodes=x.size(0)) x = x.unsqueeze(-1) if x.dim() == 1 else x return self.propagate(edge_index, size=size, x=x)
Example #9
Source File: pyg_gnn_layer.py From GraphNAS with Apache License 2.0 | 5 votes |
def forward(self, x, edge_index): """""" edge_index, _ = remove_self_loops(edge_index) edge_index, _ = add_self_loops(edge_index, num_nodes=x.size(0)) # prepare x = torch.mm(x, self.weight).view(-1, self.heads, self.out_channels) return self.propagate(edge_index, x=x, num_nodes=x.size(0))
Example #10
Source File: pyg_layer_new.py From GraphNAS with Apache License 2.0 | 5 votes |
def forward(self, x, edge_index): """""" edge_index, _ = remove_self_loops(edge_index) edge_index, _ = add_self_loops(edge_index, num_nodes=x.size(0)) # prepare x = torch.mm(x, self.weight).view(-1, self.heads, self.out_channels) x_i = x[edge_index[0]] x_j = x[edge_index[1]] alpha = self.att(x_j, x_i) out = self.agg(x_j, alpha, edge_index, x.size(0)) return self.update(out)
Example #11
Source File: torch_vertex.py From deep_gcns_torch with MIT License | 5 votes |
def forward(self, x, edge_index, size=None): """""" if size is None: edge_index, _ = remove_self_loops(edge_index) edge_index, _ = add_self_loops(edge_index, num_nodes=x.size(0)) x = x.unsqueeze(-1) if x.dim() == 1 else x return self.propagate(edge_index, size=size, x=x)
Example #12
Source File: test.py From deep_gcns_torch with MIT License | 5 votes |
def main(): args = ArgsInit().args dataset = PygNodePropPredDataset(name=args.dataset) graph = dataset[0] if args.self_loop: graph.edge_index = add_self_loops(edge_index=graph.edge_index, num_nodes=graph.num_nodes)[0] split_idx = dataset.get_idx_split() evaluator = Evaluator(args.dataset) args.in_channels = graph.x.size(-1) args.num_tasks = dataset.num_classes print(args) model = DeeperGCN(args) print(model) model.load_state_dict(torch.load(args.model_load_path)['model_state_dict']) result = test(model, graph.x, graph.edge_index, graph.y, split_idx, evaluator) print(result) model.print_params(final=True)
Example #13
Source File: graph_star_conv.py From graph_star with MIT License | 5 votes |
def forward(self, x, stars, init_x, edge_index, edge_type=None): """""" num_nodes = x.size(0) nodes = x if init_x is not None: nodes = torch.cat([nodes, init_x], dim=0) if stars is not None: nodes = torch.cat([nodes, stars], dim=0) edge_index = add_self_loops(edge_index, num_nodes=x.size(0)) nodes = self.propagate('add', edge_index, x=nodes, num_nodes=num_nodes) return nodes
Example #14
Source File: softmax_issue.py From FlexTensor with MIT License | 5 votes |
def forward(self, x, edge_index, size=None): """""" if size is None and torch.is_tensor(x): edge_index, _ = remove_self_loops(edge_index) edge_index, _ = add_self_loops(edge_index, num_nodes=x.size(0)) if torch.is_tensor(x): x = torch.matmul(x, self.weight) else: x = (None if x[0] is None else torch.matmul(x[0], self.weight), None if x[1] is None else torch.matmul(x[1], self.weight)) return self.propagate(edge_index, size=size, x=x)
Example #15
Source File: graph_models.py From G-Bert with MIT License | 5 votes |
def forward(self, x, edge_index): """""" edge_index = add_self_loops(edge_index, num_nodes=x.size(0)) x = torch.mm(x, self.weight).view(-1, self.heads, self.out_channels) return self.propagate('add', edge_index, x=x, num_nodes=x.size(0))
Example #16
Source File: test_create_gnn.py From pytorch_geometric with MIT License | 5 votes |
def forward(self, x, edge_index): edge_index, _ = add_self_loops(edge_index, num_nodes=x.size(0)) row, col = edge_index deg = degree(row, x.size(0), dtype=x.dtype) deg_inv_sqrt = deg.pow(-0.5) norm = deg_inv_sqrt[row] * deg_inv_sqrt[col] x = self.lin(x) return self.propagate(edge_index, size=(x.size(0), x.size(0)), x=x, norm=norm)
Example #17
Source File: ppf_conv.py From pytorch_geometric with MIT License | 5 votes |
def forward(self, x: Union[OptTensor, PairOptTensor], pos: Union[Tensor, PairTensor], normal: Union[Tensor, PairTensor], edge_index: Adj) -> Tensor: # yapf: disable """""" if not isinstance(x, tuple): x: PairOptTensor = (x, None) if isinstance(pos, Tensor): pos: PairTensor = (pos, pos) if isinstance(normal, Tensor): normal: PairTensor = (normal, normal) if self.add_self_loops: if isinstance(edge_index, Tensor): edge_index, _ = remove_self_loops(edge_index) edge_index, _ = add_self_loops(edge_index, num_nodes=pos[1].size(0)) elif isinstance(edge_index, SparseTensor): edge_index = set_diag(edge_index) # propagate_type: (x: PairOptTensor, pos: PairTensor, normal: PairTensor) # noqa out = self.propagate(edge_index, x=x, pos=pos, normal=normal, size=None) if self.global_nn is not None: out = self.global_nn(out) return out
Example #18
Source File: signedsageconvolution.py From SGCN with GNU General Public License v3.0 | 5 votes |
def forward(self, x_1, x_2, edge_index_pos, edge_index_neg): """ Forward propagation pass with features an indices. :param x_1: Features for left hand side vertices. :param x_2: Features for right hand side vertices. :param edge_index_pos: Positive indices. :param edge_index_neg: Negative indices. :return out: Abstract convolved features. """ edge_index_pos, _ = remove_self_loops(edge_index_pos, None) edge_index_pos, _ = add_self_loops(edge_index_pos, num_nodes=x_1.size(0)) edge_index_neg, _ = remove_self_loops(edge_index_neg, None) edge_index_neg, _ = add_self_loops(edge_index_neg, num_nodes=x_2.size(0)) row_pos, col_pos = edge_index_pos row_neg, col_neg = edge_index_neg if self.norm: out_1 = scatter_mean(x_1[col_pos], row_pos, dim=0, dim_size=x_1.size(0)) out_2 = scatter_mean(x_2[col_neg], row_neg, dim=0, dim_size=x_2.size(0)) else: out_1 = scatter_add(x_1[col_pos], row_pos, dim=0, dim_size=x_1.size(0)) out_2 = scatter_add(x_2[col_neg], row_neg, dim=0, dim_size=x_2.size(0)) out = torch.cat((out_1, out_2, x_1), 1) out = torch.matmul(out, self.weight) if self.bias is not None: out = out + self.bias if self.norm_embed: out = F.normalize(out, p=2, dim=-1) return out
Example #19
Source File: agnn_conv.py From pytorch_geometric with MIT License | 5 votes |
def forward(self, x: Tensor, edge_index: Adj) -> Tensor: """""" if self.add_self_loops: if isinstance(edge_index, Tensor): edge_index, _ = remove_self_loops(edge_index) edge_index, _ = add_self_loops(edge_index, num_nodes=x.size(self.node_dim)) elif isinstance(edge_index, SparseTensor): edge_index = set_diag(edge_index) x_norm = F.normalize(x, p=2., dim=-1) # propagate_type: (x: Tensor, x_norm: Tensor) return self.propagate(edge_index, x=x, x_norm=x_norm, size=None)
Example #20
Source File: gat_conv.py From pytorch_geometric with MIT License | 5 votes |
def __init__(self, in_channels: Union[int, Tuple[int, int]], out_channels: int, heads: int = 1, concat: bool = True, negative_slope: float = 0.2, dropout: float = 0., add_self_loops: bool = True, bias: bool = True, **kwargs): super(GATConv, self).__init__(aggr='add', node_dim=0, **kwargs) self.in_channels = in_channels self.out_channels = out_channels self.heads = heads self.concat = concat self.negative_slope = negative_slope self.dropout = dropout self.add_self_loops = add_self_loops if isinstance(in_channels, int): self.lin_l = Linear(in_channels, heads * out_channels, bias=False) self.lin_r = self.lin_l else: self.lin_l = Linear(in_channels[0], heads * out_channels, False) self.lin_r = Linear(in_channels[1], heads * out_channels, False) self.att_l = Parameter(torch.Tensor(1, heads, out_channels)) self.att_r = Parameter(torch.Tensor(1, heads, out_channels)) if bias and concat: self.bias = Parameter(torch.Tensor(heads * out_channels)) elif bias and not concat: self.bias = Parameter(torch.Tensor(out_channels)) else: self.register_parameter('bias', None) self._alpha = None self.reset_parameters()
Example #21
Source File: point_conv.py From pytorch_geometric with MIT License | 5 votes |
def __init__(self, local_nn: Optional[Callable] = None, global_nn: Optional[Callable] = None, add_self_loops: bool = True, **kwargs): super(PointConv, self).__init__(aggr='max', **kwargs) self.local_nn = local_nn self.global_nn = global_nn self.add_self_loops = add_self_loops self.reset_parameters()
Example #22
Source File: cluster_gcn_conv.py From pytorch_geometric with MIT License | 5 votes |
def forward(self, x: Tensor, edge_index: Adj, size: Size = None) -> Tensor: """""" edge_weight: OptTensor = None if isinstance(edge_index, Tensor): num_nodes = size[1] if size is not None else x.size(self.node_dim) if self.add_self_loops: edge_index, _ = remove_self_loops(edge_index) edge_index, _ = add_self_loops(edge_index, num_nodes=num_nodes) row, col = edge_index[0], edge_index[1] deg_inv = 1. / degree(col, num_nodes=num_nodes).clamp_(1.) edge_weight = deg_inv[col] edge_weight[row == col] += self.diag_lambda * deg_inv elif isinstance(edge_index, SparseTensor): if self.add_self_loops: edge_index = set_diag(edge_index) col, row, _ = edge_index.coo() # Transposed. deg_inv = 1. / sum(edge_index, dim=1).clamp_(1.) edge_weight = deg_inv[col] edge_weight[row == col] += self.diag_lambda * deg_inv edge_index = edge_index.set_value(edge_weight, layout='coo') # propagate_type: (x: Tensor, edge_weight: OptTensor) out = self.propagate(edge_index, x=x, edge_weight=edge_weight, size=None) out = out @ self.weight + x @ self.root_weight if self.bias is not None: out += self.bias return out
Example #23
Source File: add_self_loops.py From pytorch_geometric with MIT License | 5 votes |
def __call__(self, data): N = data.num_nodes edge_index = data.edge_index assert data.edge_attr is None edge_index, _ = add_self_loops(edge_index, num_nodes=N) edge_index, _ = coalesce(edge_index, None, N, N) data.edge_index = edge_index return data
Example #24
Source File: gdc.py From pytorch_geometric with MIT License | 5 votes |
def __call__(self, data): N = data.num_nodes edge_index = data.edge_index if data.edge_attr is None: edge_weight = torch.ones(edge_index.size(1), device=edge_index.device) else: edge_weight = data.edge_attr assert self.exact assert edge_weight.dim() == 1 if self.self_loop_weight: edge_index, edge_weight = add_self_loops( edge_index, edge_weight, fill_value=self.self_loop_weight, num_nodes=N) edge_index, edge_weight = coalesce(edge_index, edge_weight, N, N) if self.exact: edge_index, edge_weight = self.transition_matrix( edge_index, edge_weight, N, self.normalization_in) diff_mat = self.diffusion_matrix_exact(edge_index, edge_weight, N, **self.diffusion_kwargs) edge_index, edge_weight = self.sparsify_dense( diff_mat, **self.sparsification_kwargs) else: edge_index, edge_weight = self.diffusion_matrix_approx( edge_index, edge_weight, N, self.normalization_in, **self.diffusion_kwargs) edge_index, edge_weight = self.sparsify_sparse( edge_index, edge_weight, N, **self.sparsification_kwargs) edge_index, edge_weight = coalesce(edge_index, edge_weight, N, N) edge_index, edge_weight = self.transition_matrix( edge_index, edge_weight, N, self.normalization_out) data.edge_index = edge_index data.edge_attr = edge_weight return data
Example #25
Source File: DGCNN.py From gnn-comparison with GNU General Public License v3.0 | 5 votes |
def forward(self, x, edge_index): # x has shape [N, in_channels] # edge_index has shape [2, E] # Step 1: Add self-loops to the adjacency matrix. edge_index, _ = add_self_loops(edge_index, num_nodes=x.size(0)) # Step 2: Linearly transform node feature matrix. x = self.lin(x) # Step 3-5: Start propagating messages. return self.propagate(edge_index, size=(x.size(0), x.size(0)), x=x)
Example #26
Source File: test.py From deep_gcns_torch with MIT License | 4 votes |
def main(): args = ArgsInit().args if args.use_gpu: device = torch.device("cuda:" + str(args.device)) if torch.cuda.is_available() else torch.device("cpu") else: device = torch.device('cpu') dataset = PygNodePropPredDataset(name=args.dataset) data = dataset[0] split_idx = dataset.get_idx_split() evaluator = Evaluator(args.dataset) x = data.x.to(device) y_true = data.y.to(device) edge_index = data.edge_index.to(device) edge_index = to_undirected(edge_index, data.num_nodes) if args.self_loop: edge_index = add_self_loops(edge_index, num_nodes=data.num_nodes)[0] args.in_channels = data.x.size(-1) args.num_tasks = dataset.num_classes print(args) model = DeeperGCN(args) model.load_state_dict(torch.load(args.model_load_path)['model_state_dict']) model.to(device) result = test(model, x, edge_index, y_true, split_idx, evaluator) train_accuracy, valid_accuracy, test_accuracy = result print({'Train': train_accuracy, 'Validation': valid_accuracy, 'Test': test_accuracy}) model.print_params(final=True)