forked from TonyTangYu/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
41 lines (34 loc) · 1.12 KB
/
test.py
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
import json
import torch
import torch.legacy.optim as optim
def rosenbrock(tensor):
x, y = tensor
return (1 - x) ** 2 + 100 * (y - x ** 2) ** 2
def drosenbrock(tensor):
x, y = tensor
return torch.DoubleTensor((-400 * x * (y - x ** 2) - 2 * (1 - x), 200 * (y - x ** 2)))
algorithms = {
'adadelta': optim.adadelta,
'adagrad': optim.adagrad,
'adam': optim.adam,
'adamw': optim.adamw,
'adamax': optim.adamax,
'asgd': optim.asgd,
'cg': optim.cg,
'nag': optim.nag,
'rmsprop': optim.rmsprop,
'rprop': optim.rprop,
'sgd': optim.sgd,
'lbfgs': optim.lbfgs,
}
with open('tests.json', 'r') as f:
tests = json.loads(f.read())
for test in tests:
print(test['algorithm'] + '\t')
algorithm = algorithms[test['algorithm']]
for config in test['config']:
print('================================================================================\t')
params = torch.DoubleTensor((1.5, 1.5))
for i in range(100):
algorithm(lambda x: (rosenbrock(x), drosenbrock(x)), params, config)
print('{:.8f}\t{:.8f}\t'.format(params[0], params[1]))