gurudesh/pytracebugs huggingface dataset:
# Buggy code
def __init__(
    self,
    classifier,
    attacker="deepfool",
    attacker_params=None,
    delta=0.2,
    max_iter=20,
    eps=10.0,
    norm=np.inf,
):
    """
    :param classifier: A trained model.
    :type classifier: :class:`Classifier`
    :param attacker: Adversarial attack name. Default is 'deepfool'. Supported names: 'carlini', 'deepfool', 'fgsm',
            'newtonfool', 'jsma', 'vat'.
    :type attacker: `str`
    :param attacker_params: Parameters specific to the adversarial attack.
    :type attacker_params: `dict`
    :param delta: desired accuracy
    :type delta: `float`
    :param max_iter: The maximum number of iterations for computing universal perturbation.
    :type max_iter: `int`
    :param eps: Attack step size (input variation)
    :type eps: `float`
    :param norm: Order of the norm. Possible values: np.inf, 2 (default is np.inf)
    :type norm: `int`
    """
    super(UniversalPerturbation, self).__init__(classifier)
    kwargs = {
        "attacker": attacker,
        "attacker_params": attacker_params,
        "delta": delta,
        "max_iter": max_iter,
        "eps": eps,
        "norm": norm,
    }
    self.set_params(**kwargs)


# Traceback (IndexError)
Traceback (most recent call last):
File "cw_pytorch.py", line 172, in <module>
x_test_adv = cl2m.generate(inputs, **params)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/art/attacks/carlini.py", line 380, in generate
x_adv_batch_tanh[active_and_update_adv] = x_adv_batch_tanh[update_adv] + \
IndexError: boolean index did not match indexed array along dimension 0; dimension is 18 but corresponding boolean dimension is 17

# Fixed code
def __init__(
    self,
    classifier,
    attacker="deepfool",
    attacker_params=None,
    delta=0.2,
    max_iter=20,
    eps=10.0,
    norm=np.inf,
    expectation=None,
):
    """
    :param classifier: A trained model.
    :type classifier: :class:`Classifier`
    :param attacker: Adversarial attack name. Default is 'deepfool'. Supported names: 'carlini', 'carlini_inf',
                     'deepfool', 'fgsm', 'bim', 'pgd', 'margin', 'ead', 'newtonfool', 'jsma', 'vat'.
    :type attacker: `str`
    :param attacker_params: Parameters specific to the adversarial attack.
    :type attacker_params: `dict`
    :param delta: desired accuracy
    :type delta: `float`
    :param max_iter: The maximum number of iterations for computing universal perturbation.
    :type max_iter: `int`
    :param eps: Attack step size (input variation)
    :type eps: `float`
    :param norm: Order of the norm. Possible values: np.inf, 2 (default is np.inf)
    :type norm: `int`
    :param expectation: An expectation over transformations to be applied when computing
                        classifier gradients and predictions.
    :type expectation: :class:`ExpectationOverTransformations`
    """
    super(UniversalPerturbation, self).__init__(classifier)
    kwargs = {
        "attacker": attacker,
        "attacker_params": attacker_params,
        "delta": delta,
        "max_iter": max_iter,
        "eps": eps,
        "norm": norm,
        "expectation": expectation,
    }
    self.set_params(**kwargs)
# Buggy code
def __init__(self, classifier, max_iter=1, finite_diff=1e-6, eps=0.1):
    """
    Create a VirtualAdversarialMethod instance.

    :param classifier: A trained model.
    :type classifier: :class:`Classifier`
    :param eps: Attack step (max input variation).
    :type eps: `float`
    :param finite_diff: The finite difference parameter.
    :type finite_diff: `float`
    :param max_iter: The maximum number of iterations.
    :type max_iter: `int`
    """
    super(VirtualAdversarialMethod, self).__init__(classifier)

    kwargs = {"finite_diff": finite_diff, "eps": eps, "max_iter": max_iter}
    self.set_params(**kwargs)


# Traceback (IndexError)
Traceback (most recent call last):
File "cw_pytorch.py", line 172, in <module>
x_test_adv = cl2m.generate(inputs, **params)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/art/attacks/carlini.py", line 380, in generate
x_adv_batch_tanh[active_and_update_adv] = x_adv_batch_tanh[update_adv] + \
IndexError: boolean index did not match indexed array along dimension 0; dimension is 18 but corresponding boolean dimension is 17

# Fixed code
def __init__(
    self,
    classifier,
    max_iter=1,
    finite_diff=1e-6,
    eps=0.1,
    batch_size=128,
    expectation=None,
):
    """
    Create a VirtualAdversarialMethod instance.

    :param classifier: A trained model.
    :type classifier: :class:`Classifier`
    :param eps: Attack step (max input variation).
    :type eps: `float`
    :param finite_diff: The finite difference parameter.
    :type finite_diff: `float`
    :param max_iter: The maximum number of iterations.
    :type max_iter: `int`
    :param batch_size: Batch size
    :type batch_size: `int`
    :param expectation: An expectation over transformations to be applied when computing
                        classifier gradients and predictions.
    :type expectation: :class:`ExpectationOverTransformations`
    """
    super(VirtualAdversarialMethod, self).__init__(classifier)
    kwargs = {
        "finite_diff": finite_diff,
        "eps": eps,
        "max_iter": max_iter,
        "batch_size": batch_size,
        "expectation": expectation,
    }
    self.set_params(**kwargs)
# Buggy code
def _normalize(x):
    """
    Apply L_2 batch normalization on `x`.

    :param x: The input array to normalize.
    :type x: `np.ndarray`
    :return: The normalized version of `x`.
    :rtype: `np.ndarray`
    """
    tol = 1e-10
    dims = x.shape

    x = x.flatten()
    inverse = (np.sum(x**2) + tol) ** -0.5
    x = x * inverse
    x = np.reshape(x, dims)

    return x


# Traceback (IndexError)
Traceback (most recent call last):
File "cw_pytorch.py", line 172, in <module>
x_test_adv = cl2m.generate(inputs, **params)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/art/attacks/carlini.py", line 380, in generate
x_adv_batch_tanh[active_and_update_adv] = x_adv_batch_tanh[update_adv] + \
IndexError: boolean index did not match indexed array along dimension 0; dimension is 18 but corresponding boolean dimension is 17

# Fixed code
def _normalize(x):
    """
    Apply L_2 batch normalization on `x`.

    :param x: The input array batch to normalize.
    :type x: `np.ndarray`
    :return: The normalized version of `x`.
    :rtype: `np.ndarray`
    """
    tol = 1e-10
    dims = x.shape

    x = x.reshape(dims[0], -1)
    inverse = (np.sum(x**2, axis=1) + tol) ** -0.5
    x = x * inverse[:, None]
    x = np.reshape(x, dims)

    return x
# Buggy code
def set_params(self, **kwargs):
    """
    Take in a dictionary of parameters and applies attack-specific checks before saving them as attributes.

    :param eps: Attack step (max input variation).
    :type eps: `float`
    :param finite_diff: The finite difference parameter.
    :type finite_diff: `float`
    :param max_iter: The maximum number of iterations.
    :type max_iter: `int`
    """
    # Save attack-specific parameters
    super(VirtualAdversarialMethod, self).set_params(**kwargs)

    return True


# Traceback (IndexError)
Traceback (most recent call last):
File "cw_pytorch.py", line 172, in <module>
x_test_adv = cl2m.generate(inputs, **params)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/art/attacks/carlini.py", line 380, in generate
x_adv_batch_tanh[active_and_update_adv] = x_adv_batch_tanh[update_adv] + \
IndexError: boolean index did not match indexed array along dimension 0; dimension is 18 but corresponding boolean dimension is 17

# Fixed code
def set_params(self, **kwargs):
    """
    Take in a dictionary of parameters and applies attack-specific checks before saving them as attributes.

    :param eps: Attack step (max input variation).
    :type eps: `float`
    :param finite_diff: The finite difference parameter.
    :type finite_diff: `float`
    :param max_iter: The maximum number of iterations.
    :type max_iter: `int`
    :param batch_size: Internal size of batches on which adversarial samples are generated.
    :type batch_size: `int`
    """
    # Save attack-specific parameters
    super(VirtualAdversarialMethod, self).set_params(**kwargs)

    if not isinstance(self.max_iter, (int, np.int)) or self.max_iter <= 0:
        raise ValueError("The number of iterations must be a positive integer.")

    if self.eps <= 0:
        raise ValueError("The attack step must be positive.")

    if not isinstance(self.finite_diff, float) or self.finite_diff <= 0:
        raise ValueError("The finite difference parameter must be a positive float.")

    if self.batch_size <= 0:
        raise ValueError("The batch size `batch_size` has to be positive.")

    return True
# Buggy code
def predict(self, x, logits=False, batch_size=128):
    """
    Perform prediction for a batch of inputs.

    :param x: Test set.
    :type x: `np.ndarray`
    :param logits: `True` if the prediction should be done at the logits layer.
    :type logits: `bool`
    :param batch_size: Size of batches.
    :type batch_size: `int`
    :return: Array of predictions of shape `(nb_inputs, self.nb_classes)`.
    :rtype: `np.ndarray`
    """
    import keras.backend as k

    k.set_learning_phase(0)

    # Apply defences
    x_ = self._apply_processing(x)
    x_ = self._apply_defences_predict(x_)

    # Run predictions with batching
    preds = np.zeros((x_.shape[0], self.nb_classes), dtype=np.float32)
    for b in range(int(np.ceil(x_.shape[0] / float(batch_size)))):
        begin, end = b * batch_size, min((b + 1) * batch_size, x_.shape[0])
        preds[begin:end] = self._preds([x_[begin:end]])[0]

        if not logits and not self._custom_activation:
            exp = np.exp(
                preds[begin:end] - np.max(preds[begin:end], axis=1, keepdims=True)
            )
            preds[begin:end] = exp / np.sum(exp, axis=1, keepdims=True)

    return preds


# Traceback (IndexError)
Traceback (most recent call last):
File "cw_pytorch.py", line 172, in <module>
x_test_adv = cl2m.generate(inputs, **params)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/art/attacks/carlini.py", line 380, in generate
x_adv_batch_tanh[active_and_update_adv] = x_adv_batch_tanh[update_adv] + \
IndexError: boolean index did not match indexed array along dimension 0; dimension is 18 but corresponding boolean dimension is 17

# Fixed code
def predict(self, x, logits=False, batch_size=128):
    """
    Perform prediction for a batch of inputs.

    :param x: Test set.
    :type x: `np.ndarray`
    :param logits: `True` if the prediction should be done at the logits layer.
    :type logits: `bool`
    :param batch_size: Size of batches.
    :type batch_size: `int`
    :return: Array of predictions of shape `(nb_inputs, self.nb_classes)`.
    :rtype: `np.ndarray`
    """
    # Apply defences
    x_ = self._apply_processing(x)
    x_ = self._apply_defences_predict(x_)

    # Run predictions with batching
    preds = np.zeros((x_.shape[0], self.nb_classes), dtype=np.float32)
    for b in range(int(np.ceil(x_.shape[0] / float(batch_size)))):
        begin, end = b * batch_size, min((b + 1) * batch_size, x_.shape[0])
        preds[begin:end] = self._preds([x_[begin:end]])[0]

        if not logits and not self._custom_activation:
            exp = np.exp(
                preds[begin:end] - np.max(preds[begin:end], axis=1, keepdims=True)
            )
            preds[begin:end] = exp / np.sum(exp, axis=1, keepdims=True)

    return preds
# Buggy code
def fit(self, x, y, batch_size=128, nb_epochs=20):
    """
    Fit the classifier on the training set `(x, y)`.

    :param x: Training data.
    :type x: `np.ndarray`
    :param y: Labels, one-vs-rest encoding.
    :type y: `np.ndarray`
    :param batch_size: Size of batches.
    :type batch_size: `int`
    :param nb_epochs: Number of epochs to use for trainings.
    :type nb_epochs: `int`
    :return: `None`
    """
    import keras.backend as k

    k.set_learning_phase(1)

    # Apply preprocessing and defences
    x_ = self._apply_processing(x)
    x_, y_ = self._apply_defences_fit(x_, y)

    gen = generator_fit(x_, y_, batch_size)
    self._model.fit_generator(
        gen, steps_per_epoch=x_.shape[0] / batch_size, epochs=nb_epochs
    )


# Traceback (IndexError)
Traceback (most recent call last):
File "cw_pytorch.py", line 172, in <module>
x_test_adv = cl2m.generate(inputs, **params)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/art/attacks/carlini.py", line 380, in generate
x_adv_batch_tanh[active_and_update_adv] = x_adv_batch_tanh[update_adv] + \
IndexError: boolean index did not match indexed array along dimension 0; dimension is 18 but corresponding boolean dimension is 17

# Fixed code
def fit(self, x, y, batch_size=128, nb_epochs=20):
    """
    Fit the classifier on the training set `(x, y)`.

    :param x: Training data.
    :type x: `np.ndarray`
    :param y: Labels, one-vs-rest encoding.
    :type y: `np.ndarray`
    :param batch_size: Size of batches.
    :type batch_size: `int`
    :param nb_epochs: Number of epochs to use for trainings.
    :type nb_epochs: `int`
    :return: `None`
    """
    # Apply preprocessing and defences
    x_ = self._apply_processing(x)
    x_, y_ = self._apply_defences_fit(x_, y)

    gen = generator_fit(x_, y_, batch_size)
    self._model.fit_generator(
        gen, steps_per_epoch=x_.shape[0] / batch_size, epochs=nb_epochs
    )
# Buggy code
def fit_generator(self, generator, nb_epochs=20):
    """
    Fit the classifier using the generator that yields batches as specified.

    :param generator: Batch generator providing `(x, y)` for each epoch. If the generator can be used for native
                      training in Keras, it will.
    :type generator: `DataGenerator`
    :param nb_epochs: Number of epochs to use for trainings.
    :type nb_epochs: `int`
    :return: `None`
    """
    import keras.backend as k
    from art.data_generators import KerasDataGenerator

    k.set_learning_phase(1)

    # Try to use the generator as a Keras native generator, otherwise use it through the `DataGenerator` interface
    # TODO Testing for preprocessing defenses is currently hardcoded; this should be improved (add property)
    if isinstance(generator, KerasDataGenerator) and not (
        hasattr(self, "label_smooth") or hasattr(self, "feature_squeeze")
    ):
        try:
            self._model.fit_generator(generator.generator, epochs=nb_epochs)
        except ValueError:
            logger.info(
                "Unable to use data generator as Keras generator. Now treating as framework-independent."
            )
            super(KerasClassifier, self).fit_generator(generator, nb_epochs=nb_epochs)
    else:
        super(KerasClassifier, self).fit_generator(generator, nb_epochs=nb_epochs)


# Traceback (IndexError)
Traceback (most recent call last):
File "cw_pytorch.py", line 172, in <module>
x_test_adv = cl2m.generate(inputs, **params)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/art/attacks/carlini.py", line 380, in generate
x_adv_batch_tanh[active_and_update_adv] = x_adv_batch_tanh[update_adv] + \
IndexError: boolean index did not match indexed array along dimension 0; dimension is 18 but corresponding boolean dimension is 17

# Fixed code
def fit_generator(self, generator, nb_epochs=20):
    """
    Fit the classifier using the generator that yields batches as specified.

    :param generator: Batch generator providing `(x, y)` for each epoch. If the generator can be used for native
                      training in Keras, it will.
    :type generator: `DataGenerator`
    :param nb_epochs: Number of epochs to use for trainings.
    :type nb_epochs: `int`
    :return: `None`
    """
    from art.data_generators import KerasDataGenerator

    # Try to use the generator as a Keras native generator, otherwise use it through the `DataGenerator` interface
    # TODO Testing for preprocessing defenses is currently hardcoded; this should be improved (add property)
    if isinstance(generator, KerasDataGenerator) and not (
        hasattr(self, "label_smooth") or hasattr(self, "feature_squeeze")
    ):
        try:
            self._model.fit_generator(generator.generator, epochs=nb_epochs)
        except ValueError:
            logger.info(
                "Unable to use data generator as Keras generator. Now treating as framework-independent."
            )
            super(KerasClassifier, self).fit_generator(generator, nb_epochs=nb_epochs)
    else:
        super(KerasClassifier, self).fit_generator(generator, nb_epochs=nb_epochs)
# Buggy code
def predict(self, x, logits=False, batch_size=128):
    """
    Perform prediction for a batch of inputs.

    :param x: Test set.
    :type x: `np.ndarray`
    :param logits: `True` if the prediction should be done at the logits layer.
    :type logits: `bool`
    :param batch_size: Size of batches.
    :type batch_size: `int`
    :return: Array of predictions of shape `(nb_inputs, self.nb_classes)`.
    :rtype: `np.ndarray`
    """
    from mxnet import autograd, nd

    # Apply preprocessing and defences
    x_ = self._apply_processing(x)
    x_ = self._apply_defences_predict(x_)

    # Run prediction with batch processing
    results = np.zeros((x_.shape[0], self.nb_classes), dtype=np.float32)
    num_batch = int(np.ceil(len(x_) / float(batch_size)))
    for m in range(num_batch):
        # Batch indexes
        begin, end = m * batch_size, min((m + 1) * batch_size, x_.shape[0])

        # Predict
        x_batch = nd.array(x_[begin:end], ctx=self._ctx)
        x_batch.attach_grad()
        with autograd.record(train_mode=False):
            preds = self._model(x_batch)

        if logits is False:
            preds = preds.softmax()

        results[begin:end] = preds.asnumpy()

    return results


# Traceback (IndexError)
Traceback (most recent call last):
File "cw_pytorch.py", line 172, in <module>
x_test_adv = cl2m.generate(inputs, **params)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/art/attacks/carlini.py", line 380, in generate
x_adv_batch_tanh[active_and_update_adv] = x_adv_batch_tanh[update_adv] + \
IndexError: boolean index did not match indexed array along dimension 0; dimension is 18 but corresponding boolean dimension is 17

# Fixed code
def predict(self, x, logits=False, batch_size=128):
    """
    Perform prediction for a batch of inputs.

    :param x: Test set.
    :type x: `np.ndarray`
    :param logits: `True` if the prediction should be done at the logits layer.
    :type logits: `bool`
    :param batch_size: Size of batches.
    :type batch_size: `int`
    :return: Array of predictions of shape `(nb_inputs, self.nb_classes)`.
    :rtype: `np.ndarray`
    """
    from mxnet import autograd, nd

    train_mode = self._learning_phase if hasattr(self, "_learning_phase") else False

    # Apply preprocessing and defences
    x_ = self._apply_processing(x)
    x_ = self._apply_defences_predict(x_)

    # Run prediction with batch processing
    results = np.zeros((x_.shape[0], self.nb_classes), dtype=np.float32)
    num_batch = int(np.ceil(len(x_) / float(batch_size)))
    for m in range(num_batch):
        # Batch indexes
        begin, end = m * batch_size, min((m + 1) * batch_size, x_.shape[0])

        # Predict
        x_batch = nd.array(x_[begin:end], ctx=self._ctx)
        x_batch.attach_grad()
        with autograd.record(train_mode=train_mode):
            preds = self._model(x_batch)

        if logits is False:
            preds = preds.softmax()

        results[begin:end] = preds.asnumpy()

    return results
# Buggy code
def loss_gradient(self, x, y):
    """
    Compute the gradient of the loss function w.r.t. `x`.

    :param x: Sample input with shape as expected by the model.
    :type x: `np.ndarray`
    :param y: Correct labels, one-vs-rest encoding.
    :type y: `np.ndarray`
    :return: Array of gradients of the same shape as `x`.
    :rtype: `np.ndarray`
    """
    from mxnet import autograd, gluon, nd

    x_ = nd.array(x, ctx=self._ctx)
    y_ = nd.array([np.argmax(y, axis=1)]).T

    x_.attach_grad()
    loss = gluon.loss.SoftmaxCrossEntropyLoss()
    with autograd.record(train_mode=False):
        preds = self._model(x_)
        loss = loss(preds, y_)

    loss.backward()
    grads = x_.grad.asnumpy()
    grads = self._apply_processing_gradient(grads)
    assert grads.shape == x.shape

    return grads


# Traceback (IndexError)
Traceback (most recent call last):
File "cw_pytorch.py", line 172, in <module>
x_test_adv = cl2m.generate(inputs, **params)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/art/attacks/carlini.py", line 380, in generate
x_adv_batch_tanh[active_and_update_adv] = x_adv_batch_tanh[update_adv] + \
IndexError: boolean index did not match indexed array along dimension 0; dimension is 18 but corresponding boolean dimension is 17

# Fixed code
def loss_gradient(self, x, y):
    """
    Compute the gradient of the loss function w.r.t. `x`.

    :param x: Sample input with shape as expected by the model.
    :type x: `np.ndarray`
    :param y: Correct labels, one-vs-rest encoding.
    :type y: `np.ndarray`
    :return: Array of gradients of the same shape as `x`.
    :rtype: `np.ndarray`
    """
    from mxnet import autograd, gluon, nd

    train_mode = self._learning_phase if hasattr(self, "_learning_phase") else False

    x_ = nd.array(x, ctx=self._ctx)
    y_ = nd.array([np.argmax(y, axis=1)]).T

    x_.attach_grad()
    loss = gluon.loss.SoftmaxCrossEntropyLoss()
    with autograd.record(train_mode=train_mode):
        preds = self._model(x_)
        loss = loss(preds, y_)

    loss.backward()
    grads = x_.grad.asnumpy()
    grads = self._apply_processing_gradient(grads)
    assert grads.shape == x.shape

    return grads
# Buggy code
def get_activations(self, x, layer):
    """
    Return the output of the specified layer for input `x`. `layer` is specified by layer index (between 0 and
    `nb_layers - 1`) or by name. The number of layers can be determined by counting the results returned by
    calling `layer_names`.

    :param x: Input for computing the activations.
    :type x: `np.ndarray`
    :param layer: Layer for computing the activations
    :type layer: `int` or `str`
    :return: The output of `layer`, where the first dimension is the batch size corresponding to `x`.
    :rtype: `np.ndarray`
    """
    import torch

    # Apply defences
    x = self._apply_defences_predict(x)

    # Set test phase
    self._model.train(False)

    # Run prediction
    model_outputs = self._model(torch.from_numpy(x).to(self._device).float())[:-1]

    if isinstance(layer, six.string_types):
        if layer not in self._layer_names:
            raise ValueError("Layer name %s not supported" % layer)
        layer_index = self._layer_names.index(layer)

    elif isinstance(layer, (int, np.integer)):
        layer_index = layer

    else:
        raise TypeError("Layer must be of type str or int")

    return model_outputs[layer_index].detach().cpu().numpy()


# Traceback (IndexError)
Traceback (most recent call last):
File "cw_pytorch.py", line 172, in <module>
x_test_adv = cl2m.generate(inputs, **params)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/art/attacks/carlini.py", line 380, in generate
x_adv_batch_tanh[active_and_update_adv] = x_adv_batch_tanh[update_adv] + \
IndexError: boolean index did not match indexed array along dimension 0; dimension is 18 but corresponding boolean dimension is 17

# Fixed code
def get_activations(self, x, layer):
    """
    Return the output of the specified layer for input `x`. `layer` is specified by layer index (between 0 and
    `nb_layers - 1`) or by name. The number of layers can be determined by counting the results returned by
    calling `layer_names`.

    :param x: Input for computing the activations.
    :type x: `np.ndarray`
    :param layer: Layer for computing the activations
    :type layer: `int` or `str`
    :return: The output of `layer`, where the first dimension is the batch size corresponding to `x`.
    :rtype: `np.ndarray`
    """
    import torch

    # Apply defences
    x = self._apply_defences_predict(x)

    # Run prediction
    model_outputs = self._model(torch.from_numpy(x).to(self._device).float())[:-1]

    if isinstance(layer, six.string_types):
        if layer not in self._layer_names:
            raise ValueError("Layer name %s not supported" % layer)
        layer_index = self._layer_names.index(layer)

    elif isinstance(layer, (int, np.integer)):
        layer_index = layer

    else:
        raise TypeError("Layer must be of type str or int")

    return model_outputs[layer_index].detach().cpu().numpy()
# Buggy code
def predict(self, x, logits=False, batch_size=128):
    """
    Perform prediction for a batch of inputs.

    :param x: Test set.
    :type x: `np.ndarray`
    :param logits: `True` if the prediction should be done at the logits layer.
    :type logits: `bool`
    :param batch_size: Size of batches.
    :type batch_size: `int`
    :return: Array of predictions of shape `(nb_inputs, self.nb_classes)`.
    :rtype: `np.ndarray`
    """
    # Apply defences
    x_ = self._apply_processing(x)
    x_ = self._apply_defences_predict(x_)

    # Run prediction with batch processing
    results = np.zeros((x_.shape[0], self.nb_classes), dtype=np.float32)
    num_batch = int(np.ceil(len(x_) / float(batch_size)))
    for m in range(num_batch):
        # Batch indexes
        begin, end = m * batch_size, min((m + 1) * batch_size, x_.shape[0])

        # Create feed_dict
        fd = {self._input_ph: x_[begin:end]}
        if self._learning is not None:
            fd[self._learning] = False

        # Run prediction
        if logits:
            results[begin:end] = self._sess.run(self._logits, feed_dict=fd)
        else:
            results[begin:end] = self._sess.run(self._probs, feed_dict=fd)

    return results


# Traceback (IndexError)
Traceback (most recent call last):
File "cw_pytorch.py", line 172, in <module>
x_test_adv = cl2m.generate(inputs, **params)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/art/attacks/carlini.py", line 380, in generate
x_adv_batch_tanh[active_and_update_adv] = x_adv_batch_tanh[update_adv] + \
IndexError: boolean index did not match indexed array along dimension 0; dimension is 18 but corresponding boolean dimension is 17

# Fixed code
def predict(self, x, logits=False, batch_size=128):
    """
    Perform prediction for a batch of inputs.

    :param x: Test set.
    :type x: `np.ndarray`
    :param logits: `True` if the prediction should be done at the logits layer.
    :type logits: `bool`
    :param batch_size: Size of batches.
    :type batch_size: `int`
    :return: Array of predictions of shape `(nb_inputs, self.nb_classes)`.
    :rtype: `np.ndarray`
    """
    # Apply defences
    x_ = self._apply_processing(x)
    x_ = self._apply_defences_predict(x_)

    # Run prediction with batch processing
    results = np.zeros((x_.shape[0], self.nb_classes), dtype=np.float32)
    num_batch = int(np.ceil(len(x_) / float(batch_size)))
    for m in range(num_batch):
        # Batch indexes
        begin, end = m * batch_size, min((m + 1) * batch_size, x_.shape[0])

        # Create feed_dict
        fd = {self._input_ph: x_[begin:end]}
        fd.update(self._feed_dict)

        # Run prediction
        if logits:
            results[begin:end] = self._sess.run(self._logits, feed_dict=fd)
        else:
            results[begin:end] = self._sess.run(self._probs, feed_dict=fd)

    return results
# Buggy code
def fit_generator(self, generator, nb_epochs=20):
    """
    Fit the classifier using the generator that yields batches as specified.

    :param generator: Batch generator providing `(x, y)` for each epoch. If the generator can be used for native
                      training in TensorFlow, it will.
    :type generator: `DataGenerator`
    :param nb_epochs: Number of epochs to use for trainings.
    :type nb_epochs: `int`
    :return: `None`
    """
    # TODO Implement TF-specific version
    super(TFClassifier, self).fit_generator(generator, nb_epochs=nb_epochs)


# Traceback (IndexError)
Traceback (most recent call last):
File "cw_pytorch.py", line 172, in <module>
x_test_adv = cl2m.generate(inputs, **params)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/art/attacks/carlini.py", line 380, in generate
x_adv_batch_tanh[active_and_update_adv] = x_adv_batch_tanh[update_adv] + \
IndexError: boolean index did not match indexed array along dimension 0; dimension is 18 but corresponding boolean dimension is 17

# Fixed code
def fit_generator(self, generator, nb_epochs=20):
    """
    Fit the classifier using the generator that yields batches as specified.

    :param generator: Batch generator providing `(x, y)` for each epoch. If the generator can be used for native
                      training in TensorFlow, it will.
    :type generator: `DataGenerator`
    :param nb_epochs: Number of epochs to use for trainings.
    :type nb_epochs: `int`
    :return: `None`
    """
    from art.data_generators import TFDataGenerator

    # Train directly in Tensorflow
    if isinstance(generator, TFDataGenerator) and not (
        hasattr(self, "label_smooth") or hasattr(self, "feature_squeeze")
    ):
        for _ in range(nb_epochs):
            for _ in range(int(generator.size / generator.batch_size)):
                i_batch, o_batch = generator.get_batch()

                # Create feed_dict
                fd = {self._input_ph: i_batch, self._output_ph: o_batch}
                fd.update(self._feed_dict)

                # Run train step
                self._sess.run(self._train, feed_dict=fd)
    else:
        super(TFClassifier, self).fit_generator(generator, nb_epochs=nb_epochs)
# Buggy code
def loss_gradient(self, x, y):
    """
    Compute the gradient of the loss function w.r.t. `x`.

    :param x: Sample input with shape as expected by the model.
    :type x: `np.ndarray`
    :param y: Correct labels, one-vs-rest encoding.
    :type y: `np.ndarray`
    :return: Array of gradients of the same shape as `x`.
    :rtype: `np.ndarray`
    """
    x_ = self._apply_processing(x)

    # Check if loss available
    if (
        not hasattr(self, "_loss_grads")
        or self._loss_grads is None
        or self._output_ph is None
    ):
        raise ValueError(
            "Need the loss function and the labels placeholder to compute the loss gradient."
        )

    # Compute the gradient and return
    grds = self._sess.run(
        self._loss_grads, feed_dict={self._input_ph: x_, self._output_ph: y}
    )
    grds = self._apply_processing_gradient(grds)
    assert grds.shape == x_.shape

    return grds


# Traceback (IndexError)
Traceback (most recent call last):
File "cw_pytorch.py", line 172, in <module>
x_test_adv = cl2m.generate(inputs, **params)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/art/attacks/carlini.py", line 380, in generate
x_adv_batch_tanh[active_and_update_adv] = x_adv_batch_tanh[update_adv] + \
IndexError: boolean index did not match indexed array along dimension 0; dimension is 18 but corresponding boolean dimension is 17

# Fixed code
def loss_gradient(self, x, y):
    """
    Compute the gradient of the loss function w.r.t. `x`.

    :param x: Sample input with shape as expected by the model.
    :type x: `np.ndarray`
    :param y: Correct labels, one-vs-rest encoding.
    :type y: `np.ndarray`
    :return: Array of gradients of the same shape as `x`.
    :rtype: `np.ndarray`
    """
    x_ = self._apply_processing(x)

    # Check if loss available
    if (
        not hasattr(self, "_loss_grads")
        or self._loss_grads is None
        or self._output_ph is None
    ):
        raise ValueError(
            "Need the loss function and the labels placeholder to compute the loss gradient."
        )

    # Create feed_dict
    fd = {self._input_ph: x_, self._output_ph: y}
    fd.update(self._feed_dict)

    # Compute the gradient and return
    grds = self._sess.run(self._loss_grads, feed_dict=fd)
    grds = self._apply_processing_gradient(grds)
    assert grds.shape == x_.shape

    return grds
# Buggy code
def __init__(self, generator, size, batch_size):
    """
    Create a Keras data generator wrapper instance.

    :param generator: A generator as specified by Keras documentation. Its output must be a tuple of either
                      `(inputs, targets)` or `(inputs, targets, sample_weights)`. All arrays in this tuple must have
                      the same length. The generator is expected to loop over its data indefinitely.
    :type generator: generator function or `keras.utils.Sequence` or `keras.preprocessing.image.ImageDataGenerator`
    :param size: Total size of the dataset.
    :type size: `int` or `None`
    :param batch_size: Size of the minibatches.
    :type batch_size: `int`
    """
    self.generator = generator

    if size is not None and (type(size) is not int or size < 1):
        raise ValueError(
            "The total size of the dataset must be an integer greater than zero."
        )

    self.size = size

    if type(batch_size) is not int or batch_size < 1:
        raise ValueError("The batch size must be an integer greater than zero.")

    self.batch_size = batch_size


# Traceback (IndexError)
Traceback (most recent call last):
File "cw_pytorch.py", line 172, in <module>
x_test_adv = cl2m.generate(inputs, **params)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/art/attacks/carlini.py", line 380, in generate
x_adv_batch_tanh[active_and_update_adv] = x_adv_batch_tanh[update_adv] + \
IndexError: boolean index did not match indexed array along dimension 0; dimension is 18 but corresponding boolean dimension is 17

# Fixed code
def __init__(self, generator, size, batch_size):
    """
    Create a Keras data generator wrapper instance.
    :param generator: A generator as specified by Keras documentation. Its output must be a tuple of either
                      `(inputs, targets)` or `(inputs, targets, sample_weights)`. All arrays in this tuple must have
                      the same length. The generator is expected to loop over its data indefinitely.
    :type generator: generator function or `keras.utils.Sequence` or `keras.preprocessing.image.ImageDataGenerator`
    :param size: Total size of the dataset.
    :type size: `int` or `None`
    :param batch_size: Size of the minibatches.
    :type batch_size: `int`
    """
    super(KerasDataGenerator, self).__init__(size=size, batch_size=batch_size)
    self.generator = generator
# Buggy code
def __init__(self, data_loader, size, batch_size):
    """
    Create a data generator wrapper on top of a PyTorch :class:`DataLoader`.

    :param data_loader: A PyTorch data generator.
    :type data_loader: `torch.utils.data.DataLoader`
    :param size: Total size of the dataset.
    :type size: int
    :param batch_size: Size of the minibatches.
    :type batch_size: int
    """
    from torch.utils.data import DataLoader

    if not isinstance(data_loader, DataLoader):
        raise TypeError(
            "Expected instance of PyTorch `DataLoader, received %s instead.`"
            % str(type(data_loader))
        )

    self.data_loader = data_loader

    if size is not None and (type(size) is not int or size < 1):
        raise ValueError(
            "The total size of the dataset must be an integer greater than zero."
        )

    self.size = size

    if type(batch_size) is not int or batch_size < 1:
        raise ValueError("The batch size must be an integer greater than zero.")

    self.batch_size = batch_size


# Traceback (IndexError)
Traceback (most recent call last):
File "cw_pytorch.py", line 172, in <module>
x_test_adv = cl2m.generate(inputs, **params)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/art/attacks/carlini.py", line 380, in generate
x_adv_batch_tanh[active_and_update_adv] = x_adv_batch_tanh[update_adv] + \
IndexError: boolean index did not match indexed array along dimension 0; dimension is 18 but corresponding boolean dimension is 17

# Fixed code
def __init__(self, data_loader, size, batch_size):
    """
    Create a data generator wrapper on top of a PyTorch :class:`DataLoader`.
    :param data_loader: A PyTorch data generator.
    :type data_loader: `torch.utils.data.DataLoader`
    :param size: Total size of the dataset.
    :type size: int
    :param batch_size: Size of the minibatches.
    :type batch_size: int
    """
    super(PyTorchDataGenerator, self).__init__(size=size, batch_size=batch_size)

    from torch.utils.data import DataLoader

    if not isinstance(data_loader, DataLoader):
        raise TypeError(
            "Expected instance of PyTorch `DataLoader, received %s instead.`"
            % str(type(data_loader))
        )

    self.data_loader = data_loader
# Buggy code
def __init__(self, data_loader, size, batch_size):
    """
    Create a data generator wrapper on top of an MXNet :class:`DataLoader`.

    :param data_loader:
    :type data_loader: `mxnet.gluon.data.DataLoader`
    :param size: Total size of the dataset.
    :type size: int
    :param batch_size: Size of the minibatches.
    :type batch_size: int
    """
    from mxnet.gluon.data import DataLoader

    if not isinstance(data_loader, DataLoader):
        raise TypeError(
            "Expected instance of Gluon `DataLoader, received %s instead.`"
            % str(type(data_loader))
        )

    self.data_loader = data_loader

    if size is not None and (type(size) is not int or size < 1):
        raise ValueError(
            "The total size of the dataset must be an integer greater than zero."
        )

    self.size = size

    if type(batch_size) is not int or batch_size < 1:
        raise ValueError("The batch size must be an integer greater than zero.")

    self.batch_size = batch_size


# Traceback (IndexError)
Traceback (most recent call last):
File "cw_pytorch.py", line 172, in <module>
x_test_adv = cl2m.generate(inputs, **params)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/art/attacks/carlini.py", line 380, in generate
x_adv_batch_tanh[active_and_update_adv] = x_adv_batch_tanh[update_adv] + \
IndexError: boolean index did not match indexed array along dimension 0; dimension is 18 but corresponding boolean dimension is 17

# Fixed code
def __init__(self, data_loader, size, batch_size):
    """
    Create a data generator wrapper on top of an MXNet :class:`DataLoader`.
    :param data_loader:
    :type data_loader: `mxnet.gluon.data.DataLoader`
    :param size: Total size of the dataset.
    :type size: int
    :param batch_size: Size of the minibatches.
    :type batch_size: int
    """
    super(MXDataGenerator, self).__init__(size=size, batch_size=batch_size)

    from mxnet.gluon.data import DataLoader

    if not isinstance(data_loader, DataLoader):
        raise TypeError(
            "Expected instance of Gluon `DataLoader, received %s instead.`"
            % str(type(data_loader))
        )

    self.data_loader = data_loader
# Buggy code
def __init__(self):
    pass


# Traceback (IndexError)
Traceback (most recent call last):
File "cw_pytorch.py", line 172, in <module>
x_test_adv = cl2m.generate(inputs, **params)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/art/attacks/carlini.py", line 380, in generate
x_adv_batch_tanh[active_and_update_adv] = x_adv_batch_tanh[update_adv] + \
IndexError: boolean index did not match indexed array along dimension 0; dimension is 18 but corresponding boolean dimension is 17

# Fixed code
def __init__(self, sess, iterator, iterator_type, iterator_arg, size, batch_size):
    """
    Create a data generator wrapper for TensorFlow. Supported iterators: initializable, reinitializable, feedable.
    :param sess: Tensorflow session.
    :type sess: `tf.Session`
    :param iterator: Data iterator from TensorFlow.
    :type iterator: `tensorflow.python.data.ops.iterator_ops.Iterator`
    :param iterator_type: Type of the iterator. Supported types: `initializable`, `reinitializable`, `feedable`.
    :type iterator_type: `string`
    :param iterator_arg: Argument to initialize the iterator. It is either a feed_dict used for the initializable
    and feedable mode, or an init_op used for the reinitializable mode.
    :type iterator_arg: `dict`, `tuple` or `tensorflow.python.framework.ops.Operation`
    :param size: Total size of the dataset.
    :type size: `int`
    :param batch_size: Size of the minibatches.
    :type batch_size: `int`
    """
    import tensorflow as tf

    super(TFDataGenerator, self).__init__(size=size, batch_size=batch_size)
    self.sess = sess
    self.iterator = iterator
    self.iterator_type = iterator_type
    self.iterator_arg = iterator_arg

    if not isinstance(iterator, tf.data.Iterator):
        raise ("Only support object tf.data.Iterator")

    if iterator_type == "initializable":
        if type(iterator_arg) != dict:
            raise ("Need to pass a dictionary for iterator type %s" % iterator_type)
    elif iterator_type == "reinitializable":
        if not isinstance(iterator_arg, tf.Operation):
            raise (
                "Need to pass a tensorflow operation for iterator type %s"
                % iterator_type
            )
    elif iterator_type == "feedable":
        if type(iterator_arg) != tuple:
            raise ("Need to pass a tuple for iterator type %s" % iterator_type)
    else:
        raise ("Iterator type %s not supported" % iterator_type)
# Buggy code
def get_batch(self):
    """
    Provide the next batch for training in the form of a tuple `(x, y)`. The generator should loop over the data
    indefinitely.

    :return: A tuple containing a batch of data `(x, y)`.
    :rtype: `tuple`
    """
    raise NotImplementedError


# Traceback (IndexError)
Traceback (most recent call last):
File "cw_pytorch.py", line 172, in <module>
x_test_adv = cl2m.generate(inputs, **params)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/art/attacks/carlini.py", line 380, in generate
x_adv_batch_tanh[active_and_update_adv] = x_adv_batch_tanh[update_adv] + \
IndexError: boolean index did not match indexed array along dimension 0; dimension is 18 but corresponding boolean dimension is 17

# Fixed code
def get_batch(self):
    """
    Provide the next batch for training in the form of a tuple `(x, y)`. The generator should loop over the data
    indefinitely.
    :return: A tuple containing a batch of data `(x, y)`.
    :rtype: `tuple`
    :raises: `ValueError` if the iterator has reached the end.
    """
    import tensorflow as tf

    # Get next batch
    next_batch = self.iterator.get_next()

    # Process to get the batch
    try:
        if self.iterator_type in ("initializable", "reinitializable"):
            return self.sess.run(next_batch)
        else:
            return self.sess.run(next_batch, feed_dict=self.iterator_arg[1])
    except (tf.errors.FailedPreconditionError, tf.errors.OutOfRangeError):
        if self.iterator_type == "initializable":
            self.sess.run(self.iterator.initializer, feed_dict=self.iterator_arg)
            return self.sess.run(next_batch)
        elif self.iterator_type == "reinitializable":
            self.sess.run(self.iterator_arg)
            return self.sess.run(next_batch)
        else:
            self.sess.run(self.iterator_arg[0].initializer)
            return self.sess.run(next_batch, feed_dict=self.iterator_arg[1])
# Buggy code
def set_params(self, **kwargs):
    """
    Take in a dictionary of parameters and applies defence-specific checks before saving them as attributes.

    :param quality: The image quality, on a scale from 1 (worst) to 95 (best). Values above 95 should be avoided.
    :type quality: `int`
    :param channel_index: Index of the axis in data containing the color channels or features.
    :type channel_index: `int`
    """
    # Save defense-specific parameters
    super(JpegCompression, self).set_params(**kwargs)

    if (
        not isinstance(self.quality, (int, np.int))
        or self.quality <= 0
        or self.quality > 100
    ):
        logger.error("Image quality must be a positive integer and smaller than 101.")
        raise ValueError(
            "Image quality must be a positive integer and smaller than 101."
        )

    if not isinstance(self.channel_index, (int, np.int)) or self.channel_index <= 0:
        logger.error(
            "Data channel must be a positive integer. The batch dimension is not a valid channel."
        )
        raise ValueError(
            "Image quality must be a positive integer and smaller than 101."
        )

    return True


# Traceback (IndexError)
Traceback (most recent call last):
File "cw_pytorch.py", line 172, in <module>
x_test_adv = cl2m.generate(inputs, **params)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/art/attacks/carlini.py", line 380, in generate
x_adv_batch_tanh[active_and_update_adv] = x_adv_batch_tanh[update_adv] + \
IndexError: boolean index did not match indexed array along dimension 0; dimension is 18 but corresponding boolean dimension is 17

# Fixed code
def set_params(self, **kwargs):
    """
    Take in a dictionary of parameters and applies defence-specific checks before saving them as attributes.

    :param quality: The image quality, on a scale from 1 (worst) to 95 (best). Values above 95 should be avoided.
    :type quality: `int`
    :param channel_index: Index of the axis in data containing the color channels or features.
    :type channel_index: `int`
    """
    # Save defense-specific parameters
    super(JpegCompression, self).set_params(**kwargs)

    if (
        not isinstance(self.quality, (int, np.int))
        or self.quality <= 0
        or self.quality > 100
    ):
        logger.error("Image quality must be a positive integer and smaller than 101.")
        raise ValueError(
            "Image quality must be a positive integer and smaller than 101."
        )

    if not isinstance(self.channel_index, (int, np.int)) or self.channel_index <= 0:
        logger.error(
            "Data channel must be a positive integer. The batch dimension is not a valid channel."
        )
        raise ValueError(
            "Data channel must be a positive integer and smaller than 101."
        )

    return True
# Buggy code
def __init__(self, prob=0.3, norm=2, lam=0.5, solver="L-BFGS-B", maxiter=10):
    """
    Create an instance of total variance minimization.

    :param prob: Probability of the Bernoulli distribution.
    :type prob: `float`
    :param norm: The norm (positive integer).
    :type norm: `int`
    :param lam: The lambda parameter in the objective function.
    :type lam: `float`
    :param solver: Current support: L-BFGS-B, CG, Newton-CG
    :type solver: `string`
    :param maxiter: Maximum number of iterations in an optimization.
    :type maxiter: `int`
    """
    super(TotalVarMin, self).__init__()
    self._is_fitted = True
    self.set_params(prob=prob, norm=norm, lam=lam, solver=solver, maxiter=maxiter)


# Traceback (IndexError)
Traceback (most recent call last):
File "cw_pytorch.py", line 172, in <module>
x_test_adv = cl2m.generate(inputs, **params)
File "/home/weitian/anaconda3/envs/xnor/lib/python3.6/site-packages/art/attacks/carlini.py", line 380, in generate
x_adv_batch_tanh[active_and_update_adv] = x_adv_batch_tanh[update_adv] + \
IndexError: boolean index did not match indexed array along dimension 0; dimension is 18 but corresponding boolean dimension is 17

# Fixed code
def __init__(self, prob=0.3, norm=2, lamb=0.5, solver="L-BFGS-B", max_iter=10):
    """
    Create an instance of total variance minimization.

    :param prob: Probability of the Bernoulli distribution.
    :type prob: `float`
    :param norm: The norm (positive integer).
    :type norm: `int`
    :param lamb: The lambda parameter in the objective function.
    :type lamb: `float`
    :param solver: Current support: L-BFGS-B, CG, Newton-CG
    :type solver: `str`
    :param max_iter: Maximum number of iterations in an optimization.
    :type max_iter: `int`
    """
    super(TotalVarMin, self).__init__()
    self._is_fitted = True
    self.set_params(prob=prob, norm=norm, lamb=lamb, solver=solver, max_iter=max_iter)