Repository navigation
Expand file tree
/
Copy pathDynamicalSystem.py
More file actions
84 lines (75 loc) · 3.3 KB
/
Copy pathDynamicalSystem.py
File metadata and controls
84 lines (75 loc) · 3.3 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import torch
from torch import nn
import numpy as np
from methods import RationalQuadratic, trial_wise_matmul
from mlp import MLP_h
from LtrRNN import LtrRNN
import torch.nn.functional as F
from torchdiffeq import odeint
import math
# from low_tensor_rank_RNN.TimeConvDecoder import TimeConvDecoder
class DynamicalSystem(nn.Module):
def __init__(self, input_size,
hidden_size,
output_size,
rank,
sampling_rate,
TF_interval,
ortho = False,
original_space = False,
real_data = False,
activation_func='relu',
dataset='Lorenz',
device='cpu'):
super(DynamicalSystem, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
self.rank = rank
self.sampling_rate = sampling_rate
self.activation_func = activation_func
self.device = device
self.interval = TF_interval
self.ortho = ortho
self.real_data = real_data
self.original_space = original_space
self.decoder = nn.Linear(self.hidden_size,self.output_size)
self.LtrRNN = LtrRNN(input_size=self.input_size,
hidden_size=self.hidden_size,
output_size=self.output_size,
rank=self.rank,
sampling_rate = self.sampling_rate,
ortho = self.ortho,
activation_func=self.activation_func,
dataset=dataset,
device=self.device)
self.dataset = dataset
self.b_h = nn.Parameter(torch.empty((output_size,1)),requires_grad=True)
nn.init.kaiming_normal_(self.b_h, a=math.sqrt(5), mode='fan_out', nonlinearity='relu')
fan_in, _ = nn.init._calculate_fan_in_and_fan_out(self.b_h)
def forward(self, x,u=None):
z_pred = torch.zeros_like(x).to(self.device)
T = x.shape[3]
z = x[:,:,:,0:1]
self.softplus = nn.Softplus()
for t in range(T):
if (t%self.interval == 0) and (t>0):
z = x[:, :, :, t-1:t]
if self.dataset == 'continuous_attractor': # and self.original_space:
_ , z = self.LtrRNN(z,u)
else:
_ , z = self.LtrRNN(z)
z_pred[:,:,:,t:t+1]=z
if self.dataset == 'continuous_attractor':
output = z_pred
mlp_reconstruction = x
else:
if self.real_data:
output = torch.permute(self.softplus(self.decoder(torch.permute(z_pred,(0,1,3,2)))),(0,1,3,2))
mlp_reconstruction = torch.permute(self.softplus(self.decoder(torch.permute(x,(0,1,3,2)))),(0,1,3,2))
else:
output = torch.permute(self.decoder(torch.permute(z_pred,(0,1,3,2))),(0,1,3,2))
mlp_reconstruction = torch.permute(self.decoder(torch.permute(x,(0,1,3,2))),(0,1,3,2))
return output,z_pred,mlp_reconstruction
def set_time(self,time):
self.LtrRNN.set_time(time=time)