本文整理汇总了Python中optimizer.Optimizer.evolve方法的典型用法代码示例。如果您正苦于以下问题:Python Optimizer.evolve方法的具体用法?Python Optimizer.evolve怎么用?Python Optimizer.evolve使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类optimizer.Optimizer
的用法示例。
在下文中一共展示了Optimizer.evolve方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: generate
# 需要导入模块: from optimizer import Optimizer [as 别名]
# 或者: from optimizer.Optimizer import evolve [as 别名]
def generate(generations, population, nn_param_choices, dataset):
"""Generate a network with the genetic algorithm.
Args:
generations (int): Number of times to evole the population
population (int): Number of networks in each generation
nn_param_choices (dict): Parameter choices for networks
dataset (str): Dataset to use for training/evaluating
"""
optimizer = Optimizer(nn_param_choices)
networks = optimizer.create_population(population)
# Evolve the generation.
for i in range(generations):
logging.info("")
logging.info("")
logging.info("***Doing generation %d of %d***" % (i + 1, generations))
print("\n\n\n**************************************")
print("***Generation %d/%d" % (i + 1, generations))
print("**************************************\n\n")
# Train and get accuracy for networks.
train_networks(networks, dataset)
# Get the average accuracy for this generation.
average_accuracy, average_loss = get_averages(networks)
# Print out the average accuracy each generation.
logging.info("Generation average: %.2f%% (%.4f)" % (average_accuracy * 100, average_loss ))
logging.info('-'*80)
# Evolve, except on the last iteration.
if i != generations - 1:
# Do the evolution.
networks = optimizer.evolve(networks)
copy_accuracies(networks)
# Sort our final population.
networks = sorted(networks, key=lambda x: x.accuracy, reverse=True)
# Print out the top 5 networks.
print_networks(networks[:5])