Team Ai
Apppublic

OpenMotionLab/MotionGPT

sourceHugging Facemitupdated 1y agoView on Hugging Face
118likes
tools.py38 linesDownload Raw Back to utils
1import torch.nn as nn2 3def remove_padding(tensors, lengths):4    return [tensor[:tensor_length] for tensor, tensor_length in zip(tensors, lengths)]5 6class AutoParams(nn.Module):7    def __init__(self, **kargs):8        try:9            for param in self.needed_params:10                if param in kargs:11                    setattr(self, param, kargs[param])12                else:13                    raise ValueError(f"{param} is needed.")14        except :15            pass16            17        try:18            for param, default in self.optional_params.items():19                if param in kargs and kargs[param] is not None:20                    setattr(self, param, kargs[param])21                else:22                    setattr(self, param, default)23        except :24            pass25        super().__init__()26 27 28# taken from joeynmt repo29def freeze_params(module: nn.Module) -> None:30    """31    Freeze the parameters of this module,32    i.e. do not update them during training33 34    :param module: freeze parameters of this module35    """36    for _, p in module.named_parameters():37        p.requires_grad = False38