7239447415c21586291d5c993e30fba5df54b9db,foolbox/models/pytorch.py,PyTorchModel,predictions_and_gradient,#PyTorchModel#Any#Any#,105
Before Change
images = image[np.newaxis]
images = torch.from_numpy(images).to(self.device)
if self._old_pytorch(): // pragma: no cover
target = Variable(target)
images = Variable(images, requires_grad=True)
else:
images.requires_grad_()
predictions = self._model(images)
ce = nn.CrossEntropyLoss()
loss = ce(predictions, target)
loss.backward()
grad = images.grad
if self._old_pytorch(): // pragma: no cover
predictions = predictions.data
predictions = predictions.to("cpu")
if not self._old_pytorch():
predictions = predictions.detach()
predictions = predictions.numpy()
predictions = np.squeeze(predictions, axis=0)
assert predictions.ndim == 1
assert predictions.shape == (self.num_classes(),)
if self._old_pytorch(): // pragma: no cover
grad = grad.data
grad = grad.to("cpu")
if not self._old_pytorch():
grad = grad.detach()
grad = grad.numpy()
grad = np.squeeze(grad, axis=0)
grad = self._process_gradient(dpdx, grad)
assert grad.shape == input_shape
After Change
assert predictions.ndim == 1
assert predictions.shape == (self.num_classes(),)
grad = grad.detach().cpu().numpy()
grad = np.squeeze(grad, axis=0)
grad = self._process_gradient(dpdx, grad)
assert grad.shape == input_shape
In pattern: SUPERPATTERN
Frequency: 3
Non-data size: 23
Instances
Project Name: bethgelab/foolbox
Commit Name: 7239447415c21586291d5c993e30fba5df54b9db
Time: 2019-05-17
Author: git@jonasrauber.de
File Name: foolbox/models/pytorch.py
Class Name: PyTorchModel
Method Name: predictions_and_gradient
Project Name: bethgelab/foolbox
Commit Name: 7239447415c21586291d5c993e30fba5df54b9db
Time: 2019-05-17
Author: git@jonasrauber.de
File Name: foolbox/models/pytorch.py
Class Name: PyTorchModel
Method Name: batch_gradients
Project Name: bethgelab/foolbox
Commit Name: 7239447415c21586291d5c993e30fba5df54b9db
Time: 2019-05-17
Author: git@jonasrauber.de
File Name: foolbox/models/pytorch.py
Class Name: PyTorchModel
Method Name: predictions_and_gradient
Project Name: bethgelab/foolbox
Commit Name: 7239447415c21586291d5c993e30fba5df54b9db
Time: 2019-05-17
Author: git@jonasrauber.de
File Name: foolbox/models/pytorch.py
Class Name: PyTorchModel
Method Name: batch_backward