def simulatorOrdersPerf(plant, orderList, testNum): machines = plant.machines[:] orders = orderList.orders[:] pprint( "PERF Starting simulator benchmark test " + str(testNum) + " on orders", BLUE) orderList.orders = [] plant.machines = machines[:] times = [0] for i in range(1, len(orders) + 1): pprint("PERF Number of orders = " + str(i), BLUE) orderList.orders = orders[:i] optimizer = Optimizer(plant, orderList, Simulator(plant), Evaluator(plant)) schedule = optimizer.initialIndividual() t = time() simulator = Simulator(plant) simulator.simulate(schedule) t = time() - t times.append(t) pprint("PERF Time = " + str(t), GREEN) try: from thirdparty.CairoPlot import dot_line_plot except: pprint("PERF Will not output to graph.", RED) return dot_line_plot( path.join("benchmarks", "simulator-orders-" + str(testNum)) + FORMAT, times, 800, 800, (255, 255, 255), 5, True, True, True, None, None, None, None)
def setup_network(self): config = BertConfig.from_json_file(cfg.CONFIG_FILE) if cfg.TRAIN.FROM_PRETRAINED: model = BaseBertPreTraining.from_pretrained( cfg.TRAIN.FROM_PRETRAINED, config) else: model = BaseBertPreTraining(config) model.to(self.device) if args.local_rank != -1: self.model = torch.nn.parallel.DistributedDataParallel( model, find_unused_parameters=True, device_ids=[self.args.local_rank], output_device=self.args.local_rank, broadcast_buffers=False) elif self.n_gpu > 1: self.model = torch.nn.DataParallel(model) else: self.model = model epoch_steps = len(self.train_dataset_loader) n_steps = epoch_steps * cfg.SOLVER.NUM_TRAIN_EPOCHS self.optim = Optimizer(self.model, epoch_steps=epoch_steps, n_steps=n_steps)
def bench(self): recipes = [] for o in self.orderList.orders: recipes.append(o.recipe.recipe[:]) o.recipe.recipe = [] machines = self.plant.machines[:] self.plant.machines = [] i = self.startValue while i <= len(machines): pprint("PERF Number of machines = " + str(i), BLUE) self.plant.machines = machines[:i] for j, o in enumerate(self.orderList.orders): o.recipe.recipe = recipes[j][:i] optimizer = Optimizer(self.plant, self.orderList, Simulator(self.plant), Evaluator(self.plant)) optimizer.populationSize = 10 optimizer.iterations = 10 optimizer.indivMutationRate = 0.5 optimizer.selectionRate = 0.5 optimizer.mutationRange = 10 t = time() optimizer.run() t = time() - t t -= optimizer.simulatorTime self.addCairoPlotTime(t) self.addGnuPlotTime(i, t) i += 1
def setup_network(self): model = models.create(cfg.MODEL.TYPE) self.trainer = model.cuda() self.checkpointer = CaptionCheckpointer( self.trainer, os.path.join(cfg.ROOT_DIR, "snapshot")) if self.args.resume > 0: self.checkpointer.load( self.snapshot_path("caption_model", self.args.resume)) self.predictor = models.create(cfg.MODEL.TYPE).cuda() self.predictor.load_state_dict(self.trainer.state_dict()) self.optim = Optimizer(self.trainer)
def mix(self): if len(self.loaded_songs) == 0: self.message.set("No loaded songs...") return first_goal = MixGoal(0.0, 0.0, 0.0, 0.0, 1) goals = [first_goal] self.analyze_songs() print("Chosen Style: {}".format(self.style)) print("Maximum Length: {}".format(self.max_length)) print("First Song: {}".format(self.first_song)) dj = Optimizer(self.loaded_songs, goals, Style_Lib[self.style].value, self.max_length, self.first_song) mix = dj.generate_mixtape() self.log_message(mix) comp = composer.composer_parser(mix) comp.compose()
def main_loop(self): #optimizer = T4l_optimizer(self.optimizer_config) optimizer = Optimizer(self.optimizer_config) broker = Broker(self.broker_config, optimizer) self.print_info() broker.schedule_tasks(self.tasks)
def main_loop(self): optimizer = Optimizer(self.optimizer_config) broker = LocalBroker(self.broker_config, optimizer) self.print_info() broker.schedule_tasks(self.tasks) if broker.sys_info is not None: broker.sys_info.stop_server.set()
def bench(self): orders = self.orderList.orders[:] self.orderList.orders = [] i = self.startValue while i <= len(orders): pprint("PERF Number of orders = " + str(i), BLUE) self.orderList.orders = orders[:i] optimizer = Optimizer(self.plant, self.orderList, Simulator(self.plant), Evaluator(self.plant)) schedule = optimizer.initialIndividual() t = time() simulator = Simulator(self.plant) simulator.simulate(schedule) t = time() - t self.addCairoPlotTime(t) self.addGnuPlotTime(i, t) i += 1
def test_warm_start(self): params = Parameter() params.set("wheel_base", 2.7) params.set("dt", 0.2) params.set("function_tolerance", 1e-6) params.set("max_num_iterations", 1000) initial_state = np.array([[0., 0., 0., 10.], [2., 0., 0., 10.], [4., 0., 0., 10.], [6., 0., 0., 10.]]) opt_vec = np.array( [[0., 0.], [-0.00581466, -0.02969236], [-0.0100573, -0.05063586], [-0.01290205, -0.06336555], [-0.0145111, -0.06958468], [-0.01504364, -0.07147759], [-0.01466189, -0.0712203], [-0.01353391, -0.07066254], [-0.01183398, -0.0711591], [-0.00974178, -0.0735261], [-0.00744058, -0.07806701], [-0.00509113, -0.08413195], [-0.0028139, -0.09089476], [-0.00069383, -0.0976187], [0.00121458, -0.10373539], [0.00288213, -0.10887319], [0.00430011, -0.11284776], [0.00547573, -0.11562908], [0.00642782, -0.11729807], [0.00718276, -0.11800159], [0.0077706, -0.11791354], [0.00821974, -0.11720583], [0.00855625, -0.11603267], [0.0088038, -0.11452536], [0.0089836, -0.11278997], [0.00911419, -0.11090794], [0.00921137, -0.10893774], [0.00928801, -0.10691821], [0.00935393, -0.10487191], [0., 0.00059502]]) ref_line = np.array([[0., 4.], [1000., 4.]]) obstacle_outline0 = np.array([[14., 1.7], [22., 1.7], [22., 4.7], [14., 4.7], [14., 1.7]]) # optimizer opt = Optimizer(params) opt.SetOptimizationVector(opt_vec) # costs ref_cost = ReferenceLineCost(params, 100.) ref_cost.SetReferenceLine(ref_line) jerk_cost = JerkCost(params, 10000.) outline = ObjectOutline() obstacle_outline1 = obstacle_outline0 + np.array([[30.0, -2.]]) outline.Add(obstacle_outline0, 0.) outline.Add(obstacle_outline1, 6.) object_cost = StaticObjectCost(params, 2.5, 10000.) object_cost.AddObjectOutline(outline) input_cost = InputCost(params, 10.) input_cost.SetLowerBound(np.array([[-0.2, -1.0]])) input_cost.SetUpperBound(np.array([[0.2, 1.0]])) speed_cost = SpeedCost(params, 10.) speed_cost.SetDesiredSpeed(10.) # optimization problem functor = opt.AddFastSingleTrackFunctor( initial_state, params, [jerk_cost, ref_cost, input_cost, object_cost, speed_cost]) opt.FixOptimizationVector(0, 1) opt.Solve() opt.Report()
def optimize(self): simulator = Simulator(self.plant) evaluator = Evaluator.fromXmlFile(self.configFilename, self.plant) optimizer = Optimizer.fromXmlFile(self.configFilename, self.plant, self.orderList, simulator, evaluator) result = optimizer.run() best = bestSolution(result) best.unNormalize(self.normValue) print best
def bench(self): val = 4 i = self.startValue while i <= 6: pprint("PERF Large Value = " + str(i * val), BLUE) for o in self.orderList.orders: o.deadline *= val for r in o.recipe.recipe: r[1] *= val optimizer = Optimizer(self.plant, self.orderList, Simulator(self.plant), Evaluator(self.plant)) schedule = optimizer.initialIndividual() t = time() simulator = Simulator(self.plant) simulator.simulate(schedule) t = time() - t self.addCairoPlotTime(t) self.addGnuPlotTime((i + 1) * val, t) i += 1
def optimizerMachinesPerf(plant, orderList, testNum): machines = plant.machines[:] orders = orderList.orders[:] pprint( "PERF Starting optimizer benchmark test " + str(testNum) + " on machines", BLUE) orderList.orders = orders[:] recipes = [] for o in orderList.orders: recipes.append(o.recipe.recipe[:]) o.recipe.recipe = [] plant.machines = [] times = [0] for i in range(1, len(machines) + 1): pprint("PERF Number of machines = " + str(i), BLUE) plant.machines = machines[:i] for j, o in enumerate(orderList.orders): o.recipe.recipe = recipes[j][:i] optimizer = Optimizer(plant, orderList, Simulator(plant), Evaluator(plant)) optimizer.populationSize = 10 optimizer.iterations = 10 optimizer.indivMutationRate = 0.5 optimizer.selectionRate = 0.5 optimizer.mutationRange = 10 t = time() optimizer.run() t = time() - t t -= optimizer.simulatorTime times.append(t) pprint("PERF Time = " + str(t), GREEN) try: from thirdparty.CairoPlot import dot_line_plot except: pprint("PERF Will not output to graph.", RED) return dot_line_plot( path.join("benchmarks", "optimizer-machines-" + str(testNum)) + FORMAT, times, 800, 800, (255, 255, 255), 5, True, True, True, None, None, None, None)
def bench(self): i = 10 while i <= 200: pprint("PERF Mutation Range = " + str(i), BLUE) optimizer = Optimizer(self.plant, self.orderList, Simulator(self.plant), Evaluator(self.plant)) optimizer.populationSize = 10 optimizer.iterations = 10 optimizer.indivMutationRate = 0.5 optimizer.selectionRate = 0.5 optimizer.mutationRange = i t = time() optimizer.run() t = time() - t t -= optimizer.simulatorTime self.addCairoPlotTime(t) self.addGnuPlotTime(i, t) i += 20
def setup_network(self): model = models.create(cfg.MODEL.TYPE) if self.distributed: # this should be removed if we update BatchNorm stats self.model = torch.nn.parallel.DistributedDataParallel( model.to(self.device), device_ids=[self.args.local_rank], output_device=self.args.local_rank, broadcast_buffers=False) else: self.model = torch.nn.DataParallel(model).cuda() if self.args.resume > 0: self.model.load_state_dict( torch.load(self.snapshot_path("caption_model", self.args.resume), map_location=lambda storage, loc: storage)) self.optim = Optimizer(self.model) self.xe_criterion = losses.create(cfg.LOSSES.XE_TYPE).cuda() self.rl_criterion = losses.create(cfg.LOSSES.RL_TYPE).cuda()
def simulatorLargeValuesPerf(plant, orderList, testNum): machines = plant.machines[:] orders = orderList.orders[:] pprint( "PERF Starting simulator benchmark test " + str(testNum) + " with large values", BLUE) orderList.orders = orders[:] plant.machines = machines[:] times = [0] val = 4 for i in range(1, 7): pprint("PERF Large Value = " + str(i * val), BLUE) for o in orderList.orders: o.deadline *= val for r in o.recipe.recipe: r[1] *= val optimizer = Optimizer(plant, orderList, Simulator(plant), Evaluator(plant)) schedule = optimizer.initialIndividual() t = time() simulator = Simulator(plant) simulator.simulate(schedule) t = time() - t times.append(t) pprint("PERF Time = " + str(t), GREEN) try: from thirdparty.CairoPlot import dot_line_plot except: pprint("PERF Will not output to graph.", RED) return dot_line_plot( path.join("benchmarks", "simulator-largevalues-" + str(testNum)) + FORMAT, times, 800, 800, (255, 255, 255), 5, True, True, True, None, None, None, None)
def optimize(self): simulator = Simulator(self.plant) evaluator = Evaluator.fromXmlFile(self.configFilename, self.plant, self.normValue) optimizer = Optimizer.fromXmlFile(self.configFilename, self.plant, self.orderList, simulator, evaluator) result = optimizer.run() best = bestSolution(result) best.unNormalize(self.normValue) # for item in best: # if "kettle" in item[1]: # print item[0], item[2] print best
def optimizerLargeValuesPerf(plant, orderList, testNum): machines = plant.machines[:] orders = orderList.orders[:] pprint( "PERF Starting optimizer benchmark test " + str(testNum) + " with large values", BLUE) orderList.orders = orders[:6] plant.machines = machines[:] times = [0] val = 2 for i in range(1, 6): pprint("PERF Large Value = " + str(i * val), BLUE) for o in orderList.orders: o.deadline *= val for r in o.recipe.recipe: r[1] *= val optimizer = Optimizer(plant, orderList, Simulator(plant), Evaluator(plant)) optimizer.populationSize = 5 optimizer.iterations = 5 optimizer.indivMutationRate = 0.5 optimizer.selectionRate = 0.5 optimizer.mutationRange = 500 t = time() optimizer.run() t = time() - t t -= optimizer.simulatorTime times.append(t) pprint("PERF Time = " + str(t), GREEN) try: from thirdparty.CairoPlot import dot_line_plot except: pprint("PERF Will not output to graph.", RED) return dot_line_plot( path.join("benchmarks", "optimizer-largevalues-" + str(testNum)) + FORMAT, times, 800, 800, (255, 255, 255), 5, True, True, True, None, None, None, None)
def bench(self): recipes = [] for o in self.orderList.orders: recipes.append(o.recipe.recipe[:]) o.recipe.recipe = [] machines = self.plant.machines[:] self.plant.machines = [] i = self.startValue while i <= len(machines): pprint("PERF Number of machines = " + str(i), BLUE) self.plant.machines = machines[:i] for j, o in enumerate(self.orderList.orders): o.recipe.recipe = recipes[j][:i] optimizer = Optimizer(self.plant, self.orderList, Simulator(self.plant), Evaluator(self.plant)) schedule = optimizer.initialIndividual() t = time() simulator = Simulator(self.plant) simulator.simulate(schedule) t = time() - t self.addCairoPlotTime(t) self.addGnuPlotTime(i, t) i += 1
def bench(self): orders = self.orderList.orders[:] self.orderList.orders = [] i = self.startValue while i <= len(orders): pprint("PERF Number of orders = " + str(i), BLUE) self.orderList.orders = orders[:i] optimizer = Optimizer(self.plant, self.orderList, Simulator(self.plant), Evaluator(self.plant)) optimizer.populationSize = 10 optimizer.iterations = 10 optimizer.indivMutationRate = 0.5 optimizer.selectionRate = 0.5 optimizer.mutationRange = 10 t = time() optimizer.run() t = time() - t t -= optimizer.simulatorTime self.addCairoPlotTime(t) self.addGnuPlotTime(i, t) i += 1
def run(self): simulator = Simulator(self.plant) evaluator = Evaluator.fromXmlFile(self.configFilename, self.plant) optimizer = Optimizer.fromXmlFile(self.configFilename, self.plant, self.orderList, simulator, evaluator) scheduler = Scheduler(self.plant, self.orderList) result = scheduler.start() if result != None: solutions = parseSolutions(result, self.plant, self.orderList) for s in solutions: s.loadStartTimes(self.plant) result = optimizer.run(solutions) best = bestSolution(result) best.unNormalize(self.normValue) print best
def bench(self): val = 2 i = self.startValue while i < 10: pprint("PERF Large Value = " + str(i * val), BLUE) for o in self.orderList.orders: o.deadline *= val for r in o.recipe.recipe: r[1] *= val optimizer = Optimizer(self.plant, self.orderList, Simulator(self.plant), Evaluator(self.plant)) optimizer.populationSize = 10 optimizer.iterations = 10 optimizer.indivMutationRate = 0.5 optimizer.selectionRate = 0.5 optimizer.mutationRange = 500 t = time() optimizer.run() t = time() - t t -= optimizer.simulatorTime self.addCairoPlotTime(t) self.addGnuPlotTime((i + 1) * val, t) i += 1
def optimizerMutationRangePerf(plant, orderList, testNum): machines = plant.machines[:] orders = orderList.orders[:] pprint( "PERF Starting optimizer benchmark test " + str(testNum) + " on mutation range", BLUE) orderList.orders = orders[:] plant.machines = machines[:] times = [0] for i in range(10, 200, 20): pprint("PERF Selection rate = " + str(i), BLUE) optimizer = Optimizer(plant, orderList, Simulator(plant), Evaluator(plant)) optimizer.populationSize = 5 optimizer.iterations = 5 optimizer.indivMutationRate = 0.5 optimizer.selectionRate = 0.5 optimizer.mutationRange = i t = time() optimizer.run() t = time() - t t -= optimizer.simulatorTime times.append(t) pprint("PERF Time = " + str(t), GREEN) try: from thirdparty.CairoPlot import dot_line_plot except: pprint("PERF Will not output to graph.", RED) return dot_line_plot( path.join("benchmarks", "optimizer-mutationrange-" + str(testNum)) + FORMAT, times, 800, 800, (255, 255, 255), 5, True, True, True, None, None, None, None)
from multiprocessing import Queue, Process, Lock, Manager from Exchange import MyExchange from optimizer.optimizer import Optimizer optimizer = Optimizer(symbol='ETH/USD', recentPerformanceTimeDuration=5) if __name__ == "__main__": optimizer.screen_for_best_parameter()
def test_single_track_optimizer(self): params = Parameter() params.set("wheel_base", 2.7) params.set("dt", 0.2) params.set("function_tolerance", 1e-8) params.set("max_num_iterations", 1000) initial_state = np.array([[0., 0., 0., 10.], [2., 0., 0., 10.], [4., 0., 0., 10.], [6., 0., 0., 10.]]) opt_vec = np.array([[ 0. , 0. ], [-0.00581466, -0.02969236], [-0.0100573 , -0.05063586], [-0.01290205, -0.06336555], [-0.0145111 , -0.06958468], [-0.01504364, -0.07147759], [-0.01466189, -0.0712203 ], [-0.01353391, -0.07066254], [-0.01183398, -0.0711591 ], [-0.00974178, -0.0735261 ], [-0.00744058, -0.07806701], [-0.00509113, -0.08413195], [-0.0028139 , -0.09089476], [-0.00069383, -0.0976187 ], [ 0.00121458, -0.10373539], [ 0.00288213, -0.10887319], [ 0.00430011, -0.11284776], [ 0.00547573, -0.11562908], [ 0.00642782, -0.11729807], [ 0.00718276, -0.11800159], [ 0.0077706 , -0.11791354], [ 0.00821974, -0.11720583], [ 0.00855625, -0.11603267], [ 0.0088038 , -0.11452536], [ 0.0089836 , -0.11278997], [ 0.00911419, -0.11090794], [ 0.00921137, -0.10893774], [ 0.00928801, -0.10691821], [ 0.00935393, -0.10487191], [ 0. , 0.00059502]]) ref_line = np.array([[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.00000000e+01], [ 2.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.00000000e+01], [ 4.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.00000000e+01], [ 6.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.00000000e+01], [ 8.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.00000000e+01], [ 9.99939997e+00, -4.30463875e-03, -4.30592393e-03, 9.99406153e+00], [ 1.19971306e+01, -2.03405218e-02, -1.17478308e-02, 9.98393436e+00], [ 1.39923704e+01, -5.32967584e-02, -2.12840277e-02, 9.97126125e+00], [ 1.59845144e+01, -1.06379431e-01, -3.19953707e-02, 9.95734431e+00], [ 1.79731414e+01, -1.81067097e-01, -4.30841450e-02, 9.94304879e+00], [ 1.99579824e+01, -2.77367780e-01, -5.38759931e-02, 9.92880473e+00], [ 2.19388867e+01, -3.94078028e-01, -6.38232626e-02, 9.91467222e+00], [ 2.39157899e+01, -5.29044532e-01, -7.25085470e-02, 9.90044040e+00], [ 2.58886800e+01, -6.79428615e-01, -7.96477572e-02, 9.88573518e+00], [ 2.78575651e+01, -8.41973335e-01, -8.50921200e-02, 9.87012178e+00], [ 2.98224543e+01, -1.01325561e+00, -8.88112060e-02, 9.85329539e+00], [ 3.17833555e+01, -1.18989285e+00, -9.08631109e-02, 9.83511644e+00], [ 3.37402750e+01, -1.36869381e+00, -9.13680809e-02, 9.81559270e+00], [ 3.56932177e+01, -1.54676003e+00, -9.04859136e-02, 9.79484562e+00], [ 3.76421873e+01, -1.72154515e+00, -8.83971179e-02, 9.77307098e+00], [ 3.95871864e+01, -1.89087947e+00, -8.52877092e-02, 9.75050143e+00], [ 4.15282170e+01, -2.05296693e+00, -8.13374631e-02, 9.72737561e+00], [ 4.34652803e+01, -2.20636175e+00, -7.67114427e-02, 9.70391600e+00], [ 4.53983772e+01, -2.34993117e+00, -7.15546057e-02, 9.68031568e+00], [ 4.73275086e+01, -2.48281085e+00, -6.59892879e-02, 9.65673297e+00], [ 4.92526750e+01, -2.60435938e+00, -6.01165980e-02, 9.63329181e+00], [ 5.11738771e+01, -2.71411662e+00, -5.40182610e-02, 9.61008527e+00], [ 5.30911154e+01, -2.81176608e+00, -4.77585100e-02, 9.58718020e+00], [ 5.50043903e+01, -2.89710162e+00, -4.13860399e-02, 9.56462221e+00], [ 5.69137022e+01, -2.96999832e+00, -3.49360318e-02, 9.54244062e+00], [ 5.88190514e+01, -3.03038789e+00, -2.84322483e-02, 9.52065307e+00], [ 6.07204383e+01, -3.07823825e+00, -2.18892006e-02, 9.49926943e+00], [ 6.26178630e+01, -3.11353783e+00, -1.53143860e-02, 9.47829505e+00], [ 6.45133116e+01, -3.14256773e+00, -1.53143860e-02, 9.47841405e+00]]) obstacle_outline0 = np.array([[14., 1.7], [22., 1.7], [22., 4.7], [14., 4.7], [14., 1.7]]) # optimizer opt = Optimizer(params) opt.SetOptimizationVector(opt_vec) # costs ref_cost = ReferenceCost(params, 100.) ref_cost.SetReference(ref_line) jerk_cost = JerkCost(params, 10000.) outline = ObjectOutline() obstacle_outline1 = obstacle_outline0 + np.array([[30.0, -3.]]) outline.Add(obstacle_outline0, 0.) outline.Add(obstacle_outline1, 6.) object_cost = StaticObjectCost(params, 2.5, 10000.) object_cost.AddObjectOutline(outline) input_cost = InputCost(params, 10.) input_cost.SetLowerBound(np.array([[-0.2, -1.0]])) input_cost.SetUpperBound(np.array([[0.2, 1.0]])) # optimization problem functor = opt.AddFastSingleTrackFunctor(initial_state, params, [jerk_cost, object_cost, input_cost, ref_cost]) opt.FixOptimizationVector(0, 1) opt.Solve() opt.Report() inputs = opt.Result() trajectory = GenerateTrajectorySingleTrack(initial_state, inputs, params) cmap, norm = GetColorMap(0., 6.) fig, ax = plt.subplots(nrows=1, ncols=2) for t in np.arange(0, 6, 0.5): poly = outline.Query(t) ax[0].plot(poly[:, 0], poly[:, 1], color=cmap(norm(t))) colorbar_axis = ax[0].plot(trajectory[:, 0], trajectory[:, 1]) for i, pt in enumerate(trajectory[:]): ax[0].plot(pt[0], pt[1], color=cmap(norm(params.set("dt", 0.2)*i)), marker='o') ax[0].axis("equal") ax[1].plot(inputs[:-1, 0], label="Steering angle", marker='o') ax[1].plot(inputs[:-1, 1], label="Acceleration", marker='o', color="green") ax[1].legend() plt.show() print(repr(inputs)) print(repr(trajectory))
class Trainer(object): def __init__(self, args): super(Trainer, self).__init__() self.args = args if cfg.SEED > 0: random.seed(cfg.SEED) torch.manual_seed(cfg.SEED) torch.cuda.manual_seed_all(cfg.SEED) self.num_gpus = torch.cuda.device_count() self.distributed = self.num_gpus > 1 if self.distributed: torch.cuda.set_device(args.local_rank) torch.distributed.init_process_group( backend="nccl", init_method="env://" ) self.device = torch.device("cuda") self.rl_stage = False self.setup_logging() self.setup_dataset() self.setup_network() self.val_evaler = Evaler( eval_ids = cfg.DATA_LOADER.VAL_ID, gv_feat = cfg.DATA_LOADER.VAL_GV_FEAT, att_feats = cfg.DATA_LOADER.VAL_ATT_FEATS, eval_annfile = cfg.INFERENCE.VAL_ANNFILE ) self.test_evaler = Evaler( eval_ids = cfg.DATA_LOADER.TEST_ID, gv_feat = cfg.DATA_LOADER.TEST_GV_FEAT, att_feats = cfg.DATA_LOADER.TEST_ATT_FEATS, eval_annfile = cfg.INFERENCE.TEST_ANNFILE ) self.scorer = Scorer() def setup_logging(self): self.logger = logging.getLogger(cfg.LOGGER_NAME) self.logger.setLevel(logging.INFO) if self.distributed and dist.get_rank() > 0: return ch = logging.StreamHandler(stream=sys.stdout) ch.setLevel(logging.INFO) formatter = logging.Formatter("[%(levelname)s: %(asctime)s] %(message)s") ch.setFormatter(formatter) self.logger.addHandler(ch) if not os.path.exists(cfg.ROOT_DIR): os.makedirs(cfg.ROOT_DIR) fh = logging.FileHandler(os.path.join(cfg.ROOT_DIR, cfg.LOGGER_NAME + '.txt')) fh.setLevel(logging.INFO) fh.setFormatter(formatter) self.logger.addHandler(fh) self.logger.info('Training with config:') self.logger.info(pprint.pformat(cfg)) def setup_network(self): model = models.create(cfg.MODEL.TYPE) if self.distributed: # this should be removed if we update BatchNorm stats self.model = torch.nn.parallel.DistributedDataParallel( model.to(self.device), device_ids = [self.args.local_rank], output_device = self.args.local_rank, broadcast_buffers = False ) else: self.model = torch.nn.DataParallel(model).cuda() if self.args.resume > 0: self.model.load_state_dict( torch.load(self.snapshot_path("caption_model", self.args.resume), map_location=lambda storage, loc: storage) ) self.optim = Optimizer(self.model) self.xe_criterion = losses.create(cfg.LOSSES.XE_TYPE).cuda() self.rl_criterion = losses.create(cfg.LOSSES.RL_TYPE).cuda() def setup_dataset(self): self.coco_set = datasets.coco_dataset.CocoDataset( image_ids_path = cfg.DATA_LOADER.TRAIN_ID, input_seq = cfg.DATA_LOADER.INPUT_SEQ_PATH, target_seq = cfg.DATA_LOADER.TARGET_SEQ_PATH, gv_feat_path = cfg.DATA_LOADER.TRAIN_GV_FEAT, att_feats_folder = cfg.DATA_LOADER.TRAIN_ATT_FEATS, seq_per_img = cfg.DATA_LOADER.SEQ_PER_IMG, max_feat_num = cfg.DATA_LOADER.MAX_FEAT ) def setup_loader(self, epoch): self.training_loader = datasets.data_loader.load_train( self.distributed, epoch, self.coco_set) def eval(self, epoch): if (epoch + 1) % cfg.SOLVER.TEST_INTERVAL != 0: return None if self.distributed and dist.get_rank() > 0: return None val_res = self.val_evaler(self.model, 'val_' + str(epoch + 1)) self.logger.info('######## Epoch (VAL)' + str(epoch + 1) + ' ########') self.logger.info(str(val_res)) test_res = self.test_evaler(self.model,'test_' + str(epoch + 1)) self.logger.info('######## Epoch (TEST)' + str(epoch + 1) + ' ########') self.logger.info(str(test_res)) val = 0 for score_type, weight in zip(cfg.SCORER.TYPES, cfg.SCORER.WEIGHTS): val -= val_res[score_type] * weight return val def snapshot_path(self, name, epoch): snapshot_folder = os.path.join(cfg.ROOT_DIR, 'snapshot') return os.path.join(snapshot_folder, name + "_" + str(epoch) + ".pth") def save_model(self, epoch): if (epoch + 1) % cfg.SOLVER.SNAPSHOT_ITERS != 0: return if self.distributed and dist.get_rank() > 0: return snapshot_folder = os.path.join(cfg.ROOT_DIR, 'snapshot') if not os.path.exists(snapshot_folder): os.mkdir(snapshot_folder) torch.save(self.model.state_dict(), self.snapshot_path("caption_model", epoch+1)) def make_kwargs(self, indices, input_seq, target_seq, gv_feat, att_feats, att_mask): seq_mask = (input_seq > 0).type(torch.cuda.LongTensor) seq_mask[:,0] += 1 seq_mask_sum = seq_mask.sum(-1) max_len = int(seq_mask_sum.max()) input_seq = input_seq[:, 0:max_len].contiguous() target_seq = target_seq[:, 0:max_len].contiguous() kwargs = { cfg.PARAM.INDICES: indices, cfg.PARAM.INPUT_SENT: input_seq, cfg.PARAM.TARGET_SENT: target_seq, cfg.PARAM.GLOBAL_FEAT: gv_feat, cfg.PARAM.ATT_FEATS: att_feats, cfg.PARAM.ATT_FEATS_MASK: att_mask } return kwargs def scheduled_sampling(self, epoch): if epoch > cfg.TRAIN.SCHEDULED_SAMPLING.START: frac = (epoch - cfg.TRAIN.SCHEDULED_SAMPLING.START) // cfg.TRAIN.SCHEDULED_SAMPLING.INC_EVERY ss_prob = min(cfg.TRAIN.SCHEDULED_SAMPLING.INC_PROB * frac, cfg.TRAIN.SCHEDULED_SAMPLING.MAX_PROB) self.model.module.ss_prob = ss_prob def display(self, iteration, data_time, batch_time, losses, loss_info): if iteration % cfg.SOLVER.DISPLAY != 0: return if self.distributed and dist.get_rank() > 0: return info_str = ' (DataTime/BatchTime: {:.3}/{:.3}) losses = {:.5}'.format(data_time.avg, batch_time.avg, losses.avg) self.logger.info('Iteration ' + str(iteration) + info_str +', lr = ' + str(self.optim.get_lr())) for name in sorted(loss_info): self.logger.info(' ' + name + ' = ' + str(loss_info[name])) data_time.reset() batch_time.reset() losses.reset() def forward(self, kwargs): if self.rl_stage == False: logit = self.model(**kwargs) loss, loss_info = self.xe_criterion(logit, kwargs[cfg.PARAM.TARGET_SENT]) else: ids = kwargs[cfg.PARAM.INDICES] gv_feat = kwargs[cfg.PARAM.GLOBAL_FEAT] att_feats = kwargs[cfg.PARAM.ATT_FEATS] att_mask = kwargs[cfg.PARAM.ATT_FEATS_MASK] # max kwargs['BEAM_SIZE'] = 1 kwargs['GREEDY_DECODE'] = True kwargs[cfg.PARAM.GLOBAL_FEAT] = gv_feat kwargs[cfg.PARAM.ATT_FEATS] = att_feats kwargs[cfg.PARAM.ATT_FEATS_MASK] = att_mask self.model.eval() with torch.no_grad(): seq_max, logP_max = self.model.module.decode(**kwargs) self.model.train() rewards_max, rewards_info_max = self.scorer(ids, seq_max.data.cpu().numpy().tolist()) rewards_max = utils.expand_numpy(rewards_max) ids = utils.expand_numpy(ids) gv_feat = utils.expand_tensor(gv_feat, cfg.DATA_LOADER.SEQ_PER_IMG) att_feats = utils.expand_tensor(att_feats, cfg.DATA_LOADER.SEQ_PER_IMG) att_mask = utils.expand_tensor(att_mask, cfg.DATA_LOADER.SEQ_PER_IMG) # sample kwargs['BEAM_SIZE'] = 1 kwargs['GREEDY_DECODE'] = False kwargs[cfg.PARAM.GLOBAL_FEAT] = gv_feat kwargs[cfg.PARAM.ATT_FEATS] = att_feats kwargs[cfg.PARAM.ATT_FEATS_MASK] = att_mask seq_sample, logP_sample = self.model.module.decode(**kwargs) rewards_sample, rewards_info_sample = self.scorer(ids, seq_sample.data.cpu().numpy().tolist()) rewards = rewards_sample - rewards_max rewards = torch.from_numpy(rewards).float().cuda() loss = self.rl_criterion(seq_sample, logP_sample, rewards) loss_info = {} for key in rewards_info_sample: loss_info[key + '_sample'] = rewards_info_sample[key] for key in rewards_info_max: loss_info[key + '_max'] = rewards_info_max[key] return loss, loss_info def train(self): self.model.train() self.optim.zero_grad() iteration = 0 for epoch in range(cfg.SOLVER.MAX_EPOCH): if epoch == cfg.TRAIN.REINFORCEMENT.START: self.rl_stage = True self.setup_loader(epoch) start = time.time() data_time = AverageMeter() batch_time = AverageMeter() losses = AverageMeter() for _, (indices, input_seq, target_seq, gv_feat, att_feats, att_mask) in enumerate(self.training_loader): data_time.update(time.time() - start) input_seq = input_seq.cuda() target_seq = target_seq.cuda() gv_feat = gv_feat.cuda() att_feats = att_feats.cuda() att_mask = att_mask.cuda() kwargs = self.make_kwargs(indices, input_seq, target_seq, gv_feat, att_feats, att_mask) loss, loss_info = self.forward(kwargs) loss.backward() utils.clip_gradient(self.optim.optimizer, self.model, cfg.SOLVER.GRAD_CLIP_TYPE, cfg.SOLVER.GRAD_CLIP) self.optim.step() self.optim.zero_grad() self.optim.scheduler_step('Iter') batch_time.update(time.time() - start) start = time.time() losses.update(loss.item()) self.display(iteration, data_time, batch_time, losses, loss_info) iteration += 1 if self.distributed: dist.barrier() self.save_model(epoch) val = self.eval(epoch) self.optim.scheduler_step('Epoch', val) self.scheduled_sampling(epoch) if self.distributed: dist.barrier()
class Trainer(object): def __init__(self, args): super(Trainer, self).__init__() self.args = args self.setup_gpu() self.setup_logging() self.setup_loader() self.setup_network() def setup_gpu(self): if args.local_rank == -1: self.device = torch.device( "cuda" if torch.cuda.is_available() else "cpu") self.n_gpu = torch.cuda.device_count() self.distributed = False else: torch.cuda.set_device(args.local_rank) self.device = torch.device("cuda", args.local_rank) self.n_gpu = 1 torch.distributed.init_process_group( backend="nccl", init_method="env://", timeout=timedelta(minutes=180)) self.distributed = True print("device: {} n_gpu: {}, distributed training: {}".format( self.device, self.n_gpu, bool(args.local_rank != -1))) if cfg.SEED > 0: random.seed(cfg.SEED) torch.manual_seed(cfg.SEED) torch.cuda.manual_seed_all(cfg.SEED) def setup_logging(self): self.logger = logging.getLogger(cfg.LOGGER_NAME) self.logger.setLevel(logging.INFO) if self.distributed and dist.get_rank() > 0: return ch = logging.StreamHandler(stream=sys.stdout) ch.setLevel(logging.INFO) formatter = logging.Formatter( "[%(levelname)s: %(asctime)s] %(message)s") ch.setFormatter(formatter) self.logger.addHandler(ch) fh = logging.FileHandler( os.path.join(cfg.ROOT_DIR, cfg.LOGGER_NAME + '.txt')) fh.setLevel(logging.INFO) fh.setFormatter(formatter) self.logger.addHandler(fh) self.logger.info('Training with config:') self.logger.info(pprint.pformat(cfg)) def setup_loader(self): self.tokenizer = BertTokenizer.from_pretrained( cfg.TRAIN.BERT_MODEL, do_lower_case=cfg.TRAIN.DO_LOWER_CASE) self.train_dataset_loader, self.train_dataset, self.train_sampler = load_concap_train( args.local_rank, self.tokenizer) def setup_network(self): config = BertConfig.from_json_file(cfg.CONFIG_FILE) if cfg.TRAIN.FROM_PRETRAINED: model = BaseBertPreTraining.from_pretrained( cfg.TRAIN.FROM_PRETRAINED, config) else: model = BaseBertPreTraining(config) model.to(self.device) if args.local_rank != -1: self.model = torch.nn.parallel.DistributedDataParallel( model, find_unused_parameters=True, device_ids=[self.args.local_rank], output_device=self.args.local_rank, broadcast_buffers=False) elif self.n_gpu > 1: self.model = torch.nn.DataParallel(model) else: self.model = model epoch_steps = len(self.train_dataset_loader) n_steps = epoch_steps * cfg.SOLVER.NUM_TRAIN_EPOCHS self.optim = Optimizer(self.model, epoch_steps=epoch_steps, n_steps=n_steps) def display(self, iteration, batch_time, losses, loss_info): if iteration % cfg.SOLVER.DISPLAY != 0: return if self.distributed and dist.get_rank() > 0: return info_str = ' (BatchTime: {:.3}) losses = {:.5}'.format( batch_time.avg, losses.avg) self.logger.info('Iteration ' + str(iteration) + info_str + ', lr = ' + str(self.optim.get_lr())) for name in sorted(loss_info): self.logger.info(' ' + name + ' = ' + str(loss_info[name])) batch_time.reset() losses.reset() def snapshot_path(self, name, epoch): snapshot_folder = os.path.join(cfg.ROOT_DIR, 'snapshot') return os.path.join(snapshot_folder, name + "_" + str(epoch) + ".bin") def save_model(self, epoch): if (epoch + 1) % cfg.SOLVER.SNAPSHOT_ITERS != 0: return if self.distributed and dist.get_rank() > 0: return snapshot_folder = os.path.join(cfg.ROOT_DIR, 'snapshot') if not os.path.exists(snapshot_folder): os.mkdir(snapshot_folder) model_to_save = (self.model.module if hasattr(self.model, "module") else self.model) torch.save(model_to_save.state_dict(), self.snapshot_path("pytorch_model", epoch + 1)) def train(self): max_num_iter = len(self.train_dataset_loader) for epochId in range(int(cfg.SOLVER.NUM_TRAIN_EPOCHS)): if self.train_sampler is not None: self.train_sampler.set_epoch(epochId) self.model.train() start = time.time() batch_time = AverageMeter() for step, batch in enumerate(self.train_dataset_loader): iterId = step + (epochId * max_num_iter) self.optim.zero_grad() batch = tuple( t.cuda(device=self.device, non_blocking=True) for t in batch) input_ids, input_mask, segment_ids, lm_label_ids, image_feat, \ image_loc, imgfeat_cls_prob, imgfeat_label, imgfeat_mask = (batch) # TODO Train model self.save_model(epochId) if self.distributed: dist.barrier()
logging.info('Using default depth of 1') labels_true = change_label_depth(b.labels, depth=1) else: labels_true = change_label_depth(b.labels, depth=args.ldepth) # Choose appropriate clusterer and initialize. if args.algorithm == 'km': c = KMClust(v, labels_true) elif args.algorithm == 'lda': c = LDAClust(v, labels_true) else: logging.error('Clusterer name not recognized. Exiting...') sys.exit() logging.info('Clustering...') o = Optimizer() o.optimize(c, args.n_clusters, one_run=(not args.optimize)) o.latest_clusterer.export_csv_doc_topic() file_name = o.latest_clusterer.export_csv_topic_word() # Generate the heatmap for the last clusterer generate_heatmap(file_name) # Plot the Purities and Topic categories so far if args.optimize: o.plot_current() if args.search: # Compute wordclouds for the latest clusterer og = OfficialGoogler()
def test_triple_int_optimizer(self): params = Parameter() params.set("dt", 0.2) params.set("function_tolerance", 1e-6) params.set("max_num_iterations", 1000) initial_state = np.array([[0., 10., 0., 0., 10., 0., 0., 1., 0.], [2., 10., 0., 2., 10., 0., 0.2, 1., 0.], [4., 10., 0., 4., 10., 0., 0.4, 1., 0.], [6., 10., 0., 6., 10., 0., 0.6, 1., 0.]]) opt_vec = np.zeros(shape=(30, 3)) ref_line = np.array([[10., -100.], [50.1, 100.1]]) # optimizer opt = Optimizer(params) opt.SetOptimizationVector(opt_vec) # costs ref_cost = ReferenceLineCost(params, 10.) ref_cost.SetReferenceLine(ref_line) jerk_cost = JerkCost(params, 100.) speed_cost = SpeedCost(params, 10.) speed_cost.SetDesiredSpeed(20.) # optimization problem functor = opt.AddTripleIntFunctor(initial_state, params, [ref_cost, jerk_cost, speed_cost]) opt.FixOptimizationVector(0, 1) opt.Solve() opt.Report() inputs = opt.Result() trajectory = GenerateTrajectoryTripleInt(initial_state, inputs, params) fig = plt.figure() plt.subplot(144) ax = fig.add_subplot(144, projection='3d') plt.plot(trajectory[:, 0], trajectory[:, 3], trajectory[:, 6], marker='o') plt.plot([10, 50.1], [-100, 100], [0., 0.], marker='o') plt.axis("equal") plt.subplot(141) plt.plot(trajectory[:-3, 1], label="vx", marker='o') plt.plot(trajectory[:-3, 4], label="vy", marker='o', color="green") plt.plot(trajectory[:-3, 7], label="vz", marker='o', color="red") plt.xlabel("v [m/s]") plt.legend() plt.subplot(142) plt.plot(trajectory[:, 0], trajectory[:, 3], marker='o') plt.plot([10, 50.1], [-100, 100], marker='o') plt.axis("equal") plt.xlabel("x [m]") plt.ylabel("y [m]") plt.subplot(143) plt.plot(trajectory[:, 3], trajectory[:, 6], marker='o') plt.axis("equal") plt.xlabel("y [m]") plt.ylabel("z [m]") # fig, ax = plt.subplots(nrows=1, ncols=2) # colorbar_axis = ax[0].plot(trajectory[:, 0], trajectory[:, 3]) # ax[0].axis("equal") # ax[1].plot(inputs[:-1, 0], label="ax", marker='o') # ax[1].plot(inputs[:-1, 1], label="ay", marker='o', color="green") # ax[1].plot(inputs[:-1, 2], label="az", marker='o', color="red") # ax[1].legend() plt.show() print(repr(trajectory)) print(repr(inputs))
class Trainer(object): def __init__(self, args): super(Trainer, self).__init__() self.args = args if cfg.SEED > 0: np.random.seed(int(cfg.SEED)) random.seed(cfg.SEED) torch.manual_seed(cfg.SEED) torch.cuda.manual_seed_all(cfg.SEED) self.setup_logging() self.setup_dataset() self.setup_network() self.val_evaler = Evaler(eval_ids=cfg.DATA_LOADER.VAL_ID, gv_feat=cfg.DATA_LOADER.VAL_GV_FEAT, att_feats=cfg.DATA_LOADER.VAL_ATT_FEATS, eval_annfile=cfg.INFERENCE.VAL_ANNFILE) self.test_evaler = Evaler(eval_ids=cfg.DATA_LOADER.TEST_ID, gv_feat=cfg.DATA_LOADER.TEST_GV_FEAT, att_feats=cfg.DATA_LOADER.TEST_ATT_FEATS, eval_annfile=cfg.INFERENCE.TEST_ANNFILE) self.scorer = Scorer() self._init_ppo() def _init_ppo(self): self.nenvs = 4 * cfg.TRAIN.BATCH_SIZE self.noptepochs = 1 self.envsperbatch = cfg.TRAIN.BATCH_SIZE self.clip_range = 0.1 assert self.nenvs % cfg.TRAIN.BATCH_SIZE == 0 self.batch_next = None self.mv_approxkl = 0 self.mv_violate = 0 self.mv_entropy = 0 self.mv_total = 0 def setup_logging(self): self.logger = logging.getLogger(cfg.LOGGER_NAME) self.logger.setLevel(logging.INFO) ch = logging.StreamHandler(stream=sys.stdout) ch.setLevel(logging.INFO) formatter = logging.Formatter( "[%(levelname)s: %(asctime)s] %(message)s") ch.setFormatter(formatter) self.logger.addHandler(ch) if not os.path.exists(cfg.ROOT_DIR): os.makedirs(cfg.ROOT_DIR) fh = logging.FileHandler( os.path.join(cfg.ROOT_DIR, cfg.LOGGER_NAME + '.txt')) fh.setLevel(logging.INFO) fh.setFormatter(formatter) self.logger.addHandler(fh) self.logger.info('Training with config:') self.logger.info(pprint.pformat(cfg)) def setup_network(self): model = models.create(cfg.MODEL.TYPE) self.trainer = model.cuda() self.checkpointer = CaptionCheckpointer( self.trainer, os.path.join(cfg.ROOT_DIR, "snapshot")) if self.args.resume > 0: self.checkpointer.load( self.snapshot_path("caption_model", self.args.resume)) self.predictor = models.create(cfg.MODEL.TYPE).cuda() self.predictor.load_state_dict(self.trainer.state_dict()) self.optim = Optimizer(self.trainer) def setup_dataset(self): self.coco_set = datasets.coco_dataset.CocoDataset( image_ids_path=cfg.DATA_LOADER.TRAIN_ID, input_seq=cfg.DATA_LOADER.INPUT_SEQ_PATH, target_seq=cfg.DATA_LOADER.TARGET_SEQ_PATH, gv_feat_path=cfg.DATA_LOADER.TRAIN_GV_FEAT, att_feats_folder=cfg.DATA_LOADER.TRAIN_ATT_FEATS, seq_per_img=1, max_feat_num=cfg.DATA_LOADER.MAX_FEAT) def setup_loader(self, epoch): self.training_loader = datasets.data_loader.load_train( False, epoch, self.coco_set) def snapshot_path(self, name, epoch): snapshot_folder = os.path.join(cfg.ROOT_DIR, 'snapshot') return os.path.join(snapshot_folder, name + "_" + str(epoch) + ".pth") def save_model(self, iteration): snapshot_folder = os.path.join(cfg.ROOT_DIR, 'snapshot') if not os.path.exists(snapshot_folder): os.mkdir(snapshot_folder) torch.save(self.trainer.state_dict(), self.snapshot_path("caption_model", iteration)) def get_batch(self): epoch = 0 while True: self.setup_loader(epoch) for x in self.training_loader: yield epoch, x epoch += 1 def _set_kwargs(self, kwargs, repeat_factor): # sample kwargs['GREEDY_DECODE'] = False kwargs["NEED_PD"] = True kwargs["REPEAT_FACTOR"] = repeat_factor return kwargs def _prefix_rewards(self, kwargs, seq_prefix): kwargs[cfg.PARAM.MAX_GEN_LEN] = seq_prefix.shape[-1] kwargs[cfg.PARAM.GEN_RESULT] = utils.expand_tensor(seq_prefix, 5) with torch.no_grad(): seq_sample = self.predictor.extend_trajectory( **kwargs).detach().cpu().numpy().tolist() return seq_sample def _sample_trajectory(self, kwargs): self._set_kwargs(kwargs, 1) with torch.no_grad(): seq_sample, log_prob_sample = self.predictor.decode(**kwargs) seq_sample_list = seq_sample.detach().cpu().numpy().tolist() log_prob_sample, seq_sample = [ _.detach().cpu().numpy() for _ in [log_prob_sample, seq_sample] ] indices = kwargs[cfg.PARAM.INDICES] rewards_sample, _ = self.scorer(indices, seq_sample_list) repeat_factor = 5 kwargs = self._set_kwargs(kwargs, repeat_factor) kwargs['gx'], kwargs['encoder_out'], kwargs['p_att_feats'] , kwargs['att_mask']= \ self.predictor.init_gx_encoder_out_p_att_feats_att_mask(**kwargs) indices = utils.expand_numpy(kwargs[cfg.PARAM.INDICES], repeat_factor) advs = np.zeros_like(seq_sample, dtype=np.float32) for k in range(cfg.MODEL.SEQ_LEN): baseline, _ = self.scorer( indices, self._prefix_rewards( kwargs, torch.from_numpy(seq_sample[:, :k]).cuda())) baseline = baseline.reshape(-1, repeat_factor) advs[:, k] = rewards_sample - baseline.mean(-1) if seq_sample[:, k].sum() == 0: break seq_sample = seq_sample[:, None, :] advs = np.clip(advs[:, None, :], -1, 1) #advs = advs[:, None, :] log_prob_sample = log_prob_sample[:, None, ...] return seq_sample, log_prob_sample, advs def runner_run(self, iteration): mb_indices = [] mb_gv_feat = [] mb_att_feats = [] mb_att_mask = [] mb_sample_logprobs = [] mb_gen_result = [] mb_advs = [] for _ in range(self.nenvs // cfg.TRAIN.BATCH_SIZE): # data - indices, input_seq, target_seq, gv_feat, att_feats, att_mask epoch, data = next(self.batch_next) iteration += 1 indices = data[0] mb_indices.append(indices.reshape(-1, 1)) for x, y in zip(data[-3:], [mb_gv_feat, mb_att_feats, mb_att_mask]): y.append(x.numpy()) gv_feat, att_feats, att_mask = [_.cuda() for _ in data[-3:]] kwargs = { cfg.PARAM.INDICES: indices, cfg.PARAM.GLOBAL_FEAT: gv_feat, cfg.PARAM.ATT_FEATS: att_feats, cfg.PARAM.ATT_FEATS_MASK: att_mask } seq_sample, log_prob_sample, rewards = self._sample_trajectory( kwargs) trajectory = [log_prob_sample, seq_sample, rewards] for x, y in zip(trajectory, [mb_sample_logprobs, mb_gen_result, mb_advs]): y.append(x) max_att_num = np.max([_.shape[1] for _ in mb_att_feats]) for k, x in enumerate(mb_att_feats): after = max_att_num - x.shape[1] mb_att_feats[k] = np.pad(x, ((0, 0), (0, after), (0, 0)), mode="constant") mb_att_mask[k] = np.pad(mb_att_mask[k], ((0, 0), (0, after)), mode="constant") mb_indices, mb_gv_feat, mb_att_feats, mb_att_mask, \ mb_sample_logprobs, mb_gen_result, mb_advs = [np.vstack(_) for _ in [ mb_indices, mb_gv_feat, mb_att_feats, mb_att_mask, mb_sample_logprobs, mb_gen_result, mb_advs ]] return iteration, epoch, [ mb_indices, mb_gv_feat, mb_att_feats, mb_att_mask, mb_sample_logprobs, mb_gen_result, mb_advs ] def mb_train(self, kwargs): kwargs = self._set_kwargs(kwargs, 1) _, neglogpac = self.trainer.decode(**kwargs) sample_logprobs, gen_result, advs = [ kwargs[_] for _ in [cfg.PARAM.SAMPLE_LOGPROBS, cfg.PARAM.GEN_RESULT, cfg.PARAM.ADVS] ] trajectory = [sample_logprobs, gen_result, advs] for k, _ in enumerate(trajectory): trajectory[k] = _.view(-1, *_.shape[2:]) sample_logprobs, gen_result, advs = trajectory mask = gen_result > 0 mask = torch.cat( [mask.new_full((mask.shape[0], 1), True), mask[:, :-1]], 1) kl_div = torch.exp(sample_logprobs) * (sample_logprobs - neglogpac) kl_div = kl_div.sum(-1) kl_div = torch.masked_select(kl_div, mask) entropy = torch.sum(torch.exp(neglogpac) * (-neglogpac), dim=-1) entropy = entropy[mask].mean() neglogpac = torch.gather(neglogpac, 2, gen_result.unsqueeze(-1)).squeeze(-1) sample_logprobs = torch.gather(sample_logprobs, 2, gen_result.unsqueeze(-1)).squeeze(-1) advs_close_zero = (-1e-5 < advs) & (advs < 1e-5) mask &= ~advs_close_zero neglogpac = -torch.masked_select(neglogpac, mask) oldneglogpac = -torch.masked_select(sample_logprobs, mask) advs = torch.masked_select(advs, mask) ratio = torch.exp(oldneglogpac - neglogpac) pg_losses = -advs * ratio pg_losses2 = -advs * torch.clamp(ratio, 1.0 - self.clip_range, 1.0 + self.clip_range) self.mv_total = 0.9 * self.mv_total + 0.1 * pg_losses.shape[0] pg_loss = torch.max(pg_losses, pg_losses2).sum() / (cfg.TRAIN.BATCH_SIZE * 16) mask_positive = (advs > 0) & (ratio > 1 + self.clip_range) mask_negative = (advs < 0) & (ratio < 1 - self.clip_range) mask_total = mask_positive | mask_negative kl_div = kl_div.mean() loss = pg_loss self.mv_approxkl = 0.9 * self.mv_approxkl + 0.1 * kl_div.item() self.mv_entropy = 0.9 * self.mv_entropy + 0.1 * entropy.item() self.mv_violate = 0.9 * self.mv_violate + 0.1 * mask_total.sum().item() return loss def train(self): self.batch_next = self.get_batch() # eval - crucial to disable dropout self.trainer.eval() self.predictor.eval() epoch, iteration = 0, 0 val_current = None val_best = self._compute_val(iteration) self.logger.info("val @iteration 0: {}".format(val_best)) while True: iteration, epoch, data = self.runner_run(iteration) envinds = np.arange(self.nenvs) for _ in range(self.noptepochs): np.random.shuffle(envinds) for start in range(0, self.nenvs, self.envsperbatch): end = start + self.envsperbatch mbenvinds = envinds[start:end] indices = data[0][mbenvinds].reshape(-1) gv_feat, att_feats, att_mask, sample_logprobs, gen_result, advs = \ [torch.from_numpy(x).cuda() for x in [_[mbenvinds] for _ in data[1:]]] kwargs = { cfg.PARAM.INDICES: indices, cfg.PARAM.GLOBAL_FEAT: gv_feat, cfg.PARAM.ATT_FEATS: att_feats, cfg.PARAM.ATT_FEATS_MASK: att_mask, cfg.PARAM.SAMPLE_LOGPROBS: sample_logprobs, cfg.PARAM.GEN_RESULT: gen_result, cfg.PARAM.ADVS: advs } loss = self.mb_train(kwargs) self.optim.zero_grad() loss.backward() #torch.nn.utils.clip_grad_norm_(self.trainer.parameters(), 0.5, 2) self.optim.step() time.sleep(1) if iteration % 64 == 0: self.predictor.load_state_dict(self.trainer.state_dict()) val_current = self._compute_val(iteration) if val_best is None: val_best = val_current self.logger.info( "val_current @iteration {}: {}, val_predictor: {}".format( iteration, val_current, val_best)) self.logger.info("mv_approxkl: {}".format(self.mv_approxkl)) self.logger.info("mv_entropy: {}".format(self.mv_entropy)) self.logger.info("mv_violate: {}".format(self.mv_violate)) self.logger.info("mv_total: {}".format(self.mv_total)) if val_best <= val_current: self.save_model(23) val_best = val_current if iteration == 3584: break def _compute_val(self, iteration): val_res = self.val_evaler(self.trainer, 'val_' + str(iteration), 1) val = 0 for score_type, weight in zip(cfg.SCORER.TYPES, cfg.SCORER.WEIGHTS): # plus!!! val += val_res[score_type] * weight test_res = self.test_evaler(self.trainer, 'test_' + str(iteration), 2) self.logger.info('######## Iter (TEST) ' + str(iteration) + ' ########') self.logger.info(str(test_res)) # crucial! self.trainer.eval() return val
def test_single_track_optimizer(self): params = Parameter() params.set("wheel_base", 2.7) params.set("dt", 0.2) params.set("function_tolerance", 1e-6) params.set("max_num_iterations", 1000) params.set("num_threads", 1) initial_state = np.array([[0., 0., 0., 10.], [2., 0., 0., 10.], [4., 0., 0., 10.], [6., 0., 0., 10.]]) opt_vec = np.zeros(shape=(30, 2)) ref_line = np.array([[0., 4.], [1000., 4.]]) obstacle_outline0 = np.array([[14., 1.7], [22., 1.7], [22., 4.7], [14., 4.7], [14., 1.7]]) # optimizer opt = Optimizer(params) opt.SetOptimizationVector(opt_vec) # costs ref_cost = ReferenceLineCost(params, 100.) ref_cost.SetReferenceLine(ref_line) jerk_cost = JerkCost(params, 10000.) outline = ObjectOutline() obstacle_outline1 = obstacle_outline0 + np.array([[30.0, -2.]]) outline.Add(obstacle_outline0, 0.) outline.Add(obstacle_outline1, 6.) object_cost = StaticObjectCost(params, 2.5, 10000.) object_cost.AddObjectOutline(outline) input_cost = InputCost(params, 10.) input_cost.SetLowerBound(np.array([[-0.2, -1.0]])) input_cost.SetUpperBound(np.array([[0.2, 1.0]])) speed_cost = SpeedCost(params, 10.) speed_cost.SetDesiredSpeed(10.) # optimization problem functor = opt.AddFastSingleTrackFunctor( initial_state, params, [jerk_cost, ref_cost, input_cost, object_cost, speed_cost]) opt.FixOptimizationVector(0, 1) opt.Solve() opt.Report() inputs = opt.Result() trajectory = GenerateTrajectorySingleTrack(initial_state, inputs, params) cmap, norm = GetColorMap(0., 6.) fig, ax = plt.subplots(nrows=1, ncols=2) for t in np.arange(0, 6, 0.5): poly = outline.Query(t) ax[0].plot(poly[:, 0], poly[:, 1], color=cmap(norm(t))) colorbar_axis = ax[0].plot(trajectory[:, 0], trajectory[:, 1]) for i, pt in enumerate(trajectory[:]): ax[0].plot(pt[0], pt[1], color=cmap(norm(params.set("dt", 0.2) * i)), marker='o') ax[0].axis("equal") ax[1].plot(inputs[:-1, 0], label="Steering angle", marker='o') ax[1].plot(inputs[:-1, 1], label="Acceleration", marker='o', color="green") ax[1].legend() plt.show() print(repr(trajectory)) print(repr(inputs))