File size: 12,664 Bytes
61162bb |
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 |
"""Train BackpropNEAT on Spiral dataset."""
import numpy as np
import matplotlib.pyplot as plt
import jax.numpy as jnp
import jax
import os
import json
from datetime import datetime
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle
from neat.backprop_neat import BackpropNEAT
from neat.datasets import generate_spiral_dataset
from neat.network import Network
from neat.genome import Genome
class NetworkLogger:
"""Logger for tracking network evolution."""
def __init__(self, output_dir: str):
self.output_dir = output_dir
self.log_file = os.path.join(output_dir, "evolution_log.json")
self.history = []
def log_network(self, epoch: int, network, loss: float, accuracy: float):
"""Log network state."""
network_state = {
'epoch': epoch,
'loss': float(loss),
'accuracy': float(accuracy),
'n_nodes': network.genome.n_nodes,
'n_connections': len(network.genome.connections),
'complexity_score': self.calculate_complexity(network),
'structure': self.get_network_structure(network),
'timestamp': datetime.now().isoformat()
}
self.history.append(network_state)
# Save to file
with open(self.log_file, 'w') as f:
json.dump(self.history, f, indent=2)
def calculate_complexity(self, network):
"""Calculate network complexity score."""
n_nodes = network.genome.n_nodes
n_connections = len(network.genome.connections)
return n_nodes * 0.5 + n_connections
def get_network_structure(self, network):
"""Get detailed network structure."""
connections = []
for (src, dst), weight in network.genome.connections.items():
connections.append({
'source': int(src),
'target': int(dst),
'weight': float(weight)
})
return {
'input_size': network.genome.input_size,
'output_size': network.genome.output_size,
'hidden_nodes': network.genome.n_nodes - network.genome.input_size - network.genome.output_size,
'connections': connections
}
def plot_evolution(self, save_path: str):
"""Plot network evolution metrics."""
epochs = [log['epoch'] for log in self.history]
accuracies = [log['accuracy'] for log in self.history]
complexities = [log['complexity_score'] for log in self.history]
losses = [log['loss'] for log in self.history]
fig, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize=(12, 12))
# Plot accuracy
ax1.plot(epochs, accuracies, 'b-', label='Accuracy')
ax1.set_ylabel('Accuracy')
ax1.set_title('Network Evolution')
ax1.grid(True)
ax1.legend()
# Plot complexity
ax2.plot(epochs, complexities, 'r-', label='Complexity Score')
ax2.set_ylabel('Complexity Score')
ax2.grid(True)
ax2.legend()
# Plot loss
ax3.plot(epochs, losses, 'g-', label='Loss')
ax3.set_ylabel('Loss')
ax3.set_xlabel('Epoch')
ax3.grid(True)
ax3.legend()
plt.tight_layout()
plt.savefig(save_path, dpi=300, bbox_inches='tight')
plt.close()
def visualize_dataset(X, y, network=None, title=None, save_path=None):
"""Visualize dataset with decision boundary."""
plt.figure(figsize=(10, 8))
if network is not None:
# Create mesh grid
x_min, x_max = X[:, 0].min() - 0.5, X[:, 0].max() + 0.5
y_min, y_max = X[:, 1].min() - 0.5, X[:, 1].max() + 0.5
xx, yy = np.meshgrid(np.linspace(x_min, x_max, 100),
np.linspace(y_min, y_max, 100))
# Make predictions
X_mesh = jnp.array(np.c_[xx.ravel(), yy.ravel()], dtype=jnp.float32)
Z = network.predict(X_mesh)
Z = Z.reshape(xx.shape)
# Plot decision boundary
plt.contourf(xx, yy, Z, alpha=0.4, cmap='RdYlBu')
plt.scatter(X[y == 1, 0], X[y == 1, 1], c='red', label='Class 1')
plt.scatter(X[y == -1, 0], X[y == -1, 1], c='blue', label='Class -1')
plt.grid(True)
plt.legend()
plt.title(title or 'Dataset')
plt.xlabel('X1')
plt.ylabel('X2')
if save_path:
plt.savefig(save_path, dpi=300, bbox_inches='tight')
print(f"Saved plot to {save_path}")
else:
plt.show()
plt.close()
def train_network(network, X, y, n_epochs=300, batch_size=32, patience=50):
"""Train a single network."""
print("Starting network training...")
print(f"Input shape: {X.shape}, Output shape: {y.shape}")
print(f"Network params: {network.params['weights'].keys()}")
n_samples = len(X)
n_batches = n_samples // batch_size
best_accuracy = 0.0
patience_counter = 0
best_params = None
# Convert to JAX arrays
print("Converting to JAX arrays...")
X = jnp.array(X, dtype=jnp.float32)
y = jnp.array(y, dtype=jnp.float32)
# Learning rate schedule
base_lr = 0.01
warmup_epochs = 5
print(f"\nTraining for {n_epochs} epochs with {n_batches} batches per epoch")
print(f"Batch size: {batch_size}, Patience: {patience}")
for epoch in range(n_epochs):
try:
# Shuffle data
perm = np.random.permutation(n_samples)
X = X[perm]
y = y[perm]
# Adjust learning rate with warmup and cosine decay
if epoch < warmup_epochs:
lr = base_lr * (epoch + 1) / warmup_epochs
else:
# Cosine decay with restarts
cycle_length = 50
cycle = (epoch - warmup_epochs) // cycle_length
t = (epoch - warmup_epochs) % cycle_length
lr = base_lr * 0.5 * (1 + np.cos(t * np.pi / cycle_length))
# Add small restart bump every cycle
if t == 0:
lr = base_lr * (0.9 ** cycle)
epoch_loss = 0.0
# Train on mini-batches
for i in range(n_batches):
start_idx = i * batch_size
end_idx = start_idx + batch_size
X_batch = X[start_idx:end_idx]
y_batch = y[start_idx:end_idx]
try:
# Update network parameters
network.params, loss = network._train_step(
network.params,
X_batch,
y_batch
)
epoch_loss += loss
except Exception as e:
print(f"Error in batch {i}: {str(e)}")
print(f"X_batch shape: {X_batch.shape}, y_batch shape: {y_batch.shape}")
raise e
# Compute training accuracy
predictions = network.predict(X)
train_accuracy = np.mean((predictions > 0) == (y > 0))
# Early stopping check
if train_accuracy > best_accuracy:
best_accuracy = train_accuracy
best_params = {k: v.copy() for k, v in network.params.items()}
patience_counter = 0
else:
patience_counter += 1
# Print progress every epoch
print(f"Epoch {epoch}: Train Acc = {train_accuracy:.4f}, Loss = {epoch_loss/n_batches:.4f}, LR = {lr:.6f}")
# Early stopping
if patience_counter >= patience:
print(f"Early stopping at epoch {epoch}")
break
except Exception as e:
print(f"Error in epoch {epoch}: {str(e)}")
raise e
# Restore best parameters
if best_params is not None:
network.params = best_params
print(f"\nRestored best parameters with accuracy: {best_accuracy:.4f}")
return best_accuracy
def plot_decision_boundary(network, X, y, save_path):
"""Plot decision boundary with multiple views."""
fig, axes = plt.subplots(2, 2, figsize=(15, 15))
# Cartesian View
x_min, x_max = X[:, 0].min() - 0.1, X[:, 0].max() + 0.1
y_min, y_max = X[:, 1].min() - 0.1, X[:, 1].max() + 0.1
xx, yy = np.meshgrid(np.linspace(x_min, x_max, 100),
np.linspace(y_min, y_max, 100))
# Create all features for prediction
r = np.sqrt(xx**2 + yy**2)
theta = np.arctan2(yy, xx)
theta = np.unwrap(theta)
dr_dtheta = r / theta
# Normalize features
x_norm = xx.ravel() / np.max(np.abs(X[:, 0]))
y_norm = yy.ravel() / np.max(np.abs(X[:, 1]))
r_norm = r.ravel() / np.max(X[:, 2] * np.max(np.abs(X[:, 0])))
theta_norm = theta.ravel() / (6 * np.pi)
dr_norm = dr_dtheta.ravel() / np.max(np.abs(X[:, 4]))
# Make predictions
X_mesh = jnp.array(np.column_stack([
x_norm, y_norm, r_norm, theta_norm, dr_norm
]), dtype=jnp.float32)
Z = network.predict(X_mesh)
Z = Z.reshape(xx.shape)
# Plot Cartesian view
axes[0,0].contourf(xx, yy, Z, alpha=0.4, cmap='RdYlBu')
axes[0,0].scatter(X[:, 0] * np.max(np.abs(X[:, 0])),
X[:, 1] * np.max(np.abs(X[:, 1])),
c=['red' if label == 1 else 'blue' for label in y],
alpha=0.6)
axes[0,0].set_title('Cartesian View')
axes[0,0].grid(True)
# Plot Polar view (θ vs r)
axes[0,1].scatter(X[:, 3] * 6 * np.pi, # Denormalize theta
X[:, 2] * np.max(np.abs(X[:, 0])), # Denormalize radius
c=['red' if label == 1 else 'blue' for label in y],
alpha=0.6)
axes[0,1].set_title('Polar View (θ vs r)')
axes[0,1].grid(True)
# Plot dr/dθ vs θ
axes[1,0].scatter(X[:, 3] * 6 * np.pi, # theta
X[:, 4] * np.max(np.abs(X[:, 4])), # dr/dtheta
c=['red' if label == 1 else 'blue' for label in y],
alpha=0.6)
axes[1,0].set_title('Spiral Tightness (dr/dθ vs θ)')
axes[1,0].grid(True)
# Plot r vs dr/dθ
axes[1,1].scatter(X[:, 4] * np.max(np.abs(X[:, 4])), # dr/dtheta
X[:, 2] * np.max(np.abs(X[:, 0])), # radius
c=['red' if label == 1 else 'blue' for label in y],
alpha=0.6)
axes[1,1].set_title('Growth Rate (r vs dr/dθ)')
axes[1,1].grid(True)
plt.tight_layout()
plt.savefig(save_path, dpi=300, bbox_inches='tight')
plt.close()
def main():
"""Main training loop."""
print("\nTraining on Spiral dataset...")
# Generate spiral dataset
X, y = generate_spiral_dataset(n_points=1000, noise=0.1)
# Split data
X_train, X_val, y_train, y_val = train_test_split(
X, y, test_size=0.2, random_state=42
)
# Initialize BackpropNEAT with smaller network
n_features = X.shape[1]
neat = BackpropNEAT(
n_inputs=n_features,
n_outputs=1,
n_hidden=32, # Reduced hidden layer size
population_size=5,
learning_rate=0.01,
beta=0.9
)
# Training parameters
n_epochs = 300
batch_size = 32
patience = 30 # Reduced patience
# Train each network in the population
best_network = None
best_val_acc = 0.0
for i, network in enumerate(neat.population):
print(f"\nTraining network {i+1}/{len(neat.population)}...")
# Train network
train_accuracy = train_network(
network,
X_train,
y_train,
n_epochs=n_epochs,
batch_size=batch_size,
patience=patience
)
# Evaluate on validation set
val_preds = network.predict(X_val)
val_accuracy = np.mean((val_preds > 0) == (y_val > 0))
print(f"Network {i+1} - Train Acc: {train_accuracy:.4f}, Val Acc: {val_accuracy:.4f}")
# Update best network
if val_accuracy > best_val_acc:
best_val_acc = val_accuracy
best_network = network
# Plot decision boundary for best network
if best_network is not None:
plot_path = "spiral_decision_boundary.png"
plot_decision_boundary(best_network, X, y, plot_path)
print(f"\nDecision boundary plot saved to {plot_path}")
if __name__ == "__main__":
main() |