Order-Adaptive Integral Optimizer / run_experiment.py

Failed on benchmark

Raw ⬇ ZIP
 1import json
 2import numpy as np
 3import torch
 4from order_adaptive import verify, OrderAdaptiveIntegral
 5
 6
 7def torch_smoke(seed=11, steps=160):
 8    torch.manual_seed(seed)
 9    x = torch.randn(96, 2)
10    y = (2.0 * x[:, :1] - 0.2 * x[:, 1:]).tanh()
11
12    def train(adaptive):
13        torch.manual_seed(seed)
14        model = torch.nn.Sequential(
15            torch.nn.Linear(2, 8), torch.nn.Tanh(), torch.nn.Linear(8, 1)
16        )
17        if adaptive:
18            opt = OrderAdaptiveIntegral(
19                model.parameters(), lr=0.04, max_order=2, beta=0.95,
20                rho=0.98, decision_interval=10, patience=2,
21                ramp_steps=20, gains=(0.05, 0.0005)
22            )
23        else:
24            opt = torch.optim.SGD(model.parameters(), lr=0.04)
25        losses = []
26        for _ in range(steps):
27            opt.zero_grad()
28            loss = ((model(x) - y) ** 2).mean()
29            loss.backward()
30            opt.step()
31            losses.append(float(loss))
32        return {
33            'final_loss': losses[-1], 'loss_40': losses[39],
34            'loss_100': losses[99],
35            'activations': getattr(opt, 'activations', [])
36        }
37
38    return {'sgd': train(False), 'adaptive': train(True)}
39
40
41if __name__ == '__main__':
42    out = {'quadratic_verification': verify(), 'torch_smoke': torch_smoke()}
43    with open('results.json', 'w') as f:
44        json.dump(out, f, indent=2)
45    print(json.dumps(out, indent=2))