import torch
// Convert the inputs to Tensors
inputs_t = torch.from_numpy(self._apply_processing(x)).to(self._device)inputs_t = inputs_t.float()
inputs_t.requires_grad = True
// Convert the labels to Tensors
labels_t = torch.from_numpy(np.argmax(y, axis=1)).to(self._device)
// Compute the gradient and return
model_outputs = self._model(inputs_t)
loss = self._loss(model_outputs[-1], labels_t)
// Clean gradients
self._model.zero_grad()
// inputs_t.grad.data.zero_()
// Compute gradients
loss.backward()
grds = inputs_t.grad.cpu().numpy().copy()
grds = self._apply_processing_gradient(grds)
assert grds.shape == x.shape
return grds
@property
def layer_names(self):
After Change
:return: Array of gradients of the same shape as `x`.
:rtype: `np.ndarray`
raise NotImplementedError
def get_activations(self, x, layer, batch_size=128):
Return the output of the specified layer for input `x`. `layer` is specified by layer index (between 0 and