Delete src/drone_esn_training.py with huggingface_hub
Browse files- src/drone_esn_training.py +0 -138
src/drone_esn_training.py
DELETED
|
@@ -1,138 +0,0 @@
|
|
| 1 |
-
import numpy as np
|
| 2 |
-
import pickle
|
| 3 |
-
import scipy.sparse
|
| 4 |
-
import os
|
| 5 |
-
from utils import simulate
|
| 6 |
-
|
| 7 |
-
def train_drone_esn(data_path="drone_dataset.npz"):
|
| 8 |
-
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 9 |
-
if not os.path.isabs(data_path):
|
| 10 |
-
data_path = os.path.join(BASE_DIR, data_path)
|
| 11 |
-
|
| 12 |
-
print("Loading data...")
|
| 13 |
-
if not os.path.exists(data_path):
|
| 14 |
-
print(f"{data_path} not found. Run drone_data_collection.py first.")
|
| 15 |
-
return
|
| 16 |
-
|
| 17 |
-
data = np.load(data_path)
|
| 18 |
-
states = data['states'] # shape (T, 12) or (T, 20)
|
| 19 |
-
targets = data['targets'] # shape (T, 4)
|
| 20 |
-
|
| 21 |
-
Nu = states.shape[1]
|
| 22 |
-
Ny = targets.shape[1]
|
| 23 |
-
|
| 24 |
-
# Let's use 80% for training and 20% for testing
|
| 25 |
-
split = int(0.8 * len(states))
|
| 26 |
-
train_u = states[:split]
|
| 27 |
-
train_label = targets[:split]
|
| 28 |
-
test_u = states[split:]
|
| 29 |
-
test_label = targets[split:]
|
| 30 |
-
|
| 31 |
-
print("Loading biological matrices...")
|
| 32 |
-
try:
|
| 33 |
-
with open(os.path.join(BASE_DIR, 'W.pkl'), 'rb') as f:
|
| 34 |
-
W = pickle.load(f)
|
| 35 |
-
Nx = W.shape[0]
|
| 36 |
-
|
| 37 |
-
with open(os.path.join(BASE_DIR, 'W_in.pkl'), 'rb') as f:
|
| 38 |
-
Win_loaded = pickle.load(f)
|
| 39 |
-
|
| 40 |
-
with open(os.path.join(BASE_DIR, 'W_out.pkl'), 'rb') as f:
|
| 41 |
-
Wout_loaded = pickle.load(f)
|
| 42 |
-
|
| 43 |
-
with open(os.path.join(BASE_DIR, 'bias.pkl'), 'rb') as f:
|
| 44 |
-
bias_loaded = pickle.load(f)
|
| 45 |
-
|
| 46 |
-
print("Creating 50K Bi-Hemispheric Brain...")
|
| 47 |
-
W = scipy.sparse.block_diag((W, W))
|
| 48 |
-
Win_loaded = np.concatenate((Win_loaded, Win_loaded))
|
| 49 |
-
Wout_loaded = np.vstack((Wout_loaded, Wout_loaded))
|
| 50 |
-
bias_loaded = np.concatenate((bias_loaded, bias_loaded))
|
| 51 |
-
Nx = W.shape[0] # Update Nx to 50,000
|
| 52 |
-
|
| 53 |
-
except FileNotFoundError:
|
| 54 |
-
print("Biological matrices (W.pkl, etc.) not found! Falling back to a standard random ESN (Nx=500)...")
|
| 55 |
-
Nx = 500
|
| 56 |
-
W = scipy.sparse.random(Nx, Nx, density=0.1, format='csr')
|
| 57 |
-
Win_loaded = np.random.randn(Nx)
|
| 58 |
-
Wout_loaded = np.ones((Nx, 1)) # Select all neurons
|
| 59 |
-
bias_loaded = np.random.randn(Nx, 1) * 0.1
|
| 60 |
-
|
| 61 |
-
# Scale spectral radius to 0.99 (Bypass for 50k as W.pkl is already scaled)
|
| 62 |
-
if Nx < 25000:
|
| 63 |
-
try:
|
| 64 |
-
radius, _ = scipy.sparse.linalg.eigs(W, k=1, which='LM', maxiter=10000, tol=1e-2)
|
| 65 |
-
radius = abs(np.linalg.norm(radius))
|
| 66 |
-
except:
|
| 67 |
-
radius = max(abs(np.linalg.eigvals(W.toarray())))
|
| 68 |
-
W = W * (0.99 / radius)
|
| 69 |
-
|
| 70 |
-
# Ensure Win_loaded and bias_loaded are 1D arrays to prevent sequence assignment error
|
| 71 |
-
Win_loaded = np.asarray(Win_loaded).flatten()
|
| 72 |
-
bias_loaded = np.asarray(bias_loaded).flatten()
|
| 73 |
-
|
| 74 |
-
# Reshape Win as done in main.ipynb
|
| 75 |
-
Win_loaded_reshaped = np.zeros((Nu, Nx))
|
| 76 |
-
for i in range(Nx):
|
| 77 |
-
row = np.random.choice(Nu)
|
| 78 |
-
Win_loaded_reshaped[row, i] = Win_loaded[i]
|
| 79 |
-
|
| 80 |
-
Win = Win_loaded_reshaped
|
| 81 |
-
Win = np.vstack((Win, bias_loaded.T)).transpose()
|
| 82 |
-
|
| 83 |
-
input_scaling = 0.7
|
| 84 |
-
Win[:, :-1] = Win[:, :-1] * input_scaling
|
| 85 |
-
|
| 86 |
-
Wout_mask = Wout_loaded[:, 0]
|
| 87 |
-
mask = (Wout_mask == 1)
|
| 88 |
-
|
| 89 |
-
print("Simulating reservoir on training data...")
|
| 90 |
-
step = 0.9 # 1 - leakage rate
|
| 91 |
-
|
| 92 |
-
# Need to simulate washout. Just use initial state (e.g. 100 steps)
|
| 93 |
-
wash_u = train_u[:100]
|
| 94 |
-
x0 = np.random.random((Nx, 1))
|
| 95 |
-
reservoir_wash = simulate(x0, W, Win, np.hstack((wash_u, np.ones((len(wash_u), 1)))), step)
|
| 96 |
-
|
| 97 |
-
x0 = reservoir_wash[:, -1].reshape((Nx, 1))
|
| 98 |
-
|
| 99 |
-
# Train
|
| 100 |
-
train_u_sim = train_u[100:]
|
| 101 |
-
train_label_sim = train_label[100:]
|
| 102 |
-
train_size = len(train_u_sim)
|
| 103 |
-
|
| 104 |
-
# High RAM limit (64GB) allows using maximum precision (Float64)
|
| 105 |
-
XX = np.zeros((len(np.where(mask)[0]) + 1, len(np.where(mask)[0]) + 1))
|
| 106 |
-
XY = np.zeros((Ny, len(np.where(mask)[0]) + 1))
|
| 107 |
-
Yhat = train_label_sim.T
|
| 108 |
-
|
| 109 |
-
XX, XY, reservoir = simulate(x0, W, Win, np.hstack((train_u_sim, np.ones((train_size, 1)))), step,
|
| 110 |
-
XX, XY, Yhat, building_matrices=True, mask=mask)
|
| 111 |
-
|
| 112 |
-
beta = 1e-6
|
| 113 |
-
print("Performing Ridge Regression (using np.linalg.solve in Float64)...")
|
| 114 |
-
# np.linalg.solve avoids explicit matrix inversion, making it faster and more stable
|
| 115 |
-
Wout = np.linalg.solve(XX + beta * np.identity(XX.shape[0]), XY.T).T
|
| 116 |
-
|
| 117 |
-
# Test error
|
| 118 |
-
X = np.vstack((np.ones((1, train_size)), reservoir.reshape((Nx, train_size))[mask, :]))
|
| 119 |
-
Y_train = Wout @ X
|
| 120 |
-
err_train = np.sqrt(np.mean((Y_train - Yhat)**2))
|
| 121 |
-
print(f"Training RMSE: {err_train}")
|
| 122 |
-
|
| 123 |
-
# Save the trained components
|
| 124 |
-
print("Saving trained network parameters to W_drone_components.pkl...")
|
| 125 |
-
components = {
|
| 126 |
-
'W': W,
|
| 127 |
-
'Win': Win,
|
| 128 |
-
'Wout': Wout,
|
| 129 |
-
'mask': mask,
|
| 130 |
-
'alpha': step
|
| 131 |
-
}
|
| 132 |
-
components_path = os.path.join(BASE_DIR, "W_drone_components.pkl")
|
| 133 |
-
with open(components_path, "wb") as f:
|
| 134 |
-
pickle.dump(components, f)
|
| 135 |
-
print("Training complete!")
|
| 136 |
-
|
| 137 |
-
if __name__ == "__main__":
|
| 138 |
-
train_drone_esn()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|