MCPcopy Index your code
hub / github.com/pytorch/tutorials / fgsm_attack

Function fgsm_attack

beginner_source/fgsm_tutorial.py:216–224  ·  view source on GitHub ↗
(image, epsilon, data_grad)

Source from the content-addressed store, hash-verified

214
215# FGSM attack code
216def fgsm_attack(image, epsilon, data_grad):
217 # Collect the element-wise sign of the data gradient
218 sign_data_grad = data_grad.sign()
219 # Create the perturbed image by adjusting each pixel of the input image
220 perturbed_image = image + epsilon*sign_data_grad
221 # Adding clipping to maintain [0,1] range
222 perturbed_image = torch.clamp(perturbed_image, 0, 1)
223 # Return the perturbed image
224 return perturbed_image
225
226# restores the tensors to their original scale
227def denorm(batch, mean=[0.1307], std=[0.3081]):

Callers 1

testFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected