def make_env(args): env = Arguments() system = System(args) control = Cmrac(system) system.command = control.command env.system = system env.control = control env.args = args return env
def _make_eargs(self, A, y): args = self.args eigs, eigv = eig_thr(A, args.thr) if np.any(eigs == 0): redind = ([np.argmax(eigv[:, eigs == 0].T.dot(y)**2)] + np.where(eigs != 0)[0].tolist()) else: redind = eigs != 0 eigs = eigs[redind] eigv = eigv[:, redind] l1, ln = eigs[[0, -1]] g1, gn = np.diff(eigs)[[0, -1]] v = eigv.T.dot(y) v1, vn = v[[0, -1]] nv = sla.norm(y) k1 = (l1 + v1**2 / nv**2 * g1) / (l1 + g1) kn = (ln - vn**2 / nv**2 * gn) / (ln - gn) return Arguments(l1=l1, ln=ln, g1=g1, gn=gn, nv=nv, k1=k1, kn=kn, v1=v1, vn=vn)
def reset(self): args = self.args x = self.system.reset() xr = x.copy() what = np.zeros((args.ndim_basis, args.ndim_input)) e = x - xr z = (what.T.dot(self.basis(x))[:, np.newaxis] - self.Bhat.dot(np.eye(args.ndim_state) / args.tau_f + args.A).dot( e[:, np.newaxis])).ravel() basissum = np.zeros((args.ndim_basis, args.ndim_basis)) best_basissum = basissum.copy() estsum = np.zeros((args.ndim_basis, args.ndim_input)) best_estsum = estsum.copy() h = np.float64(0) best_h = h.copy() return Arguments( xr=xr, what=what, phif=self.basis(np.zeros(args.ndim_state)), z=z, basissum=basissum, estsum=np.zeros((args.ndim_basis, args.ndim_input)), q=0, p=1, best_basissum=best_basissum, best_estsum=best_estsum, h=h, best_h=best_h, )
def findab(self, A, y, h): # ipdb.set_trace() args = self.args eigs, eigv = eig_thr(A, args.thr) if np.any(eigs == 0): redind = ([np.argmax(eigv[:, eigs == 0].T.dot(y)**2)] + np.where(eigs != 0)[0].tolist()) else: redind = eigs != 0 eigs = eigs[redind] eigv = eigv[:, redind] l1, ln = eigs[[0, -1]] g1, gn = np.diff(eigs)[[0, -1]] v = eigv.T.dot(y) v1, vn = v[[0, -1]] nv = sla.norm(y) L = self.L Lgamma = self.Lgamma if gn == ln: return L / 2 / ln, L / 2 / nv**2 k1 = (l1 + v1**2 / nv**2 * g1) / (l1 + g1) kn = (ln - vn**2 / nv**2 * gn) / (ln - gn) s0 = 0 eargs = Arguments(l1=l1, ln=ln, g1=g1, gn=gn, nv=nv, k1=k1, kn=kn, s0=s0, v1=v1, L=L, Lgamma=Lgamma, h=h) self.eargs = eargs s = scipy.optimize.minimize_scalar(self._findr, method='bounded', bounds=(s0, Lgamma / (nv * h + 1e-8))) if s.fun < -l1: q, p = self._findab_from_s(np.clip(s.x, -1e-7, None)) else: q, p = 1, 0 return q, p
async def check_request(site): url = dict(url=site) async with aiohttp.ClientSession() as session: try: async with session.get(site) as r: if r.status >= 200 and r.status < 300: logger.info('available', extra=url) else: logger.error('not available', extra=url) except (aiohttp.ClientConnectorError, aiohttp.ClientConnectorError): logger.error('not available', extra=url) async def main(resourse): if isinstance(resourse, list): for site in resourse: await check_request(site) if __name__ == "__main__": args = Arguments() logger = LoggerValidation(__name__, args).get_logger() loop = asyncio.get_event_loop() try: task = loop.create_task(main(args.resourses)) loop.run_until_complete(task) except CancelledError: task.cancel() loop.stop()
k = l1 / l2 - v1**2 / nv**2 * (l1 / l2 - 1) ct1 = (a**2 + k * nv**2 / l1 * a * b - (l1 + l2) / l1 / l2 * a * c - nv**2 / l1 / l2 * b * c + 1 / l1 / l2 * c**2) ct2 = a + nv**2 / (l1 + l2) * b - 2 * c / (l1 + l2) if l1 <= l2: return (self._c_from_ab(a, b, l1, l2, v1, nv) * ((ct1 >= 0) & (ct2 >= 0))) else: return (self._c_from_ab(a, b, l1, l2, v1, nv) * ((ct1 >= 0) & (ct2 <= 0))) def _c_from_ab(self, a, b, l1, l2, v1, nv): g = l2 - l1 return a * l1 + 0.5 * (a * g + b * nv**2 - np.sign(g) * np.sqrt( (a * g + b * nv**2)**2 - 4 * a * b * g * v1**2)) if __name__ == '__main__': args = Arguments( thr=1e-8, lmax=10, ) agent = BeAgent(args) np.random.seed(1) a = np.random.rand(5, 5) A = a.T.dot(a) y = np.random.rand(5) agent.drawab(A, y)
def ba_region(): args = Arguments( thr=1e-8, lmax=10, ) agent = BeAgent(args) np.random.seed(1) q = np.random.rand(5, 5) A = q.T.dot(q) y = np.random.rand(5) eargs = agent._make_eargs(A, y) def f1(p, q, eargs): temp = lambda p, q: agent._f_from_ab( q, p, c=eargs.c1, l1=eargs.l1, l2=eargs.g1 + eargs.l1, v1=eargs.v1, nv=eargs.nv) return np.vectorize(temp)(p, q) def fn(p, q, eargs): temp = lambda p, q: agent._f_from_ab( q, p, c=eargs.cn, l1=eargs.ln, l2=eargs.ln - eargs.gn, v1=eargs.vn, nv=eargs.nv) return np.vectorize(temp)(p, q) bb, aa = np.meshgrid(np.linspace(-2, 5, 500), np.linspace(-0.2, 1.3, 500)) eargs.c1 = eargs.l1 eargs.cn = eargs.ln*1.01 fv1 = f1(bb, aa, eargs) fv2 = fn(bb, aa, eargs) eargs.cn = eargs.ln fv2_2 = fn(bb, aa, eargs) z1 = (fv1 > 0) & (fv2_2 > 0) fv2[fv2 == 0] = np.inf q, p = agent.findab(A, y, eargs.ln) lmin = agent._c_from_ab( q, p, eargs.l1, eargs.l1 + eargs.g1, eargs.v1, eargs.nv) fv3 = f1(bb, aa, Arguments(eargs, c1=lmin)) # Colorful # ======== plt.figure() plt.vlines(0, aa.min(), aa.max(), lw=1, alpha=.3) plt.hlines(0, bb.min(), bb.max(), lw=1, alpha=.3) plt.contourf(bb, aa, fv1/eargs.l1, levels=[1, 2, 3, 4, 5, 6, 7], alpha=.2, cmap='Blues_r') plt.contourf(bb, aa, fv2/eargs.ln, levels=[0, 0.2, 0.4, 0.6, 0.8, 1], alpha=.2, cmap='Reds_r') # cs = plt.contourf(bb, aa, z1.astype(int), 1, # colors='none', hatches=[None, '//']) plt.contourf(bb, aa, z1.astype(int), 1, colors=['w', 'k'], alpha=.1) c1 = plt.contour(bb, aa, fv1/eargs.l1, levels=[1, 2, 3, 4, 5, 6], linewidths=[1.2, 1, 1, 1, 1, 1], alpha=.5, colors='b') c2 = plt.contour(bb, aa, fv2/eargs.ln, levels=[0, 0.2, 0.4, 0.6, 0.8, 1], linewidths=[1, 1, 1, 1, 1, 1.2], alpha=.5, colors='r') plt.contour(bb, aa, z1, 1, colors='k', linestyles='dashed') c3 = plt.contour(bb, aa, fv3/eargs.l1, [lmin/eargs.l1], colors=['b'], alpha=.9, linewidths=1.5, linestyles='dashed') plt.plot(p, q, marker='*', c='g', markersize=12) plt.clabel(c1, fmt='%3.1f', inline=True, use_clabeltext=True, fontsize=10) plt.clabel(c2, fmt='%3.1f', inline=True, use_clabeltext=True, fontsize=10) plt.clabel(c3, fmt='%5.3f', inline=True, use_clabeltext=True, fontsize=11) plt.xlabel('p') plt.ylabel('q') plt.tight_layout() if len(sys.argv) > 1 and sys.argv[1] == 'save': if not os.path.exists('media'): os.mkdir('media') plt.savefig('media/ba_region_color.png', dpi=400) # Grayscale # ========= plt.figure() plt.vlines(0, aa.min(), aa.max(), lw=1, alpha=.3) plt.hlines(0, bb.min(), bb.max(), lw=1, alpha=.3) c1_dashed = plt.contour(bb, aa, fv1/eargs.l1, levels=[2, 3, 4, 5, 6], linestyles='dashed', linewidths=.7, colors='k') c1 = plt.contour(bb, aa, fv1/eargs.l1, levels=1, linewidths=.7, colors='k') c2_dashed = plt.contour(bb, aa, fv2/eargs.ln, levels=[0, 0.2, 0.4, 0.6, 0.8], linestyles='dashed', linewidths=.7, colors='k') c2 = plt.contour(bb, aa, fv2/eargs.ln, levels=1, linewidths=.7, colors='k') c3 = plt.contour(bb, aa, fv3/eargs.l1, [lmin/eargs.l1], colors=['k'], linewidths=1.5, linestyles='dashed') plt.contour(bb, aa, z1, 1, colors='k', linewidths=2) plt.plot(p, q, marker='*', c='k', markersize=11) kwargs = dict(fmt='%3.1f', inline=True, fontsize=10) plt.clabel(c1_dashed, **kwargs) plt.clabel(c1, **kwargs) plt.clabel(c2_dashed, **kwargs) plt.clabel(c2, **kwargs) plt.clabel(c3, **dict(kwargs, fmt='%5.3f', fontsize=11)) plt.xlabel(r'$p$') plt.ylabel(r'$q$') plt.tight_layout() if len(sys.argv) > 1 and sys.argv[1] == 'save': if not os.path.exists('media'): os.mkdir('media') plt.savefig('media/ba_region_gray.png', dpi=400) else: plt.show()
def step(self, t, x, uvar): args = self.args xr = uvar.xr what = uvar.what phif = uvar.phif z = uvar.z basissum = uvar.basissum best_basissum = uvar.best_basissum estsum = uvar.estsum best_estsum = uvar.best_estsum h = uvar.h best_h = uvar.best_h q, p = uvar.q, uvar.p u = self.get_ua(t, x, uvar) e = x - xr c = self.command(t) phi = self.basis(x) esterror = z + self.Bhat.dot(e[:np.newaxis]).ravel() / args.tau_f next_xr = xr + args.t_step * (args.A.dot(xr[:, np.newaxis]) + args.Br.dot(c[:, np.newaxis])).ravel() next_what = what + args.t_step * ( args.g_direct * phi[:, np.newaxis] * e.T.dot(self.P).dot(args.B) - args.g_indirect * (basissum.dot(what) - estsum)) next_phif = phif + args.t_step * (-(phif - phi) / args.tau_f).ravel() next_z = z + args.t_step * ( (-u[:, np.newaxis] - self.Bhat.dot(np.eye(args.ndim_state) / args.tau_f + args.A).dot( e[:, np.newaxis]) - z[:, np.newaxis]) / args.tau_f).ravel() if args.case == 'MRAC': q, p = 0, 0 elif args.case == 'BECMRAC': eigs, _ = eig_thr(basissum, args.thr) self.L = args.lambda_max * np.tanh(args.lmax_speed * t) + 1e-3 self.Lgamma = args.b_gamma * np.tanh(args.lmax_speed * t) + 1e-3 # self.L = np.clip(1.001 * max(eigs), 0.1, 3) if max(eigs) != 0: q, p = self.findab(basissum, phif, h) elif sla.norm(phif) > args.thr: q, p = 0.999, self.L / sla.norm(phif)**2 elif args.case == 'SLSCMRAC': q, p = 0, 1 elif args.case == 'FECMRAC': # ipdb.set_trace() p = args.t_step alp = (0.1 + (10 - 0.1) * np.tanh(0.1 * sla.norm( (phif - phi) / args.tau_f))) q = 1 - alp * args.t_step if eig_thr(basissum)[0][0] > eig_thr(best_basissum)[0][0]: best_basissum = basissum.copy() best_estsum = estsum.copy() best_h = h.copy() # print(t, eig_thr(basissum, 0)[0][0]) next_what = what + args.t_step * ( args.g_direct * phi[:, np.newaxis] * e.T.dot(self.P).dot(args.B) - args.g_indirect * (best_basissum.dot(what) - best_estsum)) next_basissum = q * basissum + p * np.outer(phif, phif) next_estsum = q * estsum + p * np.outer(phif, esterror) next_h = q * h + p * np.abs(p) * np.linalg.norm(phif) return Arguments(xr=next_xr, what=next_what, phif=next_phif, z=next_z, basissum=next_basissum, estsum=next_estsum, h=next_h, best_h=best_h, q=q, p=p, best_basissum=best_basissum, best_estsum=best_estsum)
def load(filename): tmp = np.load(filename) args = Arguments(**tmp) env = make_env(args) return env
record.append('z', uvar.z) record.append('e', x - uvar.xr) record.append('q', uvar.q) record.append('p', uvar.p) record.append('best_basissum', uvar.best_basissum) x = next_x uvar = next_uvar args.update(**record) return env if __name__ == '__main__': args = Arguments() args.A = np.array([[0, 1, 0], [-15.8, -5.6, -17.3], [1, 0, 0]]) args.B = np.array([[0, 1, 0]]).T args.Br = np.array([[0, 0, -1]]).T args.ndim_state = 3 args.ndim_input = 1 args.ndim_basis = 5 args.xinit = np.array([0.3, 0, 0]) # args.Kr = np.array([[1]]) args.Q_lyap = 1 * np.eye(args.ndim_state) args.g_direct = 10000 args.g_indirect = 0 args.case = 'MRAC' args.t_step = 1e-3 args.t_final = 80 args.W = np.array([-18.59521, 15.162375, -62.45153, 9.54708,
def train_loop( run_id, dataset_dir, ckpt_run_dir, output_dir, validation_only=False, use_cuda=False, light_target=False, seed=42, ): """Train loop""" train_epochs = 10 math_mode = "fp16" rank = dist.get_rank() world_size = dist.get_world_size() # Dataset arguments train_global_batch_size = 2**17 # Global batch size max_bs = 2**13 # Max batch size for used hardware update_freq = int(max(1, train_global_batch_size // (max_bs * world_size))) max_tokens = int(train_global_batch_size // (world_size * update_freq)) max_source_positions, max_target_positions = 80, 80 seq_len_multiple = 2 left_pad = (True, False) lang = ("en", "de") # specific arch model_args = deepcopy(DEFAULT_TRANSFORMER_ARCH) model_args["max_source_positions"] = max_source_positions model_args["max_target_positions"] = max_target_positions model_args["share_all_embeddings"] = True model_args["dropout"] = 0.1 model_args["softmax_type"] = "fast_fill" lr = 1.976e-3 optimizer_args = { "lr": lr, "eps": 1e-9, "betas": (0.9, 0.98), } scheduler_args = { "base_lr": lr, "warmup_init_lr": 0.0, "warmup_steps": 1000 } loss_scaling_fp16 = { "init_scale": 2.0**7, "scale_factor": 2, "scale_window": 2000, } criterion_args = {"smoothing": 0.1, "fast_xentropy": True} # Horovod stuff use_horovod = (math_mode == "fp16") and dist.get_backend() == dist.Backend.MPI if use_horovod: hvd.init() logger.info("Using horovod rank={}".format(hvd.rank())) tensor = torch.tensor([1]) res = hvd.allreduce(tensor, op=hvd.Sum) assert res[0] == world_size # Load train and validation datasets train_set = WMT17Dataset( dataset_dir, download=True, train=True, shuffle=True, lang=lang, left_pad=left_pad, max_positions=(max_source_positions, max_target_positions), seq_len_multiple=seq_len_multiple, ) validation_set = WMT17Dataset( dataset_dir, download=False, test=True, shuffle=True, lang=lang, left_pad=left_pad, max_positions=(max_source_positions, max_target_positions), seq_len_multiple=seq_len_multiple, ) src_dict, trg_dict = train_set.src_dict, train_set.trg_dict train_batches = get_batches(train_set, max_tokens=max_tokens, bsz_mult=8, shuffle=True, seed=seed) val_batches = get_batches(validation_set, max_tokens=max_tokens, bsz_mult=8, shuffle=False) train_batches = equalize_batches(train_batches, world_size, seed=seed) # Partition by rank train_batches = partition_dataset_by_rank(train_batches, rank, world_size) val_batches = partition_dataset_by_rank(val_batches, rank, world_size) total_train_points = sum(len(b) for b in train_batches) validate_every = update_freq * round( len(train_batches) * 0.30 / update_freq) # Validate every 30% assert (validate_every % update_freq) == 0 logger.info("Using {} total train points, {} batches".format( total_train_points, len(train_batches))) train_loader = DataLoader( train_set, num_workers=1, pin_memory=False, collate_fn=train_set.collater, batch_sampler=train_batches, ) val_loader = DataLoader( validation_set, num_workers=1, pin_memory=False, collate_fn=validation_set.collater, batch_sampler=val_batches, ) model = TransformerModel(Arguments(model_args), src_dict, trg_dict) criterion = LabelSmoothing(padding_idx=src_dict.pad(), **criterion_args) if use_cuda: model = model.cuda() criterion = criterion.cuda() fp_optimizer, optimizer, model = build_optimizer( model, optimizer_args, math_mode=math_mode, scaling_args=loss_scaling_fp16, use_horovod=use_horovod, use_cuda=use_cuda, ) scheduler = SQRTTimeDecayLRWithWarmup(optimizer, **scheduler_args) metrics = [BLEUScore(use_raw=True)] checkpointer = Checkpointer(ckpt_run_dir=ckpt_run_dir, rank=rank, freq=CheckpointFreq.BEST) translator = SequenceGenerator( model, src_dict=deepcopy(src_dict), trg_dict=deepcopy(trg_dict), beam_size=4, stop_early=True, normalize_scores=True, len_penalty=0.6, sampling=False, sampling_topk=-1, minlen=1, ) if not validation_only: if light_target: goal = task4_time_to_bleu_goal(20) else: goal = task4_time_to_bleu_goal(25) num_batches_per_device_train = len(train_loader) tracker = Tracker(metrics, run_id, rank, goal=goal) dist.barrier() tracker.start() for epoch in range(0, train_epochs): if torch.cuda.is_available(): torch.cuda.empty_cache() model.train() tracker.train() iter_sample_size = 0 for batch_idx, sample in enumerate(train_loader): tracker.batch_start() sample = prepare_batch(sample, use_cuda=use_cuda) tracker.record_batch_load() is_last = batch_idx == len(train_loader) update = (batch_idx % update_freq) == update_freq - 1 init = (batch_idx % update_freq) == 0 # Clear gradients in the optimizer. if init: fp_optimizer.zero_grad() iter_sample_size = 0 tracker.record_batch_init() # Compute the output output = model(**sample["net_input"]) tracker.record_batch_fwd_pass() loss, sample_size = compute_loss(sample, output, criterion) loss_per_sample = loss.item() / sample_size iter_sample_size += sample_size tracker.record_batch_comp_loss() # Backprop fp_optimizer.backward_loss(loss) tracker.record_batch_backprop() if update or is_last: # Get batch size over all workers full_bs = get_full_batch_size(iter_sample_size, world_size=world_size, use_cuda=use_cuda) updated = opt_step( fp_optimizer, tracker, full_bs, update_freq, math_mode, world_size, ) if updated: scheduler.step() tracker.batch_end() record_train_batch_stats( batch_idx=batch_idx, loss=loss_per_sample, output=torch.Tensor([0]), metric_results={}, tracker=tracker, num_batches_per_device_train=num_batches_per_device_train, ) if (batch_idx + 1) % validate_every == 0: if torch.cuda.is_available(): torch.cuda.empty_cache() metric_values, loss = validation_round( val_loader, metrics, criterion, translator, tracker=tracker, use_cuda=use_cuda, ) record_validation_stats(metric_values, loss, tracker, rank) if tracker.goal_reached: break model.train() tracker.train() if torch.cuda.is_available(): torch.cuda.empty_cache() metric_values, loss = validation_round( val_loader, metrics, criterion, translator, tracker=tracker, use_cuda=use_cuda, ) is_best = record_validation_stats(metric_values, loss, tracker, rank) checkpointer.save( tracker, model, optimizer, scheduler, tracker.current_epoch, is_best, ) tracker.epoch_end() if tracker.goal_reached: print("Goal Reached!") time.sleep(10) return else: cecf = CheckpointsEvaluationControlFlow( ckpt_dir=ckpt_run_dir, rank=rank, world_size=world_size, checkpointer=checkpointer, model=model, epochs=train_epochs, loss_function=criterion, metrics=metrics, use_cuda=use_cuda, dtype="fp32", max_batch_per_epoch=None, ) train_stats = cecf.evaluate_by_epochs(train_loader) with open(os.path.join(output_dir, "train_stats.json"), "w") as f: json.dump(train_stats, f)