-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiffuser.py
More file actions
401 lines (325 loc) · 13.5 KB
/
Copy pathdiffuser.py
File metadata and controls
401 lines (325 loc) · 13.5 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# assume we have preoptimized W matrices
# network architecture follows:
# for ni dimensional vector input, we have n d=2,r pretrained liquid network in square mesh structure
#diffuse ni to n, meaning fill start of the square structure (first n, ni=n or project ni to n)
'''
state =
[0,0,0
0,0,0
0,0,0]
if the input is 3 dimensional, <x,y,z>
initial_state =
[x,y,z
0,0,0
0,0,0]
'''
#run resarvoir n steps
#flatten and feed trough an mlp
#optionally you can get the states and diffuse them to a bigger resarvoir and so on
#assume you read the weight matrices as numpy arrays.
# we run the resarvoir like:
'''
S = S0 # list of [batch_size, N]
for _ in range(T):
S_prev = states_list[-1] # [batch_size, N]
S_next = S_prev @ W#F.leaky_relu(, negative_slope=0.1)
states_list.append(S_next)
return states_list[-1]
'''
import torch
import torch.nn as nn
import numpy as np
import torch.nn.functional as F
class ReservoirLayer(nn.Module):
"""A single reservoir layer with pre-trained weights and optional input projection."""
def __init__(self, input_dim, reservoir_n, reservoir_d, reservoir_r, T, W_numpy):
super().__init__()
self.reservoir_n = reservoir_n
self.reservoir_d = reservoir_d
self.reservoir_r = reservoir_r
self.T = T # Number of reservoir update steps
self.N = reservoir_n ** reservoir_d # Reservoir size (n^d)
# Convert numpy weight matrix to PyTorch buffer
W_tensor = torch.from_numpy(W_numpy).float()
assert W_tensor.shape == (self.N, self.N), \
f"Expected W shape {(self.N, self.N)}, got {W_tensor.shape}"
self.W =nn.Parameter(W_tensor, requires_grad=True) # Make W a trainable parameter
# Create connectivity mask
self.connectivity_mask = (torch.tensor(W_numpy) != 0).float().to('cuda')
# Register backward hook to modify gradients
self.W.register_hook(lambda grad: grad * self.connectivity_mask)
def forward(self, x):
"""Process input through the reservoir.
Args:
x: Input tensor of shape [batch_size, input_dim]
Returns:
Final reservoir state of shape [batch_size, N]
"""
S = F.pad(x, (0, self.N - x.size(1)))
# Run reservoir updates
for _ in range(self.T):
S = F.leaky_relu(S @ self.W, negative_slope=0.1) # Reservoir state update
return S#[:,-self.reservoir_n:]
class ReservoirNetwork(nn.Module):
"""A network composed of multiple reservoir layers followed by an MLP head."""
def __init__(self, input_dim, output_dim, reservoir_layers, mlp_hidden_dims):
super().__init__()
self.reservoir_layers = nn.ModuleList(reservoir_layers)
mlp_layers = []
prev_dim = reservoir_layers[-1].N
for hidden_dim in mlp_hidden_dims:
mlp_layers.append(nn.Linear(prev_dim, hidden_dim))
mlp_layers.append(nn.ReLU())
prev_dim = hidden_dim
mlp_layers.append(nn.Linear(prev_dim, output_dim))
self.mlp = nn.Sequential(*mlp_layers)
def forward(self, x):
"""Process input through the entire network.
Args:
x: Input tensor of shape [batch_size, input_dim]
Returns:
Output tensor of shape [batch_size, output_dim]
"""
# Pass through reservoir layers
for layer in self.reservoir_layers:
x = layer(x)
# Pass through MLP head
return self.mlp(x)
# Example usage --------------------------------------------------------------
class MLP(nn.Module):
def __init__(self, input_dim=18, output_dim=18):
super().__init__()
self.model = nn.Sequential(
nn.Linear(input_dim, 324),
nn.ReLU(),
nn.Linear(324, 128),
nn.ReLU(),
nn.Linear(128, 64),
nn.ReLU(),
nn.Linear(64, output_dim)
)
def forward(self, x):
return self.model(x)
from thop import profile
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from tqdm import tqdm
import numpy as np
import pandas as pd
from torch.utils.data import TensorDataset, DataLoader, random_split
from synthdata import SyntheticTransformDataset
def create_time_series_dataset(df, feature_column, window_size):
"""
Convert a time series DataFrame into a supervised learning dataset.
Parameters:
df (pd.DataFrame): DataFrame containing the time series.
feature_column (str): The column name to use for features/target.
window_size (int): Number of time steps to include in each feature window.
Returns:
X (np.array): 2D array where each row is a window of features.
y (np.array): 1D array of target values.
"""
X, y = [], []
df['prev_close'] = df['Close'].shift(1)
# Calculate the percentage change from the previous close to the current close
df[feature_column] = ((df['Close'] - df['prev_close']) / df['prev_close'])
# Loop over the DataFrame such that we have enough data for the window and target
for i in range(window_size,len(df) - window_size):
window = df[feature_column].iloc[i : i + window_size].values
target = df[feature_column].iloc[i + window_size]
X.append(window)
y.append(target)
return np.array(X), np.array(y)
if __name__ == "__main__":
# 1. Load pre-trained weight matrices (example)
W1_numpy = np.load('W1.npy') # 10x10 grid (n=10, d=2)
W2_numpy = np.load('W2.npy') # 10x10 grid (n=10, d=2)
window = int((W2_numpy.shape[0])**0.5)
X,y = create_time_series_dataset(pd.read_csv('BTC-EUR.csv').dropna(),'Close',window)
print(W2_numpy.shape)
#W1_numpy = (np.random.rand(100, 100)-0.5)*2
# 2. Create reservoir layers
reservoir_layers = [
ReservoirLayer(input_dim=window, reservoir_n=window, reservoir_d=2,
reservoir_r=2, T=window//2, W_numpy=W2_numpy),
]
# 3. Create full network
# model = ReservoirNetwork(
# input_dim=window,
# output_dim=1,
# reservoir_layers=reservoir_layers,
# mlp_hidden_dims=[128, 64]
# ).to('cuda' if torch.cuda.is_available() else 'cpu')
model = MLP(input_dim=window,output_dim=1).to('cuda' if torch.cuda.is_available() else 'cpu')
X_tensor = torch.tensor(X, dtype=torch.float32)
y_tensor = torch.tensor(y, dtype=torch.float32)
# Create a TensorDataset from X and y
dataset = TensorDataset(X_tensor, y_tensor)
# Determine the split index (80% training, 20% testing)
total_samples = len(dataset)
train_size = int(0.8 * total_samples)
test_size = total_samples - train_size
# Alternatively, if you want to avoid random splitting for time series,
# simply slice the dataset rather than using random_split.
train_dataset = TensorDataset(X_tensor[:train_size], y_tensor[:train_size])
test_dataset = TensorDataset(X_tensor[train_size:], y_tensor[train_size:])
# Create DataLoaders
# For training, you might want to shuffle the data.
batch_size=32
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
# For testing, do not shuffle as you want to preserve the time order.
test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)
# 6. Create dataloaders
# 7. Training setup
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)
num_epochs = 50
# 8. Training loop
best_test_loss = float('inf')
device = next(model.parameters()).device
print(device)
import torch
import torch
from tqdm import tqdm
import numpy as np
import matplotlib.pyplot as plt
# Load data and create datasets
W2_numpy = np.load('W2.npy')
window = int(W2_numpy.shape[0]**0.5)
df = pd.read_csv('BTC-EUR.csv').dropna()
X, y = create_time_series_dataset(df, 'Close', window)
# Create datasets
X_tensor = torch.tensor(X, dtype=torch.float32)
y_tensor = torch.tensor(y, dtype=torch.float32)
dataset = TensorDataset(X_tensor, y_tensor)
train_size = int(0.8 * len(dataset))
test_size = len(dataset) - train_size
train_dataset, test_dataset = random_split(dataset, [train_size, test_size])
batch_size = 32
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)
# Calculate real target std once
real_std = np.std(y[train_size:])
# Initialize models
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
reservoir_layers = [
ReservoirLayer(window, window, 2, 2, window//2, W2_numpy)
]
reservoir_model = ReservoirNetwork(
input_dim=window,
output_dim=1,
reservoir_layers=reservoir_layers,
mlp_hidden_dims=[128, 64]
).to(device)
mlp_model = MLP(input_dim=window, output_dim=1).to(device)
# Training function
def train_model(model, name):
optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)
criterion = nn.MSELoss()
train_losses = []
test_losses = []
output_stds = []
best_outputs = []
best_targets = []
best_test_loss = float('inf')
for epoch in range(50):
# Training
model.train()
epoch_loss = 0
outputs_list = []
targets_list = []
for inputs, targets in train_loader:
inputs, targets = inputs.to(device), targets.to(device)
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, targets)
loss.backward()
optimizer.step()
epoch_loss += loss.item() * inputs.size(0)
outputs_list.extend(outputs.detach().cpu().numpy().flatten())
targets_list.extend(targets.detach().cpu().numpy().flatten())
train_loss = epoch_loss / len(train_dataset)
train_losses.append(train_loss)
output_std = np.std(outputs_list)
output_stds.append(output_std)
# Validation
model.eval()
test_loss = 0
epoch_outputs = []
epoch_targets = []
with torch.no_grad():
for inputs, targets in test_loader:
inputs, targets = inputs.to(device), targets.to(device)
outputs = model(inputs)
test_loss += criterion(outputs, targets).item() * inputs.size(0)
epoch_outputs.extend(outputs.cpu().numpy().flatten())
epoch_targets.extend(targets.cpu().numpy().flatten())
test_loss = test_loss / len(test_dataset)
test_losses.append(test_loss)
# Save best outputs
if test_loss < best_test_loss:
best_test_loss = test_loss
best_outputs = epoch_outputs
best_targets = epoch_targets
print(f"{name} Epoch {epoch+1}/50 | Train Loss: {train_loss:.4e} | Test Loss: {test_loss:.4e}")
return {
'train_losses': train_losses,
'test_losses': test_losses,
'output_stds': output_stds,
'best_outputs': best_outputs,
'best_targets': best_targets
}
# Train both models
reservoir_results = train_model(reservoir_model, "Reservoir")
mlp_results = train_model(mlp_model, "MLP")
# Plotting
plt.figure(figsize=(18, 5))
# Loss plot
plt.subplot(1, 3, 1)
plt.plot(reservoir_results['train_losses'], label='Reservoir Train')
plt.plot(reservoir_results['test_losses'], label='Reservoir Test')
plt.plot(mlp_results['train_losses'], label='MLP Train')
plt.plot(mlp_results['test_losses'], label='MLP Test')
plt.xlabel('Epoch')
plt.ylabel('MSE Loss')
plt.title('Training and Test Loss')
plt.legend()
plt.yscale('log')
# STD plot
plt.subplot(1, 3, 2)
plt.plot(reservoir_results['output_stds'], label='Reservoir Output')
plt.plot(mlp_results['output_stds'], label='MLP Output')
plt.axhline(real_std, color='r', linestyle='--', label='Real Target')
plt.xlabel('Epoch')
plt.ylabel('Standard Deviation')
plt.title('Output Standard Deviation')
plt.legend()
# Histogram plot (side by side)
plt.subplot(1, 3, 3)
plt.hist(reservoir_results['best_targets'], bins=50, alpha=0.7, label='Real Targets', color='blue')
plt.title('Real Targets')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.legend()
plt.figure(figsize=(18, 5))
plt.subplot(1, 3, 1)
plt.hist(reservoir_results['best_outputs'], bins=50, alpha=0.7, label='Reservoir Outputs', color='green')
plt.title('Reservoir Outputs')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.legend()
plt.subplot(1, 3, 2)
plt.hist(mlp_results['best_outputs'], bins=50, alpha=0.7, label='MLP Outputs', color='orange')
plt.title('MLP Outputs')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.legend()
plt.subplot(1, 3, 3)
plt.hist(reservoir_results['best_targets'], bins=50, alpha=0.7, label='Real Targets', color='blue')
plt.title('Real Targets')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.legend()
plt.tight_layout()
plt.show()