-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
fast_gradient.py
623 lines (528 loc) · 26.8 KB
/
fast_gradient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
863
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
# MIT License
#
# Copyright (C) The Adversarial Robustness Toolbox (ART) Authors 2018
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""
This module implements the Fast Gradient Method attack. This implementation includes the original Fast Gradient Sign
Method attack and extends it to other norms, therefore it is called the Fast Gradient Method.
| Paper link: https://arxiv.org/abs/1412.6572
"""
from __future__ import absolute_import, division, print_function, unicode_literals, annotations
import logging
from typing import TYPE_CHECKING
import numpy as np
from art.config import ART_NUMPY_DTYPE
from art.attacks.attack import EvasionAttack
from art.estimators.estimator import BaseEstimator, LossGradientsMixin
from art.estimators.classification.classifier import ClassifierMixin
from art.utils import (
compute_success,
get_labels_np_array,
random_sphere,
projection,
check_and_transform_label_format,
)
from art.summary_writer import SummaryWriter
if TYPE_CHECKING:
from art.utils import CLASSIFIER_LOSS_GRADIENTS_TYPE
logger = logging.getLogger(__name__)
class FastGradientMethod(EvasionAttack):
"""
This attack was originally implemented by Goodfellow et al. (2015) with the infinity norm (and is known as the "Fast
Gradient Sign Method"). This implementation extends the attack to other norms, and is therefore called the Fast
Gradient Method.
| Paper link: https://arxiv.org/abs/1412.6572
"""
attack_params = EvasionAttack.attack_params [
"norm",
"eps",
"eps_step",
"targeted",
"num_random_init",
"batch_size",
"minimal",
"summary_writer",
]
_estimator_requirements = (BaseEstimator, LossGradientsMixin)
def __init__(
self,
estimator: "CLASSIFIER_LOSS_GRADIENTS_TYPE",
norm: int | float | str = np.inf,
eps: int | float | np.ndarray = 0.3,
eps_step: int | float | np.ndarray = 0.1,
targeted: bool = False,
num_random_init: int = 0,
batch_size: int = 32,
minimal: bool = False,
summary_writer: str | bool | SummaryWriter = False,
) -> None:
"""
Create a :class:`.FastGradientMethod` instance.
:param estimator: A trained classifier.
:param norm: The norm of the adversarial perturbation. Possible values: "inf", `np.inf` or a real `p >= 1`.
:param eps: Attack step size (input variation).
:param eps_step: Step size of input variation for minimal perturbation computation.
:param targeted: Indicates whether the attack is targeted (True) or untargeted (False)
:param num_random_init: Number of random initialisations within the epsilon ball. For random_init=0 starting at
the original input.
:param batch_size: Size of the batch on which adversarial samples are generated.
:param minimal: Indicates if computing the minimal perturbation (True). If True, also define `eps_step` for
the step size and eps for the maximum perturbation.
:param summary_writer: Activate summary writer for TensorBoard.
Default is `False` and deactivated summary writer.
If `True` save runs/CURRENT_DATETIME_HOSTNAME in current directory.
If of type `str` save in path.
If of type `SummaryWriter` apply provided custom summary writer.
Use hierarchical folder structure to compare between runs easily. e.g. pass in
‘runs/exp1’, ‘runs/exp2’, etc. for each new experiment to compare across them.
"""
super().__init__(estimator=estimator, summary_writer=summary_writer)
self.norm = norm
self.eps = eps
self.eps_step = eps_step
self._targeted = targeted
self.num_random_init = num_random_init
self.batch_size = batch_size
self.minimal = minimal
self._project = True
FastGradientMethod._check_params(self)
self._batch_id = 0
self._i_max_iter = 0
def _check_compatibility_input_and_eps(self, x: np.ndarray):
"""
Check the compatibility of the input with `eps` and `eps_step` which are of the same shape.
:param x: An array with the original inputs.
"""
if isinstance(self.eps, np.ndarray):
# Ensure the eps array is broadcastable
if self.eps.ndim > x.ndim: # pragma: no cover
raise ValueError("The `eps` shape must be broadcastable to input shape.")
def _minimal_perturbation(self, x: np.ndarray, y: np.ndarray, mask: np.ndarray) -> np.ndarray:
"""
Iteratively compute the minimal perturbation necessary to make the class prediction change. Stop when the
first adversarial example was found.
:param x: An array with the original inputs.
:param y: Target values (class labels) one-hot-encoded of shape (nb_samples, nb_classes).
:return: An array holding the adversarial examples.
"""
adv_x = x.copy()
# Compute perturbation with implicit batching
for batch_id in range(int(np.ceil(adv_x.shape[0] / float(self.batch_size)))):
batch_index_1, batch_index_2 = (
batch_id * self.batch_size,
(batch_id 1) * self.batch_size,
)
batch = adv_x[batch_index_1:batch_index_2]
batch_labels = y[batch_index_1:batch_index_2]
mask_batch = mask
if mask is not None:
# Here we need to make a distinction: if the masks are different for each input, we need to index
# those for the current batch. Otherwise, (i.e. mask is meant to be broadcasted), keep it as it is.
if len(mask.shape) == len(x.shape):
mask_batch = mask[batch_index_1:batch_index_2]
# Get perturbation
perturbation = self._compute_perturbation(batch, batch_labels, mask_batch)
# Get current predictions
active_indices = np.arange(len(batch))
current_eps: int | float | np.ndarray
partial_stop_condition: bool | np.ndarray
if isinstance(self.eps, np.ndarray) and isinstance(self.eps_step, np.ndarray):
if len(self.eps.shape) == len(x.shape) and self.eps.shape[0] == x.shape[0]:
current_eps = self.eps_step[batch_index_1:batch_index_2]
partial_stop_condition = bool((current_eps <= self.eps[batch_index_1:batch_index_2]).all())
else:
current_eps = self.eps_step
partial_stop_condition = bool((current_eps <= self.eps).all())
else:
current_eps = self.eps_step
partial_stop_condition = current_eps <= self.eps
while active_indices.size > 0 and partial_stop_condition:
# Adversarial crafting
current_x = self._apply_perturbation(x[batch_index_1:batch_index_2], perturbation, current_eps)
# Update
batch[active_indices] = current_x[active_indices]
adv_preds = self.estimator.predict(batch)
# If targeted active check to see whether we have hit the target, otherwise head to anything but
if self.targeted:
active_indices = np.where(np.argmax(batch_labels, axis=1) != np.argmax(adv_preds, axis=1))[0]
else:
active_indices = np.where(np.argmax(batch_labels, axis=1) == np.argmax(adv_preds, axis=1))[0]
# Update current eps and check the stop condition
if isinstance(self.eps, np.ndarray) and isinstance(self.eps_step, np.ndarray):
if len(self.eps.shape) == len(x.shape) and self.eps.shape[0] == x.shape[0]:
current_eps = current_eps self.eps_step[batch_index_1:batch_index_2]
partial_stop_condition = bool((current_eps <= self.eps[batch_index_1:batch_index_2]).all())
else:
current_eps = current_eps self.eps_step
partial_stop_condition = bool((current_eps <= self.eps).all())
else:
current_eps = current_eps self.eps_step
partial_stop_condition = current_eps <= self.eps
adv_x[batch_index_1:batch_index_2] = batch
return adv_x
def generate(self, x: np.ndarray, y: np.ndarray | None = None, **kwargs) -> np.ndarray:
"""Generate adversarial samples and return them in an array.
:param x: An array with the original inputs.
:param y: Target values (class labels) one-hot-encoded of shape (nb_samples, nb_classes) or indices of shape
(nb_samples,). Only provide this parameter if you'd like to use true labels when crafting adversarial
samples. Otherwise, model predictions are used as labels to avoid the "label leaking" effect
(explained in this paper: https://arxiv.org/abs/1611.01236). Default is `None`.
:param mask: An array with a mask broadcastable to input `x` defining where to apply adversarial perturbations.
Shape needs to be broadcastable to the shape of x and can also be of the same shape as `x`. Any
features for which the mask is zero will not be adversarially perturbed.
:type mask: `np.ndarray`
:return: An array holding the adversarial examples.
"""
mask = self._get_mask(x, **kwargs)
# Ensure eps is broadcastable
self._check_compatibility_input_and_eps(x=x)
if isinstance(self.estimator, ClassifierMixin):
if y is not None:
y = check_and_transform_label_format(y, nb_classes=self.estimator.nb_classes)
if y is None:
# Throw error if attack is targeted, but no targets are provided
if self.targeted: # pragma: no cover
raise ValueError("Target labels `y` need to be provided for a targeted attack.")
# Use model predictions as correct outputs
logger.info("Using model predictions as correct labels for FGM.")
y_array = get_labels_np_array(self.estimator.predict(x, batch_size=self.batch_size)) # type: ignore
else:
y_array = y
if self.estimator.nb_classes > 2:
y_array = y_array / np.sum(y_array, axis=1, keepdims=True)
# Return adversarial examples computed with minimal perturbation if option is active
adv_x_best = x
if self.minimal:
logger.info("Performing minimal perturbation FGM.")
adv_x_best = self._minimal_perturbation(x, y_array, mask)
rate_best = 100 * compute_success(
self.estimator, # type: ignore
x,
y_array,
adv_x_best,
self.targeted,
batch_size=self.batch_size, # type: ignore
)
else:
rate_best = 0.0
for _ in range(max(1, self.num_random_init)):
adv_x = self._compute(
x,
x,
y_array,
mask,
self.eps,
self.eps,
self._project,
self.num_random_init > 0,
)
if self.num_random_init > 1:
rate = 100 * compute_success(
self.estimator, # type: ignore
x,
y_array,
adv_x,
self.targeted,
batch_size=self.batch_size, # type: ignore
)
if rate > rate_best:
rate_best = rate
adv_x_best = adv_x
else:
adv_x_best = adv_x
logger.info(
"Success rate of FGM attack: %.2f%%",
(
rate_best
if rate_best is not None
else 100
* compute_success(
self.estimator, # type: ignore
x,
y_array,
adv_x_best,
self.targeted,
batch_size=self.batch_size,
)
),
)
else:
if self.minimal: # pragma: no cover
raise ValueError("Minimal perturbation is only supported for classification.")
if y is None:
# Throw error if attack is targeted, but no targets are provided
if self.targeted: # pragma: no cover
raise ValueError("Target labels `y` need to be provided for a targeted attack.")
# Use model predictions as correct outputs
logger.info("Using model predictions as correct labels for FGM.")
y_array = self.estimator.predict(x, batch_size=self.batch_size)
else:
y_array = y
adv_x_best = self._compute(
x,
x,
y_array,
None,
self.eps,
self.eps,
self._project,
self.num_random_init > 0,
)
if self.summary_writer is not None:
self.summary_writer.reset()
return adv_x_best
def _check_params(self) -> None:
norm: float = np.inf if self.norm == "inf" else float(self.norm)
if norm < 1:
raise ValueError('Norm order must be either "inf", `np.inf` or a real `p >= 1`.')
if not (
isinstance(self.eps, (int, float))
and isinstance(self.eps_step, (int, float))
or isinstance(self.eps, np.ndarray)
and isinstance(self.eps_step, np.ndarray)
):
raise TypeError(
"The perturbation size `eps` and the perturbation step-size `eps_step` must have the same type of `int`"
", `float`, or `np.ndarray`."
)
if isinstance(self.eps, (int, float)):
if self.eps < 0:
raise ValueError("The perturbation size `eps` has to be nonnegative.")
else:
if (self.eps < 0).any():
raise ValueError("The perturbation size `eps` has to be nonnegative.")
if isinstance(self.eps_step, (int, float)):
if self.eps_step <= 0:
raise ValueError("The perturbation step-size `eps_step` has to be positive.")
else:
if (self.eps_step <= 0).any():
raise ValueError("The perturbation step-size `eps_step` has to be positive.")
if isinstance(self.eps, np.ndarray) and isinstance(self.eps_step, np.ndarray):
if self.eps.shape != self.eps_step.shape:
raise ValueError(
"The perturbation size `eps` and the perturbation step-size `eps_step` must have the same shape."
)
if not isinstance(self.targeted, bool):
raise ValueError("The flag `targeted` has to be of type bool.")
if not isinstance(self.num_random_init, int):
raise TypeError("The number of random initialisations has to be of type integer")
if self.num_random_init < 0:
raise ValueError("The number of random initialisations `random_init` has to be greater than or equal to 0.")
if self.batch_size <= 0:
raise ValueError("The batch size `batch_size` has to be positive.")
if not isinstance(self.minimal, bool):
raise ValueError("The flag `minimal` has to be of type bool.")
def _compute_perturbation(
self,
x: np.ndarray,
y: np.ndarray,
mask: np.ndarray | None,
decay: float | None = None,
momentum: np.ndarray | None = None,
) -> np.ndarray:
# Get gradient wrt loss; invert it if attack is targeted
grad = self.estimator.loss_gradient(x, y) * (1 - 2 * int(self.targeted))
# Write summary
if self.summary_writer is not None: # pragma: no cover
self.summary_writer.update(
batch_id=self._batch_id,
global_step=self._i_max_iter,
grad=grad,
patch=None,
estimator=self.estimator,
x=x,
y=y,
targeted=self.targeted,
)
# Check for NaN before normalisation and replace with 0
if grad.dtype != object and np.isnan(grad).any(): # pragma: no cover
logger.warning("Elements of the loss gradient are NaN and have been replaced with 0.0.")
grad = np.where(np.isnan(grad), 0.0, grad)
else:
for i, _ in enumerate(grad):
grad_i_array = grad[i].astype(np.float32)
if np.isnan(grad_i_array).any():
grad[i] = np.where(np.isnan(grad_i_array), 0.0, grad_i_array).astype(object)
# Apply mask
if mask is not None:
grad = np.where(mask == 0.0, 0.0, grad)
# Apply norm bound
def _apply_norm(norm, grad, object_type=False):
"""Returns an x maximizing <grad, x> subject to ||x||_norm<=1."""
if (grad.dtype != object and np.isinf(grad).any()) or np.isnan( # pragma: no cover
grad.astype(np.float32)
).any():
logger.info("The loss gradient array contains at least one positive or negative infinity.")
grad_2d = grad.reshape(1 if object_type else len(grad), -1)
if norm in [np.inf, "inf"]:
grad_2d = np.ones_like(grad_2d)
elif norm == 1:
i_max = np.argmax(np.abs(grad_2d), axis=1)
grad_2d = np.zeros_like(grad_2d)
grad_2d[range(len(grad_2d)), i_max] = 1
elif norm > 1:
conjugate = norm / (norm - 1)
q_norm = np.linalg.norm(grad_2d, ord=conjugate, axis=1, keepdims=True)
grad_2d = (np.abs(grad_2d) / np.where(q_norm, q_norm, np.inf)) ** (conjugate - 1)
grad = grad_2d.reshape(grad.shape) * np.sign(grad)
return grad
# Compute gradient momentum
if decay is not None and momentum is not None:
if x.dtype == object:
raise NotImplementedError("Momentum Iterative Method not yet implemented for object type input.")
# Update momentum in-place (important).
# The L1 normalization for accumulation is an arbitrary choice of the paper.
grad_2d = grad.reshape(len(grad), -1)
norm1 = np.linalg.norm(grad_2d, ord=1, axis=1, keepdims=True)
normalized_grad = (grad_2d / np.where(norm1, norm1, np.inf)).reshape(grad.shape)
momentum *= decay
momentum = normalized_grad
# Use the momentum to compute the perturbation, instead of the gradient
grad = momentum
if x.dtype == object:
for i_sample in range(x.shape[0]):
grad[i_sample] = _apply_norm(self.norm, grad[i_sample], object_type=True)
assert x[i_sample].shape == grad[i_sample].shape
else:
grad = _apply_norm(self.norm, grad)
assert x.shape == grad.shape
return grad
def _apply_perturbation(
self, x: np.ndarray, perturbation: np.ndarray, eps_step: int | float | np.ndarray
) -> np.ndarray:
perturbation_step = eps_step * perturbation
if perturbation_step.dtype != object:
perturbation_step[np.isnan(perturbation_step)] = 0
else:
for i, _ in enumerate(perturbation_step):
perturbation_step_i_array = perturbation_step[i].astype(np.float32)
if np.isnan(perturbation_step_i_array).any():
perturbation_step[i] = np.where(
np.isnan(perturbation_step_i_array), 0.0, perturbation_step_i_array
).astype(object)
x = x perturbation_step
if self.estimator.clip_values is not None:
clip_min, clip_max = self.estimator.clip_values
if x.dtype == object:
for i_obj in range(x.shape[0]):
x[i_obj] = np.clip(x[i_obj], clip_min, clip_max)
else:
x = np.clip(x, clip_min, clip_max)
return x
def _compute(
self,
x: np.ndarray,
x_init: np.ndarray,
y: np.ndarray,
mask: np.ndarray | None,
eps: int | float | np.ndarray,
eps_step: int | float | np.ndarray,
project: bool,
random_init: bool,
batch_id_ext: int | None = None,
decay: float | None = None,
momentum: np.ndarray | None = None,
) -> np.ndarray:
if random_init:
n = x.shape[0]
m = np.prod(x.shape[1:]).item()
random_perturbation = random_sphere(n, m, eps, self.norm).reshape(x.shape).astype(ART_NUMPY_DTYPE)
if mask is not None:
random_perturbation = random_perturbation * (mask.astype(ART_NUMPY_DTYPE))
x_adv = x.astype(ART_NUMPY_DTYPE) random_perturbation
if self.estimator.clip_values is not None:
clip_min, clip_max = self.estimator.clip_values
x_adv = np.clip(x_adv, clip_min, clip_max)
else:
if x.dtype == object:
x_adv = x.copy()
else:
x_adv = x.astype(ART_NUMPY_DTYPE)
# Compute perturbation with implicit batching
for batch_id in range(int(np.ceil(x.shape[0] / float(self.batch_size)))):
if batch_id_ext is None:
self._batch_id = batch_id
else:
self._batch_id = batch_id_ext
batch_index_1, batch_index_2 = batch_id * self.batch_size, (batch_id 1) * self.batch_size
batch_index_2 = min(batch_index_2, x.shape[0])
batch = x_adv[batch_index_1:batch_index_2]
batch_labels = y[batch_index_1:batch_index_2]
mask_batch = mask
if mask is not None:
# Here we need to make a distinction: if the masks are different for each input, we need to index
# those for the current batch. Otherwise, (i.e. mask is meant to be broadcasted), keep it as it is.
if len(mask.shape) == len(x.shape):
mask_batch = mask[batch_index_1:batch_index_2]
# Get perturbation
perturbation = self._compute_perturbation(batch, batch_labels, mask_batch, decay, momentum)
batch_eps: int | float | np.ndarray
batch_eps_step: int | float | np.ndarray
# Compute batch_eps and batch_eps_step
if isinstance(eps, np.ndarray) and isinstance(eps_step, np.ndarray):
if len(eps.shape) == len(x.shape) and eps.shape[0] == x.shape[0]:
batch_eps = eps[batch_index_1:batch_index_2]
batch_eps_step = eps_step[batch_index_1:batch_index_2]
else:
batch_eps = eps
batch_eps_step = eps_step
else:
batch_eps = eps
batch_eps_step = eps_step
# Apply perturbation and clip
x_adv[batch_index_1:batch_index_2] = self._apply_perturbation(batch, perturbation, batch_eps_step)
if project:
if x_adv.dtype == object:
for i_sample in range(batch_index_1, batch_index_2):
if isinstance(batch_eps, np.ndarray) and batch_eps.shape[0] == x_adv.shape[0]:
perturbation = projection(
x_adv[i_sample] - x_init[i_sample], batch_eps[i_sample], self.norm
)
else:
perturbation = projection(x_adv[i_sample] - x_init[i_sample], batch_eps, self.norm)
x_adv[i_sample] = x_init[i_sample] perturbation
else:
perturbation = projection(
x_adv[batch_index_1:batch_index_2] - x_init[batch_index_1:batch_index_2], batch_eps, self.norm
)
x_adv[batch_index_1:batch_index_2] = x_init[batch_index_1:batch_index_2] perturbation
return x_adv
@staticmethod
def _get_mask(x: np.ndarray, **kwargs) -> np.ndarray:
"""
Get the mask from the kwargs.
:param x: An array with the original inputs.
:param mask: An array with a mask to be applied to the adversarial perturbations. Shape needs to be
broadcastable to the shape of x. Any features for which the mask is zero will not be adversarially
perturbed.
:type mask: `np.ndarray`
:return: The mask.
"""
mask = kwargs.get("mask")
if mask is not None:
if mask.ndim > x.ndim: # pragma: no cover
raise ValueError("Mask shape must be broadcastable to input shape.")
if not (np.issubdtype(mask.dtype, np.floating) or mask.dtype == bool): # pragma: no cover
raise ValueError(
f"The `mask` has to be either of type np.float32, np.float64 or bool. The provided"
f"`mask` is of type {mask.dtype}."
)
if np.issubdtype(mask.dtype, np.floating) and np.amin(mask) < 0.0: # pragma: no cover
raise ValueError(
"The `mask` of type np.float32 or np.float64 requires all elements to be either zero"
"or positive values."
)
return mask