File size: 5,031 Bytes
2facf1f | 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 | import jax
import jax.numpy as jnp
import optax
import numpy as np
from dataclasses import dataclass
@dataclass
class Hyperparameters:
num_intervals: int = 1000
learning_rate: float = 0.01
end_lr_factor: float = 1e-5
num_steps: int = 80000
warmup_steps: int = 3000
smooth_max_temp: float = 100.0 # temperature for log-sum-exp smooth max
class AutocorrelationOptimizer:
def __init__(self, hypers: Hyperparameters):
self.hypers = hypers
self.domain_width = 0.5
self.dx = self.domain_width / self.hypers.num_intervals
def _objective_fn(self, f_values: jnp.ndarray, temp: float) -> jnp.ndarray:
f_non_negative = jax.nn.softplus(f_values) # smooth non-negativity
integral_f = jnp.sum(f_non_negative) * self.dx
eps = 1e-12
integral_f_safe = jnp.maximum(integral_f, eps)
N = self.hypers.num_intervals
padded_f = jnp.pad(f_non_negative, (0, N))
fft_f = jnp.fft.rfft(padded_f)
fft_conv = fft_f * fft_f
conv_f_f = jnp.fft.irfft(fft_conv, n=2 * N)
scaled_conv_f_f = conv_f_f * self.dx
# Use log-sum-exp smooth max for better gradients
smooth_max = jax.nn.logsumexp(temp * scaled_conv_f_f) / temp
c1_ratio = smooth_max / (integral_f_safe ** 2)
return c1_ratio
def _hard_objective(self, f_values: jnp.ndarray) -> jnp.ndarray:
f_non_negative = jax.nn.softplus(f_values)
integral_f = jnp.sum(f_non_negative) * self.dx
eps = 1e-12
integral_f_safe = jnp.maximum(integral_f, eps)
N = self.hypers.num_intervals
padded_f = jnp.pad(f_non_negative, (0, N))
fft_f = jnp.fft.rfft(padded_f)
fft_conv = fft_f * fft_f
conv_f_f = jnp.fft.irfft(fft_conv, n=2 * N)
scaled_conv_f_f = conv_f_f * self.dx
max_conv = jnp.max(scaled_conv_f_f)
c1_ratio = max_conv / (integral_f_safe ** 2)
return c1_ratio
def train_step(self, f_values, opt_state, temp):
loss, grads = jax.value_and_grad(self._objective_fn)(f_values, temp)
updates, opt_state = self.optimizer.update(grads, opt_state, f_values)
f_values = optax.apply_updates(f_values, updates)
hard_loss = self._hard_objective(f_values)
return f_values, opt_state, loss, hard_loss
def run_optimization(self, seed=42):
schedule = optax.warmup_cosine_decay_schedule(
init_value=0.0,
peak_value=self.hypers.learning_rate,
warmup_steps=self.hypers.warmup_steps,
decay_steps=self.hypers.num_steps - self.hypers.warmup_steps,
end_value=self.hypers.learning_rate * self.hypers.end_lr_factor,
)
self.optimizer = optax.adam(learning_rate=schedule)
key = jax.random.PRNGKey(seed)
N = self.hypers.num_intervals
# Better initialization: triangle-like shape centered
x = jnp.linspace(0, 1, N)
# Start with a bump function shape
f_values = jnp.exp(-20.0 * (x - 0.5) ** 2)
f_values = f_values + 0.05 * jax.random.uniform(key, (N,))
opt_state = self.optimizer.init(f_values)
train_step_jit = jax.jit(self.train_step)
best_loss = jnp.inf
best_f = f_values
for step in range(self.hypers.num_steps):
# Anneal temperature: start low, increase
progress = min(step / (self.hypers.num_steps * 0.5), 1.0)
temp = 10.0 + progress * (self.hypers.smooth_max_temp - 10.0)
f_values, opt_state, loss, hard_loss = train_step_jit(f_values, opt_state, temp)
if hard_loss < best_loss:
best_loss = hard_loss
best_f = f_values
if step % 5000 == 0 or step == self.hypers.num_steps - 1:
print(f"Step {step:5d} | C1(smooth) ≈ {loss:.8f} | C1(hard) ≈ {hard_loss:.8f} | temp={temp:.1f}")
print(f"Best C1 found: {best_loss:.8f}")
# Convert softplus to actual non-negative values
final_f = jax.nn.softplus(best_f)
final_c1 = float(best_loss)
return final_f, final_c1
def run():
best_c1 = float('inf')
best_result = None
# Try multiple seeds and configs
configs = [
(1000, 0.01, 80000, 42),
(1000, 0.005, 80000, 123),
(1500, 0.008, 60000, 42),
]
for n_intervals, lr, steps, seed in configs:
print(f"\n--- Config: N={n_intervals}, lr={lr}, steps={steps}, seed={seed} ---")
hypers = Hyperparameters(
num_intervals=n_intervals,
learning_rate=lr,
num_steps=steps,
)
optimizer = AutocorrelationOptimizer(hypers)
optimized_f, final_c1 = optimizer.run_optimization(seed=seed)
if final_c1 < best_c1:
best_c1 = final_c1
f_values_np = np.array(optimized_f)
best_result = (f_values_np, best_c1, best_c1, hypers.num_intervals)
print(f"*** New best: C1 = {best_c1:.10f}")
return best_result
|