-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8eb178f
commit 2b4c0f2
Showing
3 changed files
with
78 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,71 @@ | ||
import torch | ||
from torch.nn import Tanh | ||
from .layers import GraphIntegralKernel | ||
from .feed_forward import FeedForward | ||
|
||
class GNO(torch.nn.Module): | ||
def __init__(self, | ||
lifting_operator, | ||
projection_operator, | ||
edge_features, | ||
n_layers=1, | ||
kernel_n_layers=0, | ||
kernel_inner_size=None, | ||
kernel_layers=None, | ||
common=False | ||
): | ||
super(GNO, self).__init__() | ||
self.lifting_operator = lifting_operator | ||
self.projection_operator = projection_operator | ||
self.hidden_dim = lifting_operator.out_features | ||
self.tanh = Tanh() | ||
if common: | ||
dense = FeedForward(input_dimensions=edge_features, | ||
output_dimensions=self.hidden_dim ** 2, | ||
n_layers=kernel_n_layers, | ||
inner_size=kernel_inner_size, | ||
layers=kernel_layers) | ||
W = FeedForward(input_dimensions=self.hidden_dim, | ||
output_dimensions=self.hidden_dim, | ||
n_layers=1) | ||
self.kernels = torch.nn.ModuleList( | ||
[GraphIntegralKernel(width=lifting_operator.out_features, | ||
kernel_width=edge_features, | ||
W=W, | ||
dense=dense) for _ in range(n_layers)]) | ||
else: | ||
self.kernels = torch.nn.ModuleList( | ||
[GraphIntegralKernel(width=lifting_operator.out_features, | ||
kernel_width=edge_features, | ||
n_layers=kernel_n_layers, | ||
inner_size=kernel_inner_size, | ||
layers=kernel_layers) for _ in | ||
range(n_layers)]) | ||
from .layers import GraphIntegralLayer | ||
from .base_no import KernelNeuralOperator | ||
|
||
|
||
class GraphNeuralKernel(torch.nn.Module): | ||
def __init__( | ||
self, | ||
width, | ||
edge_features, | ||
n_layers=2, | ||
internal_n_layers=0, | ||
inner_size=None, | ||
internal_layers=None, | ||
func=None | ||
): | ||
super().__init__() | ||
if func is None: | ||
func = Tanh | ||
|
||
self.layers = torch.nn.ModuleList( | ||
[GraphIntegralLayer(width=width, | ||
edges_features=edge_features, | ||
n_layers=internal_n_layers, | ||
inner_size=inner_size, | ||
layers=internal_layers, | ||
func=func) for _ in range(n_layers)] | ||
) | ||
|
||
def forward(self, x, edge_index, edge_attr): | ||
for layer in self.layers: | ||
x = layer(x, edge_index, edge_attr) | ||
return x | ||
|
||
|
||
class GNO(KernelNeuralOperator): | ||
def __init__( | ||
self, | ||
lifting_operator, | ||
projection_operator, | ||
edge_features, | ||
n_layers=10, | ||
internal_n_layers=0, | ||
inner_size=None, | ||
internal_layers=None, | ||
func=None | ||
): | ||
if func is None: | ||
func = Tanh | ||
|
||
super().__init__( | ||
lifting_operator=lifting_operator, | ||
integral_kernels=GraphNeuralKernel( | ||
width=lifting_operator.out_features, | ||
edge_features=edge_features, | ||
internal_n_layers=internal_n_layers, | ||
inner_size=inner_size, | ||
internal_layers=internal_layers, | ||
func=func, | ||
n_layers=n_layers, | ||
), | ||
projection_operator=projection_operator | ||
) | ||
|
||
def forward(self, batch): | ||
x, edge_index, edge_attr = batch.x, batch.edge_index, batch.edge_attr | ||
x = self.lifting_operator(x) | ||
for kernel in self.kernels: | ||
x = kernel(x, edge_index, edge_attr) | ||
x = self.tanh(x) | ||
x = self.integral_kernels(x, edge_index, edge_attr) | ||
x = self.projection_operator(x) | ||
return x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters