-
Notifications
You must be signed in to change notification settings - Fork 64
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
Showing
4 changed files
with
128 additions
and
257 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,79 +1,42 @@ | ||
import torch | ||
from pina.model.layers.gnn_layer import GNN_Layer | ||
from gnn_layer import GNN_Layer | ||
from pina import LabelTensor | ||
|
||
class GNN(torch.nn.Module): | ||
""" | ||
Message passing model class. | ||
""" | ||
def __init__(self, | ||
time_window: int, | ||
n_variables: int, | ||
t_max: float, | ||
embedding_dimension: int = 128, | ||
processing_layers: int = 6): | ||
""" | ||
Initialize Message Passing model class. | ||
Args: | ||
time_window: temporal bundling parameter | ||
n_variables: number of paramaters of the PDE | ||
output_dimension: dimension of the output | ||
embedding_dimension: dimension of node features | ||
processing_layers: number of message passing layers | ||
""" | ||
|
||
|
||
def __init__(self, time_window, t_max, n_variables, embedding_dimension=128, processing_layers=6): | ||
super().__init__() | ||
self.output_dimension = time_window | ||
self.embedding_dimension = embedding_dimension | ||
self.processing_layers = processing_layers | ||
self.time_window = time_window | ||
self.n_variables = n_variables | ||
self.t_max = t_max | ||
|
||
self.encoder = torch.nn.Sequential(torch.nn.Linear(self.time_window + self.n_variables + 1, self.embedding_dimension), | ||
torch.nn.SiLU(), | ||
torch.nn.Linear(self.embedding_dimension, self.embedding_dimension), | ||
torch.nn.SiLU()) | ||
|
||
# Encoder | ||
# TODO: the user should be able to define as many layers as wanted | ||
self.encoder = torch.nn.Sequential(torch.nn.Linear(self.time_window + self.n_variables +1, self.embedding_dimension), torch.nn.SiLU(), | ||
torch.nn.Linear(self.embedding_dimension, self.embedding_dimension), torch.nn.SiLU()) | ||
|
||
# GNN layers | ||
self.gnn_layers = torch.nn.ModuleList(modules=(GNN_Layer(in_features=self.embedding_dimension, | ||
self.gnn_layers = torch.nn.ModuleList(modules=(GNN_Layer(in_features=self.embedding_dimension, | ||
hidden_features=self.embedding_dimension, | ||
out_features=self.embedding_dimension, | ||
time_window=self.time_window, | ||
n_variables=self.n_variables) for _ in range(self.processing_layers))) | ||
|
||
# Decoder | ||
# TODO: use a linear layer after convolutions to allow easier management of strides and kernel sizes. | ||
# However, it is not clean nor always correct: for self.embedding_dimension < 55, it is meaningless. | ||
# self.decoder = torch.nn.Sequential(torch.nn.Conv1d(in_channels=1, out_channels=8, kernel_size=15, stride=4), | ||
#torch.nn.SiLU(), | ||
#torch.nn.Conv1d(in_channels=8, out_channels=1, kernel_size=10, stride=1), | ||
#torch.nn.SiLU(), | ||
#torch.nn.Linear(in_features= (int((self.embedding_dimension-15)/4)-8), out_features=self.output_dimension)) | ||
|
||
# At the moment we use a fixed time_window = 25 | ||
self.decoder = torch.nn.Sequential(torch.nn.Conv1d(in_channels=1, out_channels=8, kernel_size=16, stride=3), | ||
torch.nn.SiLU(), | ||
torch.nn.SiLU(), | ||
torch.nn.Conv1d(in_channels=8, out_channels=1, kernel_size=14, stride=1)) | ||
|
||
def forward(self, graph): | ||
|
||
#Normalization | ||
vars = graph.variables.extract(['alpha', 'beta', 'gamma']) | ||
time = graph.variables.extract(['t'])/self.t_max | ||
graph.pos = graph.pos/graph.pos.max() | ||
|
||
# Encoder | ||
input = torch.cat((graph.x, graph.pos, time, vars), dim = -1) | ||
h = self.encoder(input) | ||
|
||
# Processor | ||
def forward(self, data, pos, time, variables, batch, edge_index, dt): | ||
pos = pos/pos.max() | ||
time = time/self.t_max | ||
var = torch.cat((time, variables), dim=-1) | ||
node_input = torch.cat((data, pos, var), dim=-1) | ||
h = self.encoder(node_input) | ||
for i in range(self.processing_layers): | ||
h = self.gnn_layers[i](h, graph.x, graph.pos, graph.variables, graph.edge_index, graph.batch) | ||
|
||
# Decoder -- controllare che funzioni dt | ||
dt = (torch.ones(1, self.time_window)*graph.dt).to(h.device) | ||
dt = torch.cumsum(dt, dim=1) | ||
h = self.gnn_layers[i](edge_index, h, data, pos, var, batch) | ||
dt = torch.cumsum((torch.ones(1, self.time_window)*dt).to(device=h.device), dim=1) | ||
diff = self.decoder(h[:, None]).squeeze(1) | ||
out = graph.x[:, -1].repeat(1, self.time_window) + dt*diff | ||
|
||
out = data[:,-1].repeat(1, self.time_window) + dt*diff | ||
return out |
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,30 +1,44 @@ | ||
from torch_geometric.data import Data | ||
from torch_cluster import radius_graph | ||
|
||
from torch_geometric.nn import radius_graph | ||
from pina import LabelTensor | ||
import torch | ||
|
||
class GraphHandler(): | ||
""" | ||
Creates and manages a graph with following attrubutes: | ||
- graph.u: values of u(x,t) at point x and time t in considered window | ||
- graph.pos: spatial coordinates | ||
- graph.variables: variables of the equation (time and parameters) | ||
- graph.x: node features | ||
""" | ||
def __init__(self, dt, num_neighs=10): | ||
|
||
def __init__(self, dt, neighbors = 2): | ||
super().__init__() | ||
self.num_neighs = num_neighs | ||
self.neighbors = neighbors | ||
self.dt = dt | ||
self.graph = None | ||
|
||
|
||
def create_ball_graph(self, coordinates, data, variables, batch): | ||
def create_graph(self, pts, labels, steps): | ||
time_window = labels.shape[1] | ||
x = torch.cat([pts[i,:,st-time_window:st].extract(['u']) for i,st in enumerate(steps)]).squeeze(-1) | ||
coordinates = torch.Tensor(torch.cat([pts[i,:,st].extract(['x']) for i,st in enumerate(steps)])).squeeze(-1) | ||
variables = torch.cat([pts[i,:,st].extract(['alpha', 'beta', 'gamma']) for i,st in enumerate(steps)]) | ||
variables = LabelTensor(variables, labels=['alpha', 'beta', 'gamma']) | ||
time = torch.cat([pts[i,:,st].extract(['t']) for i,st in enumerate(steps)]) | ||
time = LabelTensor(time, labels=['t']) | ||
num_x = torch.unique(coordinates).shape[0] | ||
batch = torch.cat([torch.ones(num_x)*i for i in range(pts.shape[0])]).to(device=pts.device) | ||
dx = coordinates[1] - coordinates[0] | ||
radius = self.num_neighs * dx + 0.000001 | ||
radius = self.neighbors * dx + 0.0001 | ||
edge_index = radius_graph(coordinates, r=radius, loop=False, batch=batch) | ||
|
||
graph = Data(x = data, edge_index = edge_index, edge_attr = None) | ||
graph = Data(x=x, edge_index=edge_index, edge_attr=None) | ||
graph.y = labels | ||
graph.pos = coordinates.unsqueeze(-1) | ||
graph.variables = variables | ||
graph.dt = self.dt | ||
graph.batch = batch | ||
return graph | ||
graph.batch = batch.long() | ||
graph.time = time | ||
return graph | ||
|
||
|
||
def update_graph(self, graph, pred, labels, steps, batch_size): | ||
time_window = labels.shape[1] | ||
graph.x = torch.cat((graph.x, pred), dim=1)[:,time_window:] | ||
graph.y = labels | ||
num_x = labels.shape[0] // batch_size | ||
time = [torch.ones(num_x)*steps[i]*self.dt for i in range(len(steps))] | ||
graph.time = torch.cat(time).unsqueeze(-1).to(device=pred.device) | ||
return graph |
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
Oops, something went wrong.