def grad_norm(self, norm_type: Union[(float, int, str)]) -> Dict[(str, float)]:
    "Compute each parameter's gradient's norm and their overall norm.\n\n        The overall norm is computed over all gradients together, as if they\n        were concatenated into a single vector.\n\n        Args:\n            norm_type: The type of the used p-norm, cast to float if necessary.\n                Can be ``'inf'`` for infinity norm.\n\n        Return:\n            norms: The dictionary of p-norms of each parameter's gradient and\n                a special entry for the total p-norm of the gradients viewed\n                as a single vector.\n        "
    (norms, all_norms) = ({}, [])
    for (name, p) in self.named_parameters():
        if (name.split('.')[0] == 'model'):
            name = name[6:]
        if (p.grad is None):
            continue
        param_norm = float(p.data.norm(norm_type))
        grad_norm = float(p.grad.data.norm(norm_type))
        norms[f'grad_{norm_type}_norm/{name}'] = {'param': param_norm, 'grad': grad_norm}
        all_norms.append(param_norm)
    total_norm = float(torch.tensor(all_norms).norm(norm_type))
    norms[f'grad_{norm_type}_norm/total'] = round(total_norm, 3)
    return norms
Compute each parameter's gradient's norm and their overall norm.

The overall norm is computed over all gradients together, as if they
were concatenated into a single vector.

Args:
    norm_type: The type of the used p-norm, cast to float if necessary.
        Can be ``'inf'`` for infinity norm.

Return:
    norms: The dictionary of p-norms of each parameter's gradient and
        a special entry for the total p-norm of the gradients viewed
        as a single vector.
def grad_norm(self, norm_type: Union[(float, int, str)]) -> Dict[(str, float)]:
    "Compute each parameter's gradient's norm and their overall norm.\n\n        The overall norm is computed over all gradients together, as if they\n        were concatenated into a single vector.\n\n        Args:\n            norm_type: The type of the used p-norm, cast to float if necessary.\n                Can be ``'inf'`` for infinity norm.\n\n        Return:\n            norms: The dictionary of p-norms of each parameter's gradient and\n                a special entry for the total p-norm of the gradients viewed\n                as a single vector.\n        "
    (norms, all_norms) = ({}, [])
    for (name, p) in self.named_parameters():
        if (name.split('.')[0] == 'model'):
            name = name[6:]
        if (p.grad is None):
            continue
        param_norm = float(p.data.norm(norm_type))
        grad_norm = float(p.grad.data.norm(norm_type))
        norms[f'grad_{norm_type}_norm/{name}'] = {'param': param_norm, 'grad': grad_norm}
        all_norms.append(param_norm)
    total_norm = float(torch.tensor(all_norms).norm(norm_type))
    norms[f'grad_{norm_type}_norm/total'] = round(total_norm, 3)
    return norms