IDFF-code / torchcfm /models /models.py
rezapop's picture
IDFF code release v1.0.0 (mirror of GitHub)
0252cb7 verified
Raw History Blame Contribute Delete
1.72 kB
import torch
import torch.nn as nn
class MLP(torch.nn.Module):
def __init__(self, dim, out_dim=None, w=64, time_varying=False):
super().__init__()
self.time_varying = time_varying
if out_dim is None:
out_dim = dim
self.net = torch.nn.Sequential(
torch.nn.Linear(dim + (1 if time_varying else 0), w),
torch.nn.SELU(),
torch.nn.Linear(w, w),
torch.nn.SELU(),
torch.nn.Linear(w, w),
torch.nn.SELU(),
torch.nn.Linear(w, out_dim),
)
def forward(self, x):
return self.net(x)
class MLP_Embedding(torch.nn.Module):
def __init__(self, dim, out_dim=None, w=64, time_embed=100,time_varying=False):
super().__init__()
self.time_varying = time_varying
self.time_index_embedding = nn.Embedding(time_embed, w)
if out_dim is None:
out_dim = dim
self.net = torch.nn.Sequential(
torch.nn.Linear(dim + (1+w if time_varying else w), w),
torch.nn.SELU(),
torch.nn.Linear(w, w),
torch.nn.SELU(),
torch.nn.Linear(w, w),
torch.nn.SELU(),
torch.nn.Linear(w, out_dim),
)
def forward(self, x,ti):
embedded_tensor = self.time_index_embedding(ti.to(torch.long))
return self.net(torch.cat([x,embedded_tensor.squeeze()],dim=-1))
class GradModel(torch.nn.Module):
def __init__(self, action):
super().__init__()
self.action = action
def forward(self, x):
x = x.requires_grad_(True)
grad = torch.autograd.grad(torch.sum(self.action(x)), x, create_graph=True)[0]
return grad[:, :-1]