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

bethgelab / foolbox / 8139141456

04 Mar 2024 11:03AM UTC coverage: 37.923% (-60.6%) from 98.477%
8139141456

Pull #722

github

web-flow
Merge 5663238db into 17e0e9b31
Pull Request #722: Fix guide compilation

1344 of 3544 relevant lines covered (37.92%)

0.38 hits per line

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

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

9

10
class L1FastGradientAttack(L1BaseGradientDescent):
1✔
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):
1✔
18
        super().__init__(
1✔
19
            rel_stepsize=1.0,
20
            steps=1,
21
            random_start=random_start,
22
        )
23

24
    def run(
1✔
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"):
×
34
            raise ValueError("unsupported criterion")
×
35

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

40

41
class L2FastGradientAttack(L2BaseGradientDescent):
1✔
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):
1✔
49
        super().__init__(
1✔
50
            rel_stepsize=1.0,
51
            steps=1,
52
            random_start=random_start,
53
        )
54

55
    def run(
1✔
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"):
×
65
            raise ValueError("unsupported criterion")
×
66

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

71

72
class LinfFastGradientAttack(LinfBaseGradientDescent):
1✔
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):
1✔
80
        super().__init__(
1✔
81
            rel_stepsize=1.0,
82
            steps=1,
83
            random_start=random_start,
84
        )
85

86
    def run(
1✔
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"):
×
96
            raise ValueError("unsupported criterion")
×
97

98
        return super().run(
×
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

© 2026 Coveralls, Inc