# 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 torch

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

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

    # Run prediction
    # preds = self._forward_at(torch.from_numpy(inputs), self._logit_layer).detach().numpy()
    # if not logits:
    #     exp = np.exp(preds - np.max(preds, axis=1, keepdims=True))
    #     preds = exp / np.sum(exp, axis=1, keepdims=True)

    # 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])

        model_outputs = self._model(
            torch.from_numpy(x_[begin:end]).to(self._device).float()
        )
        (logit_output, output) = (model_outputs[-2], model_outputs[-1])

        if logits:
            results[begin:end] = logit_output.detach().cpu().numpy()
        else:
            results[begin:end] = output.detach().cpu().numpy()

    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`
    """
    import torch

    # 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])

        model_outputs = self._model(
            torch.from_numpy(x_[begin:end]).to(self._device).float()
        )
        (logit_output, output) = (model_outputs[-2], model_outputs[-1])

        if logits:
            results[begin:end] = logit_output.detach().cpu().numpy()
        else:
            results[begin:end] = output.detach().cpu().numpy()

    return results