def fit(self, x: np.ndarray, y: np.ndarray) -> None:
        """
        Train the attack model.

        :param x: Input to training process. Includes all features used to train the original model.
        :param y: True labels of the features.
        """

        # Checks:
        if self.single_index_feature and self.attack_feature >= x.shape[1]:
            raise ValueError(
                "attack_feature must be a valid index to a feature in x")

        # get vector of attacked feature
        attacked_feature = x[:, self.attack_feature]
        if self.single_index_feature:
            y_one_hot = float_to_categorical(attacked_feature)
        else:
            y_one_hot = floats_to_one_hot(attacked_feature)
        y_ready = check_and_transform_label_format(
            y_one_hot, len(np.unique(attacked_feature)), return_one_hot=True)

        # create training set for attack model
        normalized_labels = y * self.prediction_normal_factor
        x_train = np.concatenate(
            (np.delete(x, self.attack_feature, 1), normalized_labels),
            axis=1).astype(np.float32)

        # train attack model
        self.attack_model.fit(x_train, y_ready)
示例#2
0
    def fit(self, x: np.ndarray) -> None:
        """
        Train the attack model.

        :param x: Input to training process. Includes all features used to train the original model.
        """

        # Checks:
        if isinstance(self.attack_feature,
                      int) and self.attack_feature >= x.shape[1]:
            raise ValueError(
                "attack_feature must be a valid index to a feature in x")

        # get vector of attacked feature
        y = x[:, self.attack_feature]
        self._values = get_feature_values(y,
                                          isinstance(self.attack_feature, int))
        if isinstance(self.attack_feature, int):
            y_one_hot = float_to_categorical(y)
        else:
            y_one_hot = floats_to_one_hot(y)
        y_ready = check_and_transform_label_format(y_one_hot,
                                                   len(np.unique(y)),
                                                   return_one_hot=True)
        if y_ready is None:
            raise ValueError("None value detected.")

        # create training set for attack model
        x_train = np.delete(x, self.attack_feature, 1).astype(np.float32)

        # train attack model
        self.attack_model.fit(x_train, y_ready)
    def fit(self, x: np.ndarray) -> None:
        """
        Train the attack model.

        :param x: Input to training process. Includes all features used to train the original model.
        """

        # Checks:
        if self.estimator.input_shape[0] != x.shape[1]:
            raise ValueError(
                "Shape of x does not match input_shape of classifier")
        if self.attack_feature >= x.shape[1]:
            raise ValueError(
                "attack_feature must be a valid index to a feature in x")

        # get model's predictions for x
        predictions = np.array([
            np.argmax(arr) for arr in self.estimator.predict(x)
        ]).reshape(-1, 1)

        # get vector of attacked feature
        y = x[:, self.attack_feature]
        y_one_hot = float_to_categorical(y)
        y_ready = check_and_transform_label_format(y_one_hot,
                                                   len(np.unique(y)),
                                                   return_one_hot=True)

        # create training set for attack model
        x_train = np.concatenate(
            (np.delete(x, self.attack_feature, 1), predictions),
            axis=1).astype(np.float32)

        # train attack model
        self.attack_model.fit(x_train, y_ready)
示例#4
0
    def fit(self, x: np.ndarray, y: Optional[np.ndarray] = None) -> None:
        """
        Train the attack model.

        :param x: Input to training process. Includes all features used to train the original model.
        :param y: True labels for x.
        """

        # Checks:
        if self.estimator.input_shape is not None:
            if self.estimator.input_shape[0] != x.shape[1]:
                raise ValueError(
                    "Shape of x does not match input_shape of model")
        if isinstance(self.attack_feature,
                      int) and self.attack_feature >= x.shape[1]:
            raise ValueError(
                "`attack_feature` must be a valid index to a feature in x")

        # get model's predictions for x
        if ClassifierMixin in type(self.estimator).__mro__:
            predictions = np.array([
                np.argmax(arr) for arr in self.estimator.predict(x)
            ]).reshape(-1, 1)
        else:  # Regression model
            if self.scale_range is not None:
                predictions = minmax_scale(self.estimator.predict(x).reshape(
                    -1, 1),
                                           feature_range=self.scale_range)
                if y is not None:
                    y = minmax_scale(y, feature_range=self.scale_range)
            else:
                predictions = self.estimator.predict(x).reshape(
                    -1, 1) * self.prediction_normal_factor
                if y is not None:
                    y = y * self.prediction_normal_factor

        # get vector of attacked feature
        y_attack = x[:, self.attack_feature]
        self._values = get_feature_values(y_attack,
                                          isinstance(self.attack_feature, int))
        if isinstance(self.attack_feature, int):
            y_one_hot = float_to_categorical(y_attack)
        else:
            y_one_hot = floats_to_one_hot(y_attack)
        y_attack_ready = check_and_transform_label_format(
            y_one_hot, len(np.unique(y_attack)), return_one_hot=True)

        # create training set for attack model
        x_train = np.concatenate(
            (np.delete(x, self.attack_feature, 1), predictions),
            axis=1).astype(np.float32)

        if y is not None:
            y = check_and_transform_label_format(y, return_one_hot=True)
            x_train = np.concatenate((x_train, y), axis=1)

        # train attack model
        self.attack_model.fit(x_train, y_attack_ready)
示例#5
0
    def fit(self, x: np.ndarray, y: np.ndarray) -> None:
        """
        Train the attack model.

        :param x: Input to training process. Includes all features used to train the original model.
        :param y: True labels of the features.
        """

        # Checks:
        if isinstance(self.attack_feature,
                      int) and self.attack_feature >= x.shape[1]:
            raise ValueError(
                "attack_feature must be a valid index to a feature in x")

        # get vector of attacked feature
        attacked_feature = x[:, self.attack_feature]
        self._values = get_feature_values(attacked_feature,
                                          isinstance(self.attack_feature, int))
        if isinstance(self.attack_feature, int):
            y_one_hot = float_to_categorical(attacked_feature)
        else:
            y_one_hot = floats_to_one_hot(attacked_feature)
        y_ready = check_and_transform_label_format(
            y_one_hot, len(np.unique(attacked_feature)), return_one_hot=True)
        if y_ready is None:
            raise ValueError("None value detected.")

        # create training set for attack model
        if self.scale_range is not None:
            normalized_labels = minmax_scale(y, feature_range=self.scale_range)
        else:
            normalized_labels = y * self.prediction_normal_factor
        normalized_labels = check_and_transform_label_format(
            normalized_labels, return_one_hot=True)
        x_train = np.concatenate(
            (np.delete(x, self.attack_feature, 1), normalized_labels),
            axis=1).astype(np.float32)

        # train attack model
        self.attack_model.fit(x_train, y_ready)