(sample, target)
| 79 | # pass to get an individual (per-sample) gradient. |
| 80 | |
| 81 | def compute_grad(sample, target): |
| 82 | sample = sample.unsqueeze(0) # prepend batch dimension for processing |
| 83 | target = target.unsqueeze(0) |
| 84 | |
| 85 | prediction = model(sample) |
| 86 | loss = loss_fn(prediction, target) |
| 87 | |
| 88 | return torch.autograd.grad(loss, list(model.parameters())) |
| 89 | |
| 90 | |
| 91 | def compute_sample_grads(data, targets): |
no test coverage detected