本文整理汇总了Python中optimizer.Optimizer类的典型用法代码示例。如果您正苦于以下问题:Python Optimizer类的具体用法?Python Optimizer怎么用?Python Optimizer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Optimizer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, mod, value, seed, dist, directory, full):
Optimizer.__init__(self, value)
self.rand = random.Random(seed)
self.model = mod
self.dist = dist
self.directory = directory
self.full = full
示例2: test_emit_operation
def test_emit_operation(self):
opt = Optimizer()
opt.add_operation(Types.VOID, Operations.FINISH, [])
ops = opt.build_operations()
assert len(ops) == 1
assert ops[0].op == Operations.FINISH
assert ops[0].getarglist() == []
示例3: end_of_iteration
def end_of_iteration(self):
""" Do something at the end of each generation.
In general, this a very customizable routine. This is where
hill-climbing heuristics can be placed. Additionally, tracking
of objectives as a function of generation is easily done. For this
default implementation, the best keff and maxpeak are kept for
each generation.
"""
best = self.GetBestIndex(PGA.OLDPOP)
bestpattern = self.GetIntegerChromosome(best, PGA.OLDPOP)
it = self.GetGAIterValue()
# Note, we need to reshuffle and reevaluate for the best pattern.
# This is *probably* more efficient than keeping k and p for
# all evaluations and then trying to find the values corresponding
# to the bestpattern.
self.reactor.shuffle(bestpattern)
self.k, self.p = self.reactor.evaluate()
# Tell mother what we've done.
Optimizer.fire_signal(self)
#print "iter = ",iter
#print " it = ", it, " best = ", self.GetEvaluation(best, PGA.NEWPOP), " k p = ",k,p,bestpattern
#print " *** ", self.GetEvaluation(best, PGA.OLDPOP)
if self.track_best :
self.best_eval[it-1] = self.GetEvaluation(best, PGA.OLDPOP)
self.best_k[it-1] = self.k
self.best_p[it-1] = self.p
self.best_pattern[it-1,:] = bestpattern
del bestpattern
示例4: cnn_2d_mnist
def cnn_2d_mnist():
d = image.mnist()
d.shuffle()
def layer_gen():
l1 = ConvLayer2d(layer_id=0, image_size=d.data_shape,
activation=calcutil.relu, c_in=1, c_out=16, k=(2, 2),
s=(1, 1), is_dropout=True)
l2 = MaxPoolLayer2d(layer_id=1, image_size=l1.output_size,
activation=calcutil.identity, c_in=16, k=(2, 2))
l3 = ConvLayer2d(layer_id=2, image_size=l2.output_size,
activation=calcutil.relu, c_in=16, c_out=32, k=(2, 2),
s=(1, 1), is_dropout=True)
l4 = MaxPoolLayer2d(layer_id=3, image_size=l3.output_size,
activation=calcutil.identity, c_in=32, k=(2, 2))
l5 = HiddenLayer(layer_id=4, n_in=l4.n_out, n_out=800,
activation=calcutil.relu)
l6 = HiddenLayer(layer_id=5, n_in=l5.n_out, n_out=100,
activation=calcutil.relu)
l7 = SoftMaxLayer(layer_id=6, n_in=l6.n_out, n_out=len(d.classes()))
layers = [l1, l2, l3, l4, l5, l6, l7]
return layers
m = Model(input_dtype='float32', layers_gen_func=layer_gen)
optimizer = Optimizer(d, m)
optimizer.optimize(100, 1000)
示例5: main
def main(args, profile=False):
db = DataBase(args.games)
results = db.load_game_results(mingames=50)
print('{} games loaded.'.format(len(results)))
players = db.load_players()
optimizer = Optimizer(disp=True)
optimizer.load_games(results)
maxiter = 30 if profile else 0
ratings, f, v = optimizer.run(method='l-bfgs-b', maxiter=maxiter)
if profile:
return
print()
by_rating = []
for iplayer, rating in ratings.items():
print(players[iplayer])
mr = 0
for date, r in sorted(rating.items()):
date = datetime.date.fromtimestamp(date * 24 * 3600)
print(date.isoformat(), r)
if r > mr:
mr = r
by_rating.append((mr, players[iplayer]))
print()
print(f)
print(f.calc(0.2) - f.calc(-0.2))
best = list(sorted(by_rating))[-20:]
for r, p in reversed(best):
print('{:24} {}'.format(p, r))
示例6: __init__
def __init__(self, argv, reactor, objective):
""" Constructor.
"""
# Initialize the Optimizer object.
Optimizer.__init__(self, argv, reactor, objective)
# Initialize the PGA object.
PGA.__init__(self, argv, PGA.DATATYPE_INTEGER, self.reactor.number_bundles(), PGA.MAXIMIZE)
# Set default operators.
self.SetCrossover(self.htbx) # Crossover
self.SetEndOfGen(self.end_of_iteration) # End of generation info
self.SetInitString(self.init) # String initialization
self.SetMutation(self.swap) # Mutation via a single swap
# Set default values.
self.maximum_generations = 100 # Maximum generations
self.population_size = 50 # Population size
self.number_replaced = 40 # Number replaced each generation
self.seed = 123 # PGA random number seed
self.np_seed = 123 # NumPy random number seed
self.binary_sweep = False # Perform one sweep of binary exchanges
# Optimizer-specific flags.
self.track_best = False
self.fixed_central = True
# Counter for evaluations on each process.
self.evals = 0
示例7: test_symmetric_wins
def test_symmetric_wins(self):
o = Optimizer(rand_seed=239)
o.load_games([(1, 2, 1, 1), (1, 2, 1, 0), (2, 1, 1, 1), (2, 1, 1, 0)])
rating, f, v = o.run(method='Newton-CG')
self.assertAlmostEqual(
f.calc(convert_rating_diff(rating[1][1] - rating[2][1])), 0.5, 2)
self.assertLess(abs(rating[1][1] - rating[2][1]), 5)
示例8: test_simple_new
def test_simple_new(self, cpu):
opt = Optimizer([Virtualize])
struct_descr = cpu.new_struct()
opt.add_operation(Types.REF, Operations.NEW, [], descr=struct_descr)
ops = opt.build_operations()
assert len(ops) == 0
示例9: test_draw
def test_draw(self):
o = Optimizer(rand_seed=239)
o.load_games([(1, 2, 1, 2), (2, 1, 1, 2)])
rating, f, v = o.run(method='cg')
self.assertAlmostEqual(f.calc(0), 0.5, 3)
self.assertAlmostEqual(
f.calc(convert_rating_diff(rating[1][1] - rating[2][1])), 0.5, 2)
self.assertLess(abs(rating[1][1] - rating[2][1]), 5)
示例10: test_objective_time_reg
def test_objective_time_reg(self):
o = Optimizer(rand_seed=239)
o.load_games([(1, 2, 1, 1), (1, 2, 2, 0)])
v = o.create_vars({1: {1: 2200, 2: 1800}, 2: {1: 1800, 2: 2200}},
(0, -1.01))
(total, likelihood, regularization, _, _, _) = o.objective(
v, verbose=True)
self.assertLess(likelihood, 0)
self.assertTrue(1E-6 < regularization < 1)
示例11: test_inputs
def test_inputs(self):
opt = Optimizer()
res = opt.add_input(Types.INT)
opt.add_operation(Types.VOID, Operations.FINISH, [res])
ops = opt.build_operations()
assert len(ops) == 1
assert ops[0].op == Operations.FINISH
assert ops[0].getarglist() == [res]
示例12: test_output_format
def test_output_format(self):
o = Optimizer()
o.load_games(GAMES1)
ratings, f = o.random_solution()
self.assertEqual(len(ratings), 3)
self.assertEqual(list(sorted(ratings.keys())), [1, 2, 3])
for player_rating in ratings.values():
for r in player_rating.values():
self.assertTrue(1000 < r < 3000)
self.assertEqual(list(sorted(ratings[1].keys())), [1, 2])
self.assertEqual(list(sorted(ratings[2].keys())), [1, 3, 4])
self.assertEqual(list(sorted(ratings[3].keys())), [1, 2, 3, 4])
示例13: test_lt
def test_lt(self):
opt = Optimizer([IntBounds, GuardPropagation])
i0 = opt.add_input(Types.INT)
i1 = opt.add_operation(Types.INT, Operations.INT_LT,
[i0, opt.new_constant_int(10)],
)
opt.add_operation(Types.VOID, Operations.GUARD_TRUE, [i1])
opt.add_operation(Types.INT, Operations.INT_LT, [i0, opt.new_constant_int(15)])
ops = opt.build_operations()
assert len(ops) == 2
示例14: test_read_unsetfield
def test_read_unsetfield(self, cpu):
opt = Optimizer([Virtualize])
struct_descr = cpu.new_struct()
field_descr = cpu.new_field(struct_descr)
p0 = opt.add_operation(Types.REF, Operations.NEW, [], descr=struct_descr)
i0 = opt.add_operation(Types.INT, Operations.GETFIELD, [p0], descr=field_descr)
ops = opt.build_operations()
assert len(ops) == 0
assert opt.getvalue(i0).getint() == 0
示例15: test_objective_single_game
def test_objective_single_game(self):
o = Optimizer()
o.load_games([(1, 2, 1, 1)])
v1 = o.create_vars({1: {1: 2200}, 2: {1: 1800}}, [0, -1.01])
v2 = o.create_vars({1: {1: 2200}, 2: {1: 2200}}, [0, -1.01])
v3 = o.create_vars({1: {1: 1800}, 2: {1: 2200}}, [0, -1.01])
(total1, likelihood1, regularization1, smoothness1,
func_hard_reg, _) = o.objective(v1, verbose=True)
self.assertLess(likelihood1, 0)
self.assertTrue(1E-6 < regularization1 < 1)
self.assertEqual(smoothness1, 0)
self.assertTrue(func_hard_reg < 1)
(total2, likelihood2, regularization2, _, _, _) = o.objective(
v2, verbose=True)
self.assertLess(likelihood2, likelihood1)
(total3, likelihood3, regularization3, _, _, _) = o.objective(
v3, verbose=True)
self.assertAlmostEqual(regularization1, regularization3)
self.assertLess(total1 / total2, 0.9)
self.assertLess(total2 / total3, 0.9)