Esempio n. 1
0
    def _fit_single(self, model, X, y, masked_data=None):
        Validation.check_dataset(X, y)

        if len(X) != 0:
            # Pre-compute target outputs if none are passed.
            if masked_data is None:
                output_dim = Validation.get_output_dimension(y)
                masked_data = self.masking_operation.get_predictions_after_masking(self.explained_model, X, y,
                                                                                   batch_size=
                                                                                   self.model_builder.batch_size,
                                                                                   downsample_factors=
                                                                                   self.downsample_factors,
                                                                                   flatten=
                                                                                   self.flatten_for_explained_model)

                masked_data = TensorflowCXPlain._clean_output_dims(output_dim, masked_data)

            self.last_masked_data = masked_data

            if self.model_filepath is None:
                from tempfile import NamedTemporaryFile
                model_filepath = NamedTemporaryFile(delete=False).name
            else:
                model_filepath = self.model_filepath

            self.last_history = self.model_builder.fit(model, masked_data, y, model_filepath)
        return self
Esempio n. 2
0
    def _build_model(self, X, y):
        Validation.check_dataset(X, y)

        if Validation.is_variable_length(X):
            raise ValueError("Variable length inputs to CXPlain are currently not supported.")

        n, p = Validation.get_input_dimension(X)
        output_dim = Validation.get_output_dimension(y)

        if self.model is None:
            if self.num_models == 1:
                build_fun = self._build_single
            else:
                build_fun = self._build_ensemble

            self.model, self.prediction_model = build_fun(input_dim=p, output_dim=output_dim)
Esempio n. 3
0
    def score(self, X, y, sample_weight=None, masked_data=None):
        """
        Evaluates the performance, in terms of causal loss, of the current CXPlain model

        :param X: The data samples to be evaluated. The first dimension must be the number of samples. (Required)
        :param y: The ground truth labels to be compared to. The first dimension must be the number of
                  samples. (Required)
        :param sample_weight: The sample weight to apply to the samples in X during evaluation. The first dimension
                              must be the number of samples and it must match that of __X__ and __y__.
                              If None, equal weihting is used (Optional, default: None).
        :param masked_data: An array of precomputed masked data as can be obtained from __get_masked_data__.
                            If None, the masked data is computed. If set, the precomputed masked data is used for
                            scoring and computation of the masked data is skipped (Optional, default: None).
        :return: Score results as returned by self.model_builder.evaluate(model, X, y, sample_weight) either
                 (i) as a single score result if __num_models__ = 1 or as a list of score results
                 if __num_models__ is greater than 1.
        :exception AssertionError Thrown if the explanation model has not been fitted using __fit__ yet.
        """
        if self.model is None:
            raise AssertionError("Model must be initialised when calling __predict__. "
                                 "Did you forget to __fit__ the explanation model?")

        output_dim = Validation.get_output_dimension(y)
        if masked_data is None:
            masked_data = self.masking_operation.get_predictions_after_masking(self.explained_model, X, y,
                                                                               batch_size=
                                                                               self.model_builder.batch_size,
                                                                               downsample_factors=
                                                                               self.downsample_factors,
                                                                               flatten=
                                                                               self.flatten_for_explained_model)
            masked_data = TensorflowCXPlain._clean_output_dims(output_dim, masked_data)

        self.last_masked_data = masked_data

        if self.num_models == 1:
            return_value = self._score_single(self.model, masked_data, y, sample_weight)
        else:
            return_value = [self._score_single(model, masked_data, y, sample_weight) for model in self.model]
        return return_value