Exemplo n.º 1
0
def signal_script_fail(return_code: int, msg="", die=False):
    if return_code:
        err_msg = "Failed to ".format(msg) if msg else "Fail"
        print(clr(err_msg, 'red'))

        if die:
            exit(return_code)
Exemplo n.º 2
0
    def end_train_task(self, model, optimizer, train_loader):
        optimizer.zero_grad()

        for _batch_idx, (data, target) in enumerate(train_loader):
            output = model(data)
            loss = functional.cross_entropy(output, target)
            loss.backward()

        grad = dict({})
        crt_mode = dict({})

        if self.merge_elasticities and self.constraints:
            # Add to previous matrices if in `merge` mode
            elasticity = self.constraints[0].elasticity
        else:
            elasticity = dict({})

        for name, param in model.named_parameters():
            if param.grad is not None:
                crt_mode[name] = param.detach().clone().view(-1)
                grad[name] = param.grad.detach().pow(2).clone().view(-1)
                if name in elasticity:
                    elasticity[name].add_(grad[name]).view(-1)
                else:
                    elasticity[name] = grad[name].clone().view(-1)

        new_constraint = Constraint(crt_mode, elasticity)
        if self.merge_elasticities:
            # Remove old constraints if in `merge` mode
            self.constraints.clear()
        self.constraints.append(new_constraint)

        print(
            clr(f"There are {len(self.constraints):d} elastic constraints!",
                attrs=["bold"]))
Exemplo n.º 3
0
def run_here(opts):
    """ If there's a single run in the experiment we run it in this process by
        calling run in the script.
    """
    prep_args = [opts.config_path, "--do"]
    if opts.copy_to_clipboard:
        prep_args.append("--cc")
    prepare_opts = prepare_parse_options(prep_args)
    # prepare_opts.args = opts.args
    prepare_opts.__dict__.update(vars(opts))

    # Fast_check if cfg file is already prepared
    with open(opts.config_path) as handler:
        dummy_config = yaml.load(handler, Loader=yaml.SafeLoader)
    if "out_dir" in dummy_config and os.path.isdir(dummy_config["out_dir"]):
        opts.experiment_path = dummy_config["out_dir"]

        run_path = dummy_config["out_dir"]
        leaf_path = os.path.join(run_path, ".__leaf")
        cfg_path = os.path.join(run_path, "cfg.yaml")
        if os.path.isfile(leaf_path):
            with open(cfg_path) as handler:
                cfg = yaml.load(handler, Loader=yaml.SafeLoader)
            args = dict_to_namespace(cfg)
            print(clr("\nStarting\n", attrs=["bold"]))
            get_function(opts)(args)
    else:
        # Virgin cfg
        opts.experiment_path = prepare_experiment(prepare_opts)

        with os.scandir(opts.experiment_path) as fit:
            for entry in fit:
                if entry.name.startswith(".") or not entry.is_dir():
                    continue
                with os.scandir(entry.path) as fit2:
                    for entry2 in fit2:
                        if entry2.name.startswith(".") or not entry2.is_dir():
                            continue
                        run_path = entry2.path
                        leaf_path = os.path.join(run_path, ".__leaf")
                        cfg_path = os.path.join(run_path, "cfg.yaml")
                        if os.path.isfile(leaf_path):
                            with open(cfg_path) as handler:
                                cfg = yaml.load(handler, Loader=yaml.SafeLoader)
                            args = dict_to_namespace(cfg)
                            print(clr("\nStarting\n", attrs=["bold"]))
                            get_function(opts)(args)
Exemplo n.º 4
0
def launch_experiment(opts):
    resources = LiftoffResources(opts)
    active_pids = []
    pid_path = os.path.join(opts.experiment_path, f".__{opts.session_id}")
    with open(pid_path, "a") as handler:
        handler.write(f"{os.getpid():d}\n")
    while True:
        available, next_gpu = resources.is_free()
        while not available:
            still_active_pids = []
            do_sleep = True
            for info in active_pids:
                pid, gpu, title, cmd, lock_path = info
                if still_active(pid, cmd):
                    still_active_pids.append(info)
                else:
                    print(f"> {title} seems to be over.")
                    os.remove(lock_path)
                    resources.free(gpu=gpu)
                    do_sleep = False
            active_pids = still_active_pids
            if do_sleep:
                time.sleep(1)
            available, next_gpu = resources.is_free()

        run_path = some_run_path(opts.experiment_path)

        if run_path is None:
            print("Nothing more to run here.")
            break

        lock_path = os.path.join(run_path, ".__lock")

        if lock_file(lock_path, opts.session_id):
            info = launch_run(run_path,
                              opts.script,
                              opts.session_id,
                              gpu=next_gpu)
            active_pids.append(info + (lock_path, ))
            resources.allocate(gpu=next_gpu)

    while active_pids:
        still_active_pids = []
        do_sleep = True
        for info in active_pids:
            pid, gpu, title, cmd, lock_path = info
            if still_active(pid, cmd):
                still_active_pids.append(info)
            else:
                print(f"> {title} seems to be over.")
                os.remove(lock_path)
                resources.free(gpu=gpu)
                do_sleep = False
        active_pids = still_active_pids
        if do_sleep:
            time.sleep(1)

    print(clr(f"Experiment {opts.experiment_path} ended.", attrs=["bold"]))
    os.remove(pid_path)
Exemplo n.º 5
0
    def _update_plot(self):
        mean_rw = self.total_rw / self.ep_cnt
        max_mean_rw = self.max_mean_rw
        bg_color = 'on_blue'
        bg_color = 'on_magenta' if mean_rw > max_mean_rw else bg_color
        self.max_mean_rw = mean_rw if mean_rw > max_mean_rw else max_mean_rw

        if self.cmdl.display_plots:
            self.vis.line(
                X=np.array([self.crt_step]),
                Y=np.array([mean_rw]),
                win=self.plot,
                update='append'
            )
        print(clr("[Evaluator] done in %5d steps. " % self.step_cnt,
              'grey', 'on_white')
              + clr(" rw/ep=%3.2f " % mean_rw, 'white', bg_color))
Exemplo n.º 6
0
    def display_board(self, structure):

        for i in range(0, 42):
            for j in range(0, 84):
                if structure[i][j] == '#':
                    print(clr(structure[i][j], "cyan"), end="")
                elif structure[i][j] == 'B':
                    print(clr(structure[i][j], "green"), end="")
                elif structure[i][j] == 'E':
                    print(clr(structure[i][j], "red"), end="")
                elif structure[i][j] == '^':
                    print(clr(structure[i][j], "yellow"), end="")
                elif structure[i][j] == '/':
                    print(clr(structure[i][j], "magenta"), end="")
                else:
                    print(structure[i][j], end="")
            print()
Exemplo n.º 7
0
 def __make_line(*args, **kwargs):
     args = [str(a) for a in args]
     kwargs = [
         f"{v['name']} = {clr(v['value'], *v.get('colors', ['red']))}"
         for _, v in kwargs.items()
     ]
     line = clr(" | ", "yellow").join(args + kwargs)
     return line
Exemplo n.º 8
0
def idx2seq(x, ii, pp=None):
    seq = []
    for kk, vv in enumerate(x):
        if vv == 0:
            break
        if vv in worddicts_r[ii]:
            word = worddicts_r[ii][vv]

            if pp is None:
                if vv > model_options['voc_sizes'][ii]:
                    seq.append(clr(word, 'green'))
                else:
                    seq.append(word)
            else:
                if pp[kk] == 0:
                    seq.append(clr(word, 'red'))
                elif (pp[kk] > 0) and (pp[kk] <= 0.25):
                    seq.append(clr(word, 'yellow'))
                elif (pp[kk] > 0.25) and (pp[kk] <= 0.5):
                    seq.append(clr(word, 'green'))
                elif (pp[kk] > 0.5) and (pp[kk] <= 0.75):
                    seq.append(clr(word, 'cyan'))
                else:
                    seq.append(clr(word, 'blue'))

        else:
            seq.append(clr('UNK', 'white'))
    return ' '.join(seq)
Exemplo n.º 9
0
    def check_stop_fuzzing(self, n: int) -> bool:
        if self.verification.stop_fuzzing():
            print(
                clr("#### Finished fuzzing after {} iterations".format(n),
                    'green',
                    attrs=['bold']))
            return True

        return False
Exemplo n.º 10
0
def discord_run(_token, v=''):
    _token = [x for x in _token.split(_ssy) if x]
    print(
        clr('Token "%s********************" Started' % _token[0][:39], 'white',
            'on_blue'))
    _discord['file'] += "\nclient.run('" + _token[0] + "')"
    v = open('comm/DIS.py', 'w')
    v.write(_discord['file'])
    v.close()
    system('start comm/dis.py')
Exemplo n.º 11
0
def run_alien(shared_objects, cfg):
    assert len(cfg.envs.minecraft.ports) >= 2, \
        clr('Not enough clients (need at least 2)', 'white', 'on_red')

    # builder = PigChaseTopDownStateBuilder()
    env = PigChaseEnvironment(
        parse_clients_args(cfg.envs.minecraft.ports),
        PigChaseSymbolicStateBuilder(),
        role=cfg.alien.role,
        randomize_positions=cfg.envs.minecraft.randomize_positions)

    agent = PigChaseChallengeAgent(cfg.alien.name)
    agent_type = PigChaseEnvironment.AGENT_TYPE_1

    print(
        clr(
            "[ %s ] type=%s, role=%d. Agent started." %
            (cfg.alien.name, agent_type, cfg.alien.role), 'blue'))

    obs = env.reset(agent_type)
    reward = 0
    ep_cnt = 0
    viz_rewards = []
    is_terminal = False

    while True:

        # select an action
        action = agent.act(obs, reward, is_terminal, is_training=True)

        # reset if needed
        if env.done:
            obs = env.reset(agent_type)
            ep_cnt += 1
            if ep_cnt % cfg.general.report_freq == 0:
                print("[A*?] Ep: %d | Rw: %d" %
                      (ep_cnt, sum(viz_rewards) / cfg.general.report_freq))
                viz_rewards.clear()

        # take a step
        obs, reward, is_terminal = env.do(action)
        viz_rewards.append(reward)
Exemplo n.º 12
0
def config_to_string(cfg: Namespace,
                     indent: int = 0,
                     color: bool = True,
                     verbose: bool = False) -> str:
    """Creates a multi-line string with the contents of @cfg."""

    text = ""
    for key, value in cfg.__dict__.items():
        if key.startswith("__") and not verbose:
            # censor some fields
            pass
        else:
            ckey = clr(key, "yellow", attrs=["bold"]) if color else key
            text += " " * indent + ckey + ": "
            if isinstance(value, Namespace):
                text += "\n" + config_to_string(value, indent + 2, color=color)
            else:
                cvalue = clr(str(value), "white") if color else str(value)
                text += cvalue + "\n"
    return text
Exemplo n.º 13
0
    def fuzz(self):

        for n, state in enumerate(self.search_plan):
            print(
                clr("State {}/{}: {}".format(n, self.search_stats["total"],
                                             state),
                    'green',
                    attrs=['bold']))

            print(
                clr("#### Executing state transition", 'cyan', attrs=['bold']))
            self.transition.perform_state_transition(state)

            print(clr("#### Verifying properties", 'cyan', attrs=['bold']))
            self.verification.verify_fib_properties(state)

            if self.check_stop_fuzzing(n):
                break

            print("===================================")
Exemplo n.º 14
0
def print_fancy_err(err, issue=None, fix=None):
    _, _, exc_tb = sys.exc_info()
    tb = traceback.extract_tb(exc_tb)
    stack = traceback.extract_stack()
    what = "what: {}: {}".format(
        type(err).__name__, clr(str(err), attrs=["bold"]))
    fpath, line_no, _, code = tuple(tb[-1])
    where = 'where: "{0}", line {1}\n\n\t| {1} >   {2}'.format(
        fpath, line_no, code)
    fpath, line_no, _, code = tuple(stack[1])
    entry_point = 'from: "{0}", line {1}\n\n\t| {1} >   {2}'.format(
        fpath, line_no, code)
    msg = "\n\t{}\n\t{}\n\n\t{}\n".format(what, where, entry_point)
    if issue is not None:
        msg += "\n\tCause: {}.".format(clr(issue, "red"))
    if fix is not None:
        hint = clr("Hint", attrs=["underline"])
        msg += "\n\t{}: {}.".format(hint, clr(fix, "green"))
    msg += "\n"
    print(msg)
Exemplo n.º 15
0
 def _debug_states(self, state_batch, next_state_batch, mask):
     for i in range(24):
         for j in range(24):
             px = state_batch[0, 0, i, j]
             if px < 0.90:
                 print(clr("%.2f  " % px, 'magenta'), end="")
             else:
                 print(("%.2f  " % px), end="")
         print()
     for i in range(24):
         for j in range(24):
             px = next_state_batch[0, 0, i, j]
             if px < 0.90:
                 print(clr("%.2f  " % px, 'magenta'), end="")
             else:
                 print(clr("%.2f  " % px, 'white'), end="")
         print()
     if mask[0] == 0:
         print(clr("Done batch ............", 'magenta'))
     else:
         print(".......................")
Exemplo n.º 16
0
 def _debug_states(self, state_batch, next_state_batch, mask):
     for i in range(24):
         for j in range(24):
             px = state_batch[0, 0, i, j]
             if px < 0.90:
                 print(clr("%.2f  " % px, 'magenta'), end="")
             else:
                 print(("%.2f  " % px), end="")
         print()
     for i in range(24):
         for j in range(24):
             px = next_state_batch[0, 0, i, j]
             if px < 0.90:
                 print(clr("%.2f  " % px, 'magenta'), end="")
             else:
                 print(clr("%.2f  " % px, 'white'), end="")
         print()
     if mask[0] == 0:
         print(clr("Done batch ............", 'magenta'))
     else:
         print(".......................")
Exemplo n.º 17
0
def get_best_agent(results, verbose=True):
    """ Receives a list of trials and returns the best model it finds across
        trials and time-steps.

        `results` is of the form [(`experiment_path`, [*.yaml, *.pth, ...]), ...]
    """
    if verbose:
        exp_no = len(results)
        models_no = exp_no * 50
        print(f"Searching through {exp_no} experiments, ~{models_no} models.")

    best_score = -float("inf")
    best_agent = None
    for i, (exp_path, file_names) in enumerate(results):
        exp_path = Path(exp_path)
        cfg = yaml.safe_load(open(exp_path / "cfg.yaml", "r"))

        for file in file_names:
            file_path = exp_path / file
            if file_path.suffix == ".pth":
                model = torch.load(file_path)
                rw_per_ep = model["rw_per_ep"]
                print("x", end="", flush=True)
                if rw_per_ep > best_score:
                    best_agent = (cfg, model)
                    best_score = rw_per_ep
                    print("X", end="")

    if verbose:
        cfg, model = best_agent
        msg = (
            f"\n\nBest agent on game {cfg['game']}" +
            f" with rw/ep={model['rw_per_ep']:5.2f} @ step={model['step']}:")
        print(clr(msg, "green"))
        for k, v in cfg.items():
            if k in ("game", "double", "prioritized",
                     "_experiment_parameters"):
                print(clr(k, "yellow"), ": ", v)

        return best_agent
Exemplo n.º 18
0
def interpret_verification_results(ping_results: dict):
    all_successful: bool = True

    for property_id, ver_res in ping_results.items():
        if ver_res["status"] == 0:
            continue
        else:
            all_successful = False

        pretty_print_failure(property_id, ver_res)

    if all_successful:
        print(clr("All properties HOLD", 'green'))
Exemplo n.º 19
0
def env_factory(cmdl, mode):
    # Undo the default logger and configure a new one.
    gym.undo_logger_setup()
    logger = logging.getLogger()
    logger.setLevel(logging.WARNING)

    print(clr("[Main] Constructing %s environment." % mode, attrs=['bold']))
    env = gym.make(cmdl.env_name)

    if hasattr(cmdl, 'rescale_dims'):
        state_dims = (cmdl.rescale_dims, cmdl.rescale_dims)
    else:
        state_dims = env.observation_space.shape[0:2]

    env_class, hist_len, cuda = cmdl.env_class, cmdl.hist_len, cmdl.cuda

    if mode == "training":
        env = PreprocessFrames(env, env_class, hist_len, state_dims, cuda)
        if hasattr(cmdl, 'reward_clamp') and cmdl.reward_clamp:
            env = SqueezeRewards(env)
        if hasattr(cmdl, 'done_after_lost_life') and cmdl.done_after_lost_life:
            env = DoneAfterLostLife(env)
        print('-' * 50)
        return env

    elif mode == "evaluation":
        if cmdl.eval_env_name != cmdl.env_name:
            print(
                clr("[%s] Warning! evaluating on a different env: %s" %
                    ("Main", cmdl.eval_env_name),
                    'red',
                    attrs=['bold']))
            env = gym.make(cmdl.eval_env_name)

        env = PreprocessFrames(env, env_class, hist_len, state_dims, cuda)
        env = EvaluationMonitor(env, cmdl)
        print('-' * 50)
        return env
Exemplo n.º 20
0
def config_to_string(cfg: Namespace,
                     indent: int = 0,
                     color: bool = True) -> str:
    """Creates a multi-line string with the contents of @cfg."""

    text = ""
    for key, value in cfg.__dict__.items():
        ckey = clr(key, "green", attrs=["bold"]) if color else key
        text += " " * indent + ckey + ": "
        if isinstance(value, Namespace):
            text += "\n" + config_to_string(value, indent + 2, color=color)
        else:
            text += str(value) + "\n"
    return text
Exemplo n.º 21
0
    def _end_train_task(self):
        if self.crt_task_idx > 1 and self.first_task_only:
            return

        train_loader, _ = self.crt_data_loaders
        assert hasattr(train_loader, "__len__")
        self._optimizer.zero_grad()
        model = self._model

        for _batch_idx, (data, targets, head_idx) in enumerate(train_loader):
            outputs = model(data, head_idx=head_idx)
            loss = torch.tensor(0., device=self.device)
            for output, target in zip(outputs, targets):
                loss += functional.cross_entropy(output, target)
            loss.backward()

        grad = dict({})
        crt_mode = dict({})

        if self.merge_elasticities and self.constraints:
            # Add to previous matrices if in `merge` mode
            diag_hessian = self.constraints[0].diag_hessian
            gradient = self.constraints[0].gradient
        else:
            diag_hessian = dict({})
            gradient = dict({})

        for name, param in model.named_parameters():
            if param.requires_grad:
                crt_mode[name] = param.detach().clone().view(-1)
                grad[name] = param.grad.clone().detach().view(-1)
                grad[name].requires_grad = False
                if name in gradient:
                    gradient[name].add_(grad[name])
                    diag_hessian[name].add_(grad[name] * grad[name])
                else:
                    gradient[name] = grad[name]
                    diag_hessian[name] = grad[name] * grad[name]

        new_constraint = FullConstraint(self.crt_task_idx, self.crt_task_epoch,
                                        crt_mode, gradient, diag_hessian)
        if self.merge_elasticities:
            # Remove old constraints if in `merge` mode
            self.constraints.clear()
        self.constraints.append(new_constraint)

        print(
            clr(f"There are {len(self.constraints):d} elastic constraints!",
                attrs=["bold"]))
Exemplo n.º 22
0
def _seqs2words(caps, idict, actions=None, target=0):
    capsw = []
    colors = ['cyan', 'green', 'yellow', 'red', 'magenta']

    for kk, cc in enumerate(caps):
        ww = []
        pos = 0
        iss = 0
        flag = False

        for w in cc:
            if w == 0:
                break

            word = idict[w]
            if actions is not None:
                while True:
                    if iss == len(actions[kk]):
                        word = clr(word, 'white')
                        break

                    if actions[kk][iss] == target:
                        word = clr(word, colors[pos % len(colors)])
                        iss += 1
                        flag = True
                        break
                    else:
                        if flag:
                            pos += 1
                            flag = False
                        iss += 1

            ww.append(word)

        capsw.append(' '.join(ww))
    return capsw
Exemplo n.º 23
0
    def __init__(self, env):
        super(DoneAfterLostLife, self).__init__(env)

        self.no_more_lives = True
        self.crt_live = env.unwrapped.ale.lives()
        self.has_many_lives = self.crt_live != 0

        if self.has_many_lives:
            self._step = self._many_lives_step
        else:
            self._step = self._one_live_step
        not_a = clr("not a", attrs=['bold'])

        print("[DoneAfterLostLife Wrapper]  %s is %s many lives game." %
              (env.env.spec.id, "a" if self.has_many_lives else not_a))
Exemplo n.º 24
0
def test(model, test_loader, name="Test", device=None):
    model.eval()
    total_no, correct_no = 0, 0
    with torch.no_grad():
        for data, target in test_loader:
            if device:
                data, target = data.to(device), target.to(device)
            output = model(data)
            correct_no += (output.argmax(dim=1) == target).sum()
            total_no += output.size(0)
    acc = float(correct_no) / float(total_no)
    correct_no, total_no = 0, 0
    sys.stdout.write(
        clr(f" | [Eval - {name:s}]  Acc: {acc*100:2.2f}%\n", attrs=['bold']))
    model.train()
Exemplo n.º 25
0
    def get_item(_i, _o, _p):
        if _i != _o:
            el = f'{_vocab.itos[_i]}/{_vocab.itos[_o]}'
        else:
            el = _vocab.itos[_i]

        if _p > 0.75:
            p_color = None
        elif _p > 0.5:
            p_color = 'blue'
        elif _p > 0.25:
            p_color = 'yellow'
        else:
            p_color = 'red'

        return clr(el, p_color)
Exemplo n.º 26
0
def train_agent(shared_objects, cfg):

    env = PigChaseEnvironment(
        parse_clients_args(cfg.envs.minecraft.ports),
        PigChaseTopDownStateBuilder(),
        role=cfg.agent.role,
        randomize_positions=cfg.envs.minecraft.randomize_positions)
    agent = get_agent(cfg.agent.type)(cfg.agent.name, ENV_ACTIONS)

    print(
        clr(
            "[ %s ] type=%s, role=%d. Agent started." %
            (cfg.agent.name, cfg.agent.type, cfg.agent.role), 'cyan'))

    obs = env.reset()
    reward = 0
    is_terminal = False
    viz_rewards = []
    ep_cnt = 0

    start_time = time.time()

    print("No of epochs: %d. Max no of steps/epoch: %d" %
          (cfg.training.episodes_no, cfg.training.max_step_no))

    training_steps = cfg.training.episodes_no * cfg.training.max_step_no
    for step in range(1, training_steps + 1):
        # check if env needs reset
        if env.done:
            obs = env.reset()
            ep_cnt += 1
            if ep_cnt % cfg.general.report_freq == 0:
                print("[DQN] Ep: %d | Rw: %d" %
                      (ep_cnt, sum(viz_rewards) / cfg.general.report_freq))
                viz_rewards.clear()

        # select an action
        action = agent.act(obs, reward, is_terminal, is_training=True)

        # take a step
        obs, reward, is_terminal = env.do(action)
        viz_rewards.append(reward)

    elapsed_time = time.time() - start_time
    print("Finished in %.2f seconds at %.2ffps." %
          (elapsed_time, training_steps / elapsed_time))
Exemplo n.º 27
0
def double_check_violations(failed_properties: dict, fib: Fib, fuzz_data,
                            failed_nets) -> dict:
    print(
        clr(
            "## Giving network {} seconds to converge before double checking".
            format(const.CONV_TIME), 'cyan'))
    time.sleep(const.CONV_TIME)

    property_failures = dict()

    for prop_id, prop in failed_properties.items():
        print("Double checking property {}".format(prop_id))
        reachability_res = verify_fib_property(prop, fib, fuzz_data,
                                               failed_nets)

        if reachability_res["status"] != 0:
            property_failures.update({prop_id: reachability_res})

    return property_failures
Exemplo n.º 28
0
def run(opt):
    """ Run experiment. This function is being launched by liftoff.
    """
    U.configure_logger(opt)

    # set seed
    opt.seed = (opt.run_id + 1) * opt.base_seed
    torch.manual_seed(opt.seed)

    # configure env
    env = ActionWrapper(TorchWrapper(gym.make(opt.env_name)))
    env.seed(opt.seed)

    # build estimator
    estimator = ActorCriticEstimator(
        env.observation_space.shape[0],
        env.action_space,
        hidden_size=opt.hidden_size,
    )
    # load checkpoint and reset
    rlog.info("Loading model from %s", opt.model_state)
    estimator.load_state_dict(torch.load(opt.model_state)["policy"])
    estimator.reset_policy()
    rlog.info("Policy reset.")
    if opt.freeze_critic:
        estimator.freeze_critic()
        rlog.info("Freezed feature extractor and critic.")

    # build the agent
    policy_improvement, policy_evaluation = build_agent(
        opt, env, estimator=estimator
    )

    # log
    rlog.info(f"\n{U.config_to_string(opt)}")
    rlog.info(policy_improvement)

    # train
    try:
        train(env, policy_improvement, policy_evaluation, opt)
    except Exception as err:
        rlog.error(clr(str(err), "red", attrs=["bold"]))
        raise err
Exemplo n.º 29
0
def play(opt, env, policy_evaluation):
    print(clr("Start playing!", "red"))
    ep_cnt, done = 0, True
    while True:
        if done:
            state, reward, done = env.reset(), 0, False
            ep_rw = 0

        with torch.no_grad():
            pi = policy_evaluation(state)

        state, reward, done, _ = env.step(pi.action)
        env.render()

        ep_rw += reward

        if done:
            print(f"Episode {ep_cnt:3d} done. Total return: {ep_rw:5.2f}.")
            ep_cnt += 1
Exemplo n.º 30
0
def get_anomaly_lines(_vocab, _input, _output, _probs, _device):
    from termcolor import colored as clr

    def get_item(_i, _o, _p):
        if _i != _o:
            el = f'{_vocab.itos[_i]}/{_vocab.itos[_o]}'
        else:
            el = _vocab.itos[_i]

        if _p > 0.75:
            p_color = None
        elif _p > 0.5:
            p_color = 'blue'
        elif _p > 0.25:
            p_color = 'yellow'
        else:
            p_color = 'red'

        return clr(el, p_color)

    b_anomalies = 0
    for line_no, (i_line, o_line,
                  p_line) in enumerate(zip(_input, _output, _probs)):
        o_line = torch.cat(
            [_input[line_no:line_no + 1, 0],
             o_line.argmax(dim=1)])
        p_line = torch.cat([torch.as_tensor([1.0]).to(_device), p_line])
        if (i_line != o_line).sum() != 0:
            b_anomalies += 1
            l_elems = []
            l_elems.append(
                f'{get_item(i_line[0], o_line[0] , p_line[0])}@{get_item(i_line[1], o_line[1] , p_line[1])}'
            )  #src_user
            l_elems.append(
                f'{get_item(i_line[2], o_line[2] , p_line[2])}@{get_item(i_line[3], o_line[3] , p_line[3])}'
            )  #dst_user
            for i in range(4, 11):
                l_elems.append(get_item(i_line[i], o_line[i], p_line[i]))
            l_elems.append(clr(f'{p_line.prod() * 100:.4f}%', attrs=['bold']))
            print(",".join(l_elems))

    return b_anomalies
Exemplo n.º 31
0
 def display_setup(self, env, config):
     emph = [
         "env_name", "agent_type", "label", "batch_size", "lr", "hist_len"
     ]
     print("-------------------------------------------------")
     for k in config.__dict__:
         if config.__dict__[k] is not None:
             v = config.__dict__[k]
             space = "." * (32 - len(k))
             config_line = "%s:  %s  %s" % (k, space, v)
             for e in emph:
                 if k == e:
                     config_line = clr(config_line, attrs=['bold'])
             print(config_line)
     print("-------------------------------------------------")
     custom = {"no_of_actions": self.action_no}
     for k, v in custom.items():
         space = "." * (32 - len(k))
         print("%s:  %s  %s" % (k, space, v))
     print("-------------------------------------------------")
Exemplo n.º 32
0
def lock_experiment(opts, unlock=False):
    """ Clean a specific argument
    """
    info = defaultdict(int)

    experiment_path = opts.experiment_path

    timestamp = f"{datetime.now():{opts.timestamp_fmt:s}}"
    prefix = f"[{timestamp:s}][{opts.session_id}]"
    with os.scandir(experiment_path) as fit:
        for entry in fit:
            if entry.name.startswith(".") or not entry.is_dir():
                continue
            with os.scandir(entry.path) as fit2:
                for entry2 in fit2:
                    if entry2.name.startswith(".") or not entry2.is_dir():
                        continue
                    try:
                        run_id = int(entry2.name)
                        if run_id in opts.runs:
                            if unlock:
                                unlock_run(entry2.path, info, prefix, opts)
                            else:
                                lock_run(entry2.path, info, prefix, opts)
                    except ValueError:
                        pass
    if unlock:
        print(f"{info['nlocks']:d} .__lock files deleted")
        print(f"{info['nstrange']:d} strange folders")
    else:
        print(f"{info['nlocks']:d} .__lock files added")
        print(f"{info['nraced']:d} times just lost the .__lock to some other process")
        print(f"{info['nstarted']:d} runs were already started")
        print(f"{info['nstrange']:d} strange folders")

    if not opts.do:
        print(
            "\nThis was just a simultation. Rerun with",
            clr("--do", attrs=["bold"]),
            "to lock/unlock the experiment for real.",
        )
Exemplo n.º 33
0
    def display_stats(self, start_time):
        fps = self.step_cnt / (time.time() - start_time)

        print(clr("[%s] step=%7d, fps=%.2f " % (self.name, self.step_cnt, fps),
                  'grey', 'on_white'))
        self.ep_reward.clear()
Exemplo n.º 34
0
def not_implemented(obj):
    import inspect
    method_name = inspect.stack()[1][3]
    raise RuntimeError(
        clr(("%s.%s not implemented nor delegated." %
            (obj.name, method_name)), 'white', 'on_red'))
Exemplo n.º 35
0
 def display_final_report(self, ep_cnt, step_cnt, elapsed_time):
     fps = step_cnt / elapsed_time
     print(clr("[  %s   ] finished after %d eps, %d steps."
           % ("Main", ep_cnt, step_cnt), 'white', 'on_magenta'))
     print(clr("[  %s   ] finished after %.2fs, %.2ffps."
           % ("Main", elapsed_time, fps), 'white', 'on_magenta'))