Caffe2 - Python API
A deep learning, cross platform ML framework
test.py
1 import json
2 import torch
3 import torch.legacy.optim as optim
4 from pprint import pprint
5 
6 
7 def rosenbrock(tensor):
8  x, y = tensor
9  return (1 - x) ** 2 + 100 * (y - x ** 2) ** 2
10 
11 
12 def drosenbrock(tensor):
13  x, y = tensor
14  return torch.DoubleTensor((-400 * x * (y - x ** 2) - 2 * (1 - x), 200 * (y - x ** 2)))
15 
16 algorithms = {
17  'adadelta': optim.adadelta,
18  'adagrad': optim.adagrad,
19  'adam': optim.adam,
20  'adamax': optim.adamax,
21  'asgd': optim.asgd,
22  'cg': optim.cg,
23  'nag': optim.nag,
24  'rmsprop': optim.rmsprop,
25  'rprop': optim.rprop,
26  'sgd': optim.sgd,
27  'lbfgs': optim.lbfgs,
28 }
29 
30 with open('tests.json', 'r') as f:
31  tests = json.loads(f.read())
32 
33 for test in tests:
34  print(test['algorithm'] + '\t')
35  algorithm = algorithms[test['algorithm']]
36  for config in test['config']:
37  print('================================================================================\t')
38  params = torch.DoubleTensor((1.5, 1.5))
39  for i in range(100):
40  algorithm(lambda x: (rosenbrock(x), drosenbrock(x)), params, config)
41  print('{:.8f}\t{:.8f}\t'.format(params[0], params[1]))