Пример #1
0
    def samples(self,
                model: Model,
                n_samples: int,
                n_burn: int,
                thin: float = 0.1,
                store_in_sample_predictions: bool = True,
                store_acceptance: bool = True) -> Chain:
        print("Starting burn")

        trace_logger = self.trace_logger_class()

        for _ in tqdm(range(n_burn)):
            self.step(model, trace_logger)
        trace = []
        model_trace = []
        acceptance_trace = []
        print("Starting sampling")

        thin_inverse = 1. / thin

        for ss in tqdm(range(n_samples)):
            step_trace_dict = self.step(model, trace_logger)
            if ss % thin_inverse == 0:
                if store_in_sample_predictions:
                    in_sample_log = trace_logger["In Sample Prediction"](
                        model.predict())
                    if in_sample_log is not None:
                        trace.append(in_sample_log)
                if store_acceptance:
                    acceptance_trace.append(step_trace_dict)
                model_log = trace_logger["Model"](model)
                if model_log is not None:
                    model_trace.append(model_log)
        return {
            "model": model_trace,
            "acceptance": acceptance_trace,
            "in_sample_predictions": trace
        }
Пример #2
0
    def samples(
        self,
        model: Model,
        n_samples: int,
        n_burn: int,
        thin: float = 0.1,
        store_in_sample_predictions: bool = True
    ) -> Tuple[List[Model], np.ndarray]:
        print("Starting burn")
        for _ in tqdm(range(n_burn)):
            self.step(model)
        trace = []
        model_trace = []
        print("Starting sampling")

        thin_inverse = 1. / thin

        for ss in tqdm(range(n_samples)):
            self.step(model)
            if ss % thin_inverse == 0:
                if store_in_sample_predictions:
                    trace.append(model.predict())
                model_trace.append(deep_copy_model(model))
        return model_trace, np.array(trace)