Skip to content

laplace

Laplacian

Bases: MessagePassing

Matrix-free uniform graph Laplacian.

The operator uses the random-walk form of the uniform Laplacian: Lx = x - mean(neighbour_values).

Source code in src/torch_structure/message_passing/laplace.py
 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
class Laplacian(MessagePassing):
    """Matrix-free uniform graph Laplacian.

    The operator uses the random-walk form of the uniform Laplacian:
    ``Lx = x - mean(neighbour_values)``.
    """

    def __init__(self, edge_index, num_nodes=None):
        super().__init__(aggr="mean")
        self.register_buffer("edge_index", edge_index)
        self.num_nodes = num_nodes

    def forward(self, x):
        """Apply the Laplacian operator: ``x - mean(neighbour_values)``.

        Args:
            x (torch.Tensor [N, *]): per-node values.

        Returns:
            torch.Tensor [N, *]: the Laplacian of ``x``.
        """
        mean_neighbours = self.propagate(self.edge_index, x=x, size=(self.num_nodes, self.num_nodes))
        return x - mean_neighbours

    def dense_matrix(self):
        """Materialize the operator as a dense matrix when a solver needs it."""
        identity = torch.eye(
            self.num_nodes, device=self.edge_index.device, dtype=torch.get_default_dtype()
        )
        return self(identity)

    def message(self, x_j):
        """Pass each neighbour's value through unchanged, to be mean-aggregated."""
        return x_j

dense_matrix()

Materialize the operator as a dense matrix when a solver needs it.

Source code in src/torch_structure/message_passing/laplace.py
29
30
31
32
33
34
def dense_matrix(self):
    """Materialize the operator as a dense matrix when a solver needs it."""
    identity = torch.eye(
        self.num_nodes, device=self.edge_index.device, dtype=torch.get_default_dtype()
    )
    return self(identity)

forward(x)

Apply the Laplacian operator: x - mean(neighbour_values).

Parameters:

Name Type Description Default
x torch.Tensor [N, *]

per-node values.

required

Returns:

Type Description

torch.Tensor [N, *]: the Laplacian of x.

Source code in src/torch_structure/message_passing/laplace.py
17
18
19
20
21
22
23
24
25
26
27
def forward(self, x):
    """Apply the Laplacian operator: ``x - mean(neighbour_values)``.

    Args:
        x (torch.Tensor [N, *]): per-node values.

    Returns:
        torch.Tensor [N, *]: the Laplacian of ``x``.
    """
    mean_neighbours = self.propagate(self.edge_index, x=x, size=(self.num_nodes, self.num_nodes))
    return x - mean_neighbours

message(x_j)

Pass each neighbour's value through unchanged, to be mean-aggregated.

Source code in src/torch_structure/message_passing/laplace.py
36
37
38
def message(self, x_j):
    """Pass each neighbour's value through unchanged, to be mean-aggregated."""
    return x_j