forked from forgi86/dynonet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
WH2009_train.py
178 lines (134 loc) · 4.59 KB
/
WH2009_train.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
169
170
171
172
import torch
import pandas as pd
import numpy as np
import os
from dynonet.lti import SisoLinearDynamicalOperator
from dynonet.static import SisoStaticNonLinearity
import matplotlib.pyplot as plt
import time
import torch.nn as nn
import dynonet.metrics
# In[Main]
if __name__ == '__main__':
# In[Set seed for reproducibility]
np.random.seed(0)
torch.manual_seed(0)
# In[Settings]
lr_ADAM = 2e-4
lr_BFGS = 1e0
num_iter_ADAM = 40000 # ADAM iterations 20000
num_iter_BFGS = 0 # final BFGS iterations
msg_freq = 100
n_skip = 5000
n_fit = 20000
decimate = 1
n_batch = 1
n_b = 8
n_a = 8
model_name = "model_WH"
num_iter = num_iter_ADAM + num_iter_BFGS
# In[Column names in the dataset]
COL_F = ['fs']
COL_U = ['uBenchMark']
COL_Y = ['yBenchMark']
# In[Load dataset]
df_X = pd.read_csv(os.path.join("data", "WienerHammerBenchmark.csv"))
# Extract data
y = np.array(df_X[COL_Y], dtype=np.float32) # batch, time, channel
u = np.array(df_X[COL_U], dtype=np.float32)
fs = np.array(df_X[COL_F].iloc[0], dtype=np.float32)
N = y.size
ts = 1/fs
t = np.arange(N)*ts
# In[Fit data]
y_fit = y[0:n_fit:decimate]
u_fit = u[0:n_fit:decimate]
t_fit = t[0:n_fit:decimate]
# In[Prepare training tensors]
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)
# In[Prepare model]
G1 = SisoLinearDynamicalOperator(n_b, n_a, n_k=1)
F_nl = SisoStaticNonLinearity(n_hidden=10, activation='tanh')
G2 = SisoLinearDynamicalOperator(n_b, n_a)
def model(u_in):
y1_lin = G1(u_fit_torch)
y1_nl = F_nl(y1_lin)
y_hat = G2(y1_nl)
return y_hat, y1_nl, y1_lin
# In[Setup optimizer]
optimizer_ADAM = torch.optim.Adam([
{'params': G1.parameters(), 'lr': lr_ADAM},
{'params': G2.parameters(), 'lr': lr_ADAM},
{'params': F_nl.parameters(), 'lr': lr_ADAM},
], lr=lr_ADAM)
optimizer_LBFGS = torch.optim.LBFGS(list(G1.parameters()) + list(G2.parameters()) + list(F_nl.parameters()), lr=lr_BFGS)
def closure():
optimizer_LBFGS.zero_grad()
# Simulate
y_hat, y1_nl, y1_lin = model(u_fit_torch)
# Compute fit loss
err_fit = y_fit_torch[:, n_skip:, :] - y_hat[:, n_skip:, :]
loss = torch.mean(err_fit**2)*1000
# Backward pas
loss.backward()
return loss
# In[Train]
LOSS = []
start_time = time.time()
for itr in range(0, num_iter):
if itr < num_iter_ADAM:
msg_freq = 10
loss_train = optimizer_ADAM.step(closure)
else:
msg_freq = 10
loss_train = optimizer_LBFGS.step(closure)
LOSS.append(loss_train.item())
if itr % msg_freq == 0:
with torch.no_grad():
RMSE = torch.sqrt(loss_train)
print(f'Iter {itr} | Fit Loss {loss_train:.6f} | RMSE:{RMSE:.4f}')
train_time = time.time() - start_time
print(f"\nTrain time: {train_time:.2f}")
# In[Save model]
model_folder = os.path.join("models", model_name)
if not os.path.exists(model_folder):
os.makedirs(model_folder)
torch.save(G1.state_dict(), os.path.join(model_folder, "G1.pkl"))
torch.save(F_nl.state_dict(), os.path.join(model_folder, "F_nl.pkl"))
torch.save(G2.state_dict(), os.path.join(model_folder, "G2.pkl"))
# In[Simulate one more time]
with torch.no_grad():
y_hat, y1_nl, y1_lin = model(u_fit_torch)
# In[Detach]
y_hat = y_hat.detach().numpy()[0, :, :]
y1_lin = y1_lin.detach().numpy()[0, :, :]
y1_nl = y1_nl.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()
# In[Plot loss]
plt.figure()
plt.plot(LOSS)
plt.grid(True)
plt.show()
# In[Plot static non-linearity]
y1_lin_min = np.min(y1_lin)
y1_lin_max = np.max(y1_lin)
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}") # target: 1mv