Skip to content

Commit

Permalink
Merge pull request #2455 from salomonhotegni/my-feature-branch
Browse files Browse the repository at this point in the history
Update projected_gradient_descent_pytorch.py [Solve non-writable NumPy array and device mismatch issues]
  • Loading branch information
beat-buesser authored Jul 2, 2024
2 parents a20c78f + d79e663 commit 7044477
Showing 1 changed file with 4 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -497,11 +497,13 @@ def _projection(
if (suboptimal or norm == 2) and norm != np.inf: # Simple rescaling
values_norm = torch.linalg.norm(values_tmp, ord=norm, dim=1, keepdim=True) # (n_samples, 1)
values_tmp = values_tmp * values_norm.where(
values_norm == 0, torch.minimum(torch.ones(1), torch.Tensor(eps) / values_norm)
values_norm == 0, torch.minimum(torch.ones(1), torch.tensor(eps).to(values_tmp.device) / values_norm)
)
else: # Optimal
if norm == np.inf: # Easy exact case
values_tmp = values_tmp.sign() * torch.minimum(values_tmp.abs(), torch.Tensor(eps))
values_tmp = values_tmp.sign() * torch.minimum(

Check warning on line 504 in art/attacks/evasion/projected_gradient_descent/projected_gradient_descent_pytorch.py

View check run for this annotation

Codecov / codecov/patch

art/attacks/evasion/projected_gradient_descent/projected_gradient_descent_pytorch.py#L504

Added line #L504 was not covered by tests
values_tmp.abs(), torch.tensor(eps).to(values_tmp.device)
)
elif norm >= 1: # Convex optim
raise NotImplementedError(
"Finite values of `norm_p >= 1` are currently not supported with `suboptimal=False`."
Expand Down

0 comments on commit 7044477

Please sign in to comment.