|
|
""" |
|
|
Augmented Neural ODE Model Architecture |
|
|
|
|
|
Augmented ODE extends vanilla ODE by appending encoded features to the state space. |
|
|
The vector field operates on [x; aug(x); u] where aug(x) = encoder(x) and u is external input. |
|
|
Only the original x dimensions are used for reconstruction loss. |
|
|
""" |
|
|
|
|
|
import torch |
|
|
import torch.optim as optim |
|
|
import itertools |
|
|
import torchdiffeq |
|
|
|
|
|
from ..base import BaseModel |
|
|
from ..components.mlp import MLPWithCustomInit |
|
|
from ..components.initialization import init_autoencoder_network, init_dynamics_network |
|
|
from ..utils import interpolate_external_input, create_vector_field_with_external_input, interpolate_trajectory |
|
|
|
|
|
|
|
|
class AugmentedODEModel(BaseModel): |
|
|
def __init__(self, config, device): |
|
|
super().__init__(config, device) |
|
|
input_dim = config['input_dim'] |
|
|
hidden_dims_enc = config.get('hidden_dims_enc', [32, 64]) |
|
|
hidden_dims_vector_field = config['hidden_dims_vector_field'] |
|
|
|
|
|
|
|
|
aug_dim = config.get('aug_dim', input_dim) |
|
|
|
|
|
|
|
|
external_input_dim = config.get('external_input_dim', 0) |
|
|
|
|
|
|
|
|
total_dim = input_dim + aug_dim + external_input_dim |
|
|
vector_field_output_dim = input_dim + aug_dim |
|
|
|
|
|
|
|
|
encoder_activations = config.get('encoder_activations', None) |
|
|
vector_field_activations = config.get('vector_field_activations', None) |
|
|
default_activation = getattr(torch.nn, config.get('default_activation', 'ReLU')) |
|
|
|
|
|
|
|
|
self.encoder = MLPWithCustomInit( |
|
|
input_dim, hidden_dims_enc, aug_dim, |
|
|
activation=default_activation, |
|
|
activation_per_layer=encoder_activations |
|
|
).to(device) |
|
|
|
|
|
|
|
|
self.vector_field = MLPWithCustomInit( |
|
|
total_dim, hidden_dims_vector_field, vector_field_output_dim, |
|
|
activation=default_activation, |
|
|
activation_per_layer=vector_field_activations |
|
|
).to(device) |
|
|
|
|
|
|
|
|
init_autoencoder_network(self.encoder) |
|
|
init_dynamics_network(self.vector_field, scale=config['dynamics_init_scale']) |
|
|
|
|
|
|
|
|
self.optimizers = { |
|
|
'encoder': optim.Adam(self.encoder.parameters(), |
|
|
lr=config.get('encoder_lr', config['learning_rate'])), |
|
|
'vector_field': optim.Adam(self.vector_field.parameters(), |
|
|
lr=config.get('vector_field_lr', config['learning_rate'])) |
|
|
} |
|
|
|
|
|
|
|
|
self.encoder.print_architecture("Augmentation Encoder") |
|
|
self.vector_field.print_architecture("Augmented Vector Field") |
|
|
|
|
|
|
|
|
config['aug_dim'] = aug_dim |
|
|
config['total_dim'] = input_dim + aug_dim |
|
|
config['vector_field_input_dim'] = total_dim |
|
|
|
|
|
print(f"Augmented ODE Configuration:") |
|
|
print(f" Original dimension: {input_dim}") |
|
|
print(f" Augmentation dimension: {aug_dim}") |
|
|
print(f" External input dimension: {external_input_dim}") |
|
|
print(f" Total state dimension: {input_dim + aug_dim}") |
|
|
print(f" Vector field input dimension: {total_dim}") |
|
|
|
|
|
total_params = sum(p.numel() for p in itertools.chain( |
|
|
self.encoder.parameters(), self.vector_field.parameters())) |
|
|
print(f"Total parameters: {total_params}") |
|
|
|
|
|
def _augment_state(self, x): |
|
|
""" |
|
|
Augment state x with encoded features. |
|
|
|
|
|
Args: |
|
|
x: State tensor [B, input_dim] or [T, B, input_dim] |
|
|
|
|
|
Returns: |
|
|
x_aug: Augmented state [B, total_dim] or [T, B, total_dim] |
|
|
""" |
|
|
original_shape = x.shape |
|
|
|
|
|
|
|
|
if len(original_shape) == 3: |
|
|
T, B, D = original_shape |
|
|
x_flat = x.reshape(T * B, D) |
|
|
else: |
|
|
x_flat = x |
|
|
|
|
|
|
|
|
aug_features = self.encoder([], x_flat) |
|
|
|
|
|
|
|
|
x_aug_flat = torch.cat([x_flat, aug_features], dim=1) |
|
|
|
|
|
|
|
|
if len(original_shape) == 3: |
|
|
x_aug = x_aug_flat.reshape(T, B, self.config['total_dim']) |
|
|
else: |
|
|
x_aug = x_aug_flat |
|
|
|
|
|
return x_aug |
|
|
|
|
|
def _extract_original_state(self, x_aug): |
|
|
""" |
|
|
Extract original state from augmented state. |
|
|
|
|
|
Args: |
|
|
x_aug: Augmented state [..., total_dim] |
|
|
|
|
|
Returns: |
|
|
x: Original state [..., input_dim] |
|
|
""" |
|
|
return x_aug[..., :self.config['input_dim']] |
|
|
|
|
|
def inference(self, xt_batch, ut_batch=None): |
|
|
|
|
|
state_dim = self.config['input_dim'] |
|
|
x_batch = xt_batch[:, :, :state_dim] |
|
|
t_batch = xt_batch[:, :, state_dim] |
|
|
B, L, D = x_batch.shape |
|
|
|
|
|
|
|
|
x0 = x_batch[:, 0, :] |
|
|
x0_aug = self._augment_state(x0) |
|
|
|
|
|
|
|
|
t_batch_relative = t_batch - t_batch[:, 0:1] |
|
|
max_time = torch.max(t_batch_relative[:, -1]) |
|
|
t_eval = torch.linspace(0, max_time.item() + 1e-5, steps=L, device=self.device) |
|
|
|
|
|
|
|
|
u_interp = None |
|
|
if ut_batch is not None and self.has_external_input: |
|
|
|
|
|
t_batch_u = ut_batch[:, :, -1] |
|
|
t_batch_u = t_batch_u - t_batch[:, 0:1] |
|
|
|
|
|
|
|
|
u_interp = interpolate_trajectory( |
|
|
t_eval, |
|
|
t_batch_u, |
|
|
ut_batch[:, :, :-1].permute(1, 0, 2) |
|
|
).permute(1, 0, 2) |
|
|
|
|
|
|
|
|
def augmented_vector_field_with_input(t, x_aug): |
|
|
if u_interp is None: |
|
|
return self.vector_field(t, x_aug) |
|
|
|
|
|
|
|
|
t_idx = torch.searchsorted(t_eval, t, right=False) |
|
|
t_idx = torch.clamp(t_idx, 0, len(t_eval) - 1) |
|
|
|
|
|
|
|
|
u_t = u_interp[t_idx, :, :] |
|
|
|
|
|
|
|
|
x_aug_u = torch.cat([x_aug, u_t], dim=-1) |
|
|
|
|
|
return self.vector_field(t, x_aug_u) |
|
|
|
|
|
vector_field_func = augmented_vector_field_with_input |
|
|
|
|
|
|
|
|
x_aug_trajectory = torchdiffeq.odeint( |
|
|
vector_field_func, x0_aug, t_eval, |
|
|
method=self.config.get('ode_method', 'rk4') |
|
|
) |
|
|
|
|
|
|
|
|
x_trajectory = self._extract_original_state(x_aug_trajectory) |
|
|
|
|
|
return { |
|
|
'x0': x0, |
|
|
't_batch': t_batch, |
|
|
't_batch_relative': t_batch_relative, |
|
|
't_eval': t_eval, |
|
|
'x_trajectory': x_trajectory, |
|
|
'x_aug_trajectory': x_aug_trajectory, |
|
|
'u_interp': u_interp, |
|
|
} |
|
|
|