MCPcopy Create free account
hub / github.com/alinlab/SelfPatch / clip_gradients

Function clip_gradients

utils.py:112–121  ·  view source on GitHub ↗
(model, clip)

Source from the content-addressed store, hash-verified

110
111
112def clip_gradients(model, clip):
113 norms = []
114 for name, p in model.named_parameters():
115 if p.grad is not None:
116 param_norm = p.grad.data.norm(2)
117 norms.append(param_norm.item())
118 clip_coef = clip / (param_norm + 1e-6)
119 if clip_coef < 1:
120 p.grad.data.mul_(clip_coef)
121 return norms
122
123
124def cancel_gradients_last_layer(epoch, model, freeze_last_layer):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected