def clip_gradients(batch_result, model, max_grad_norm):
if max_grad_norm is not None:<EOL><INDENT>grad_norm = torch.nn.utils.clip_grad_norm_(<EOL>filter(lambda p: p.requires_grad, model.parameters()),<EOL>max_norm=max_grad_norm<EOL>)<EOL><DEDENT>else:<EOL><INDENT>grad_norm = <NUM_LIT:0.0><EOL><DEDENT>batch_result['<STR_LIT>'] = grad_norm<EOL>
Clip gradients to a given maximum length