def loss_gradient(self, x: np.ndarray, y: np.ndarray, **kwargs) -> np.ndarray:
        """
        Compute the gradient of the loss function w.r.t. `x`.

        :param x: Sample input with shape as expected by the model.
        :param y: Target values (class labels) one-hot-encoded of shape `(nb_samples, nb_classes)` or indices of shape
                  `(nb_samples,)`.
        :return: Array of gradients of the same shape as `x`.
        :raises `ValueError`: If the model has not been fitted prior to calling this method.
        """

        from sklearn.utils.class_weight import compute_class_weight

        if not hasattr(self.model, "coef_"):  # pragma: no cover
            raise ValueError(
                """Model has not been fitted. Run function `fit(x, y)` of classifier first or provide a
            fitted model."""
            )

        # Apply preprocessing
        x_preprocessed, y_preprocessed = self._apply_preprocessing(x, y, fit=False)

        y_index = np.argmax(y_preprocessed, axis=1)
        if self.model.class_weight is None or self.model.class_weight == "balanced":
            class_weight = np.ones(self.nb_classes)
        else:
            class_weight = compute_class_weight(
                class_weight=self.model.class_weight,
                classes=self.model.classes_,
                y=y_index,
            )

        y_pred = self.predict(x=x_preprocessed)
        weights = self.model.coef_

        errors = class_weight * (y_pred - y)

        if weights.shape[0] == 1:
            weights = np.append(-weights, weights, axis=0)

        gradients = (errors @ weights) / self.model.classes_.size

        gradients = self._apply_preprocessing_gradient(x, gradients)

        return gradients

        Compute the gradient of the loss function w.r.t. `x`.

        :param x: Sample input with shape as expected by the model.
        :param y: Target values (class labels) one-hot-encoded of shape `(nb_samples, nb_classes)` or indices of shape
                  `(nb_samples,)`.
        :return: Array of gradients of the same shape as `x`.
        :raises `ValueError`: If the model has not been fitted prior to calling this method.
        
art/estimators/classification/scikitlearn.py