Beispiel #1
0
 def run(self, *args, **kwargs):
     logger = logging.getLogger("pyro.infer.mcmc")
     progress_bar = ProgressBar(self.warmup_steps,
                                self.num_samples,
                                disable=self.disable_progbar)
     logger = initialize_logger(logger, "", progress_bar)
     hook_w_logging = _add_logging_hook(logger, progress_bar, self.hook)
     for sample in _gen_samples(self.kernel, self.warmup_steps,
                                self.num_samples, hook_w_logging, *args,
                                **kwargs):
         yield sample, 0  # sample, chain_id (default=0)
     progress_bar.close()
Beispiel #2
0
def logger_thread(log_queue,
                  warmup_steps,
                  num_samples,
                  num_chains,
                  disable_progbar=False):
    """
    Logging thread that asynchronously consumes logging events from `log_queue`,
    and handles them appropriately.
    """
    progress_bars = ProgressBar(warmup_steps,
                                num_samples,
                                disable=disable_progbar,
                                num_bars=num_chains)
    logger = logging.getLogger(__name__)
    logger.propagate = False
    logger.addHandler(TqdmHandler())
    num_samples = [0] * num_chains
    try:
        while True:
            try:
                record = log_queue.get(timeout=1)
            except queue.Empty:
                continue
            if record is None:
                break
            metadata, msg = record.getMessage().split("]", 1)
            _, msg_type, logger_id = metadata[1:].split()
            if msg_type == DIAGNOSTIC_MSG:
                pbar_pos = int(logger_id.split(":")[-1])
                num_samples[pbar_pos] += 1
                if num_samples[pbar_pos] == warmup_steps:
                    progress_bars.set_description(
                        "Sample [{}]".format(pbar_pos + 1), pos=pbar_pos)
                diagnostics = json.loads(msg, object_pairs_hook=OrderedDict)
                progress_bars.set_postfix(diagnostics,
                                          pos=pbar_pos,
                                          refresh=False)
                progress_bars.update(pos=pbar_pos)
            else:
                logger.handle(record)
    finally:
        progress_bars.close()
Beispiel #3
0
    def run(self, *args, **kwargs):
        logger = logging.getLogger("pyro.infer.mcmc")
        for i in range(self.num_chains):
            if self.initial_params is not None:
                initial_params = {
                    k: v[i]
                    for k, v in self.initial_params.items()
                }
                self.kernel.initial_params = initial_params

            progress_bar = ProgressBar(self.warmup_steps,
                                       self.num_samples,
                                       disable=self.disable_progbar)
            logger = initialize_logger(logger, "", progress_bar)
            hook_w_logging = _add_logging_hook(logger, progress_bar, self.hook)
            for sample in _gen_samples(self.kernel, self.warmup_steps,
                                       self.num_samples, hook_w_logging,
                                       i if self.num_chains > 1 else None,
                                       *args, **kwargs):
                yield sample, i  # sample, chain_id
            self.kernel.cleanup()
            progress_bar.close()