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

Function compute_grad

intermediate_source/per_sample_grads.py:81–88  ·  view source on GitHub ↗
(sample, target)

Source from the content-addressed store, hash-verified

79# pass to get an individual (per-sample) gradient.
80
81def 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
91def compute_sample_grads(data, targets):

Callers 1

compute_sample_gradsFunction · 0.85

Calls 2

loss_fnFunction · 0.85
modelFunction · 0.50

Tested by

no test coverage detected