File size: 1,723 Bytes
0252cb7 | 1 2 3 4 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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | 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]
|