• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

bethgelab / foolbox / 8140123939

04 Mar 2024 12:25PM UTC coverage: 98.477%. Remained the same
8140123939

push

github

web-flow
Increase package version (#724)

3492 of 3546 relevant lines covered (98.48%)

7.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.86
/foolbox/attacks/fast_gradient_method.py
1
from .gradient_descent_base import L1BaseGradientDescent
10✔
2
from .gradient_descent_base import L2BaseGradientDescent
10✔
3
from .gradient_descent_base import LinfBaseGradientDescent
10✔
4
from ..models.base import Model
10✔
5
from ..criteria import Misclassification, TargetedMisclassification
10✔
6
from .base import T
10✔
7
from typing import Union, Any
10✔
8

9

10
class L1FastGradientAttack(L1BaseGradientDescent):
10✔
11
    """Fast Gradient Method (FGM) using the L1 norm
12

13
    Args:
14
        random_start : Controls whether to randomly start within allowed epsilon ball.
15
    """
16

17
    def __init__(self, *, random_start: bool = False):
10✔
18
        super().__init__(
10✔
19
            rel_stepsize=1.0,
20
            steps=1,
21
            random_start=random_start,
22
        )
23

24
    def run(
10✔
25
        self,
26
        model: Model,
27
        inputs: T,
28
        criterion: Union[Misclassification, TargetedMisclassification, T],
29
        *,
30
        epsilon: float,
31
        **kwargs: Any,
32
    ) -> T:
33
        if hasattr(criterion, "target_classes"):
6✔
34
            raise ValueError("unsupported criterion")
×
35

36
        return super().run(
6✔
37
            model=model, inputs=inputs, criterion=criterion, epsilon=epsilon, **kwargs
38
        )
39

40

41
class L2FastGradientAttack(L2BaseGradientDescent):
10✔
42
    """Fast Gradient Method (FGM)
43

44
    Args:
45
        random_start : Controls whether to randomly start within allowed epsilon ball.
46
    """
47

48
    def __init__(self, *, random_start: bool = False):
10✔
49
        super().__init__(
10✔
50
            rel_stepsize=1.0,
51
            steps=1,
52
            random_start=random_start,
53
        )
54

55
    def run(
10✔
56
        self,
57
        model: Model,
58
        inputs: T,
59
        criterion: Union[Misclassification, TargetedMisclassification, T],
60
        *,
61
        epsilon: float,
62
        **kwargs: Any,
63
    ) -> T:
64
        if hasattr(criterion, "target_classes"):
6✔
65
            raise ValueError("unsupported criterion")
×
66

67
        return super().run(
6✔
68
            model=model, inputs=inputs, criterion=criterion, epsilon=epsilon, **kwargs
69
        )
70

71

72
class LinfFastGradientAttack(LinfBaseGradientDescent):
10✔
73
    """Fast Gradient Sign Method (FGSM)
74

75
    Args:
76
        random_start : Controls whether to randomly start within allowed epsilon ball.
77
    """
78

79
    def __init__(self, *, random_start: bool = False):
10✔
80
        super().__init__(
10✔
81
            rel_stepsize=1.0,
82
            steps=1,
83
            random_start=random_start,
84
        )
85

86
    def run(
10✔
87
        self,
88
        model: Model,
89
        inputs: T,
90
        criterion: Union[Misclassification, TargetedMisclassification, T],
91
        *,
92
        epsilon: float,
93
        **kwargs: Any,
94
    ) -> T:
95
        if hasattr(criterion, "target_classes"):
8✔
96
            raise ValueError("unsupported criterion")
6✔
97

98
        return super().run(
8✔
99
            model=model, inputs=inputs, criterion=criterion, epsilon=epsilon, **kwargs
100
        )
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc