-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsilverbox_train_WH.py
174 lines (129 loc) · 4.13 KB
/
silverbox_train_WH.py
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import torch
import pandas as pd
import numpy as np
import os
from dynonet.lti import SisoLinearDynamicalOperator
import matplotlib.pyplot as plt
import time
import torch.nn as nn
import dynonet.metrics
class StaticNonLin(nn.Module):
def __init__(self):
super(StaticNonLin, self).__init__()
self.net = nn.Sequential(
nn.Linear(1, 20), # 2 states, 1 input
nn.Tanh(),
nn.Linear(20, 1)
)
for m in self.net.modules():
if isinstance(m, nn.Linear):
nn.init.normal_(m.weight, mean=0, std=1e-3)
nn.init.constant_(m.bias, val=0)
def forward(self, y_lin):
y_nl = y_lin + self.net(y_lin)
return y_nl
if __name__ == '__main__':
# Set seed for reproducibility
np.random.seed(0)
torch.manual_seed(0)
# Settings
add_noise = True
lr = 1e-3
num_iter = 10000
test_freq = 100
n_fit = 40000
decimate = 1
n_batch = 1
n_b = 3
n_a = 3
# Column names in the dataset
COL_U = ['V1']
COL_Y = ['V2']
# Load dataset
df_X = pd.read_csv(os.path.join("data", "SNLS80mV.csv"))
# Extract data
y = np.array(df_X[COL_Y], dtype=np.float32)
u = np.array(df_X[COL_U], dtype=np.float32)
u = u - np.mean(u)
fs = 10**7/2**14
N = y.size
ts = 1/fs
t = np.arange(N)*ts
# Fit data
y_fit = y[:n_fit:decimate]
u_fit = u[:n_fit:decimate]
t_fit = t[0:n_fit:decimate]
# Prepare data
u_fit_torch = torch.tensor(u_fit[None, :, :], dtype=torch.float, requires_grad=False)
y_fit_torch = torch.tensor(y_fit[None, :, :], dtype=torch.float)
# First dynamical system custom defined
G1 = SisoLinearDynamicalOperator(n_b=n_b, n_a=n_a)
y_init_1 = torch.zeros((n_batch, n_a), dtype=torch.float)
u_init_1 = torch.zeros((n_batch, n_b), dtype=torch.float)
# Second dynamical system custom defined
G2 = SisoLinearDynamicalOperator(n_b=n_b, n_a=n_a)
y_init_2 = torch.zeros((n_batch, n_a), dtype=torch.float)
u_init_2 = torch.zeros((n_batch, n_b), dtype=torch.float)
# Static non-linearity
F_nl = StaticNonLin()
# Setup optimizer
optimizer = torch.optim.Adam([
{'params': G1.parameters(), 'lr': lr},
{'params': G2.parameters(), 'lr': lr},
{'params': F_nl.parameters(), 'lr': lr},
], lr=lr)
# In[Train]
LOSS = []
start_time = time.time()
for itr in range(0, num_iter):
optimizer.zero_grad()
# Simulate
y1_lin = G1(u_fit_torch, y_init_1, u_init_1)
y1_nl = F_nl(y1_lin)
y_hat = G2(y1_nl, y_init_2, u_init_2)
# Compute fit loss
err_fit = y_fit_torch - y_hat
loss_fit = torch.mean(err_fit**2)
loss = loss_fit
LOSS.append(loss.item())
if itr % test_freq == 0:
with torch.no_grad():
RMSE = torch.sqrt(loss)
print(f'Iter {itr} | Fit Loss {loss_fit:.6f} | RMSE:{RMSE:.4f}')
# Optimize
loss.backward()
if itr == 100:
pass
optimizer.step()
train_time = time.time() - start_time
print(f"\nTrain time: {train_time:.2f}") # 182 seconds
# In[To numpy]
y_hat = y_hat.detach().numpy()[0, :, :]
y1_lin = y1_lin.detach().numpy()[0, :, :]
# In[Plot]
plt.figure()
plt.plot(t_fit, y_fit, 'k', label="$y$")
plt.plot(t_fit, y_hat, 'b', label="$\hat y$")
plt.legend()
plt.show()
plt.figure()
plt.plot(LOSS)
plt.grid(True)
plt.show()
# In[Plot static non-linearity]
y1_lin_min = np.min(y1_lin) - 1e-6
y1_lin_max = np.max(y1_lin) + 1e-6
in_nl = np.arange(y1_lin_min, y1_lin_max, (y1_lin_max- y1_lin_min)/1000).astype(np.float32).reshape(-1, 1)
with torch.no_grad():
out_nl = F_nl(torch.as_tensor(in_nl))
plt.figure()
plt.plot(in_nl, out_nl, 'b')
plt.plot(in_nl, out_nl, 'b')
#plt.plot(y1_lin, y1_nl, 'b*')
plt.xlabel('Static non-linearity input (-)')
plt.ylabel('Static non-linearity input (-)')
plt.grid(True)
plt.show()
# In[Plot]
e_rms = dynonet.metrics.error_rmse(y_hat, y_fit)[0]
print(f"RMSE: {e_rms:.2f}")