CoolFace
Apppublic

helios0l/RVC_HF

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
utils.py67 linesDownload Raw Back to torchgate
1import torch2from torch.types import Number3 4 5@torch.no_grad()6def amp_to_db(x: torch.Tensor, eps=torch.finfo(torch.float64).eps, top_db=40) -> torch.Tensor:7    """8    Convert the input tensor from amplitude to decibel scale.9 10    Arguments:11        x {[torch.Tensor]} -- [Input tensor.]12 13    Keyword Arguments:14        eps {[float]} -- [Small value to avoid numerical instability.]15                          (default: {torch.finfo(torch.float64).eps})16        top_db {[float]} -- [threshold the output at ``top_db`` below the peak]17            `             (default: {40})18 19    Returns:20        [torch.Tensor] -- [Output tensor in decibel scale.]21    """22    x_db = 20 * torch.log10(x.abs() + eps)23    return torch.max(x_db, (x_db.max(-1).values - top_db).unsqueeze(-1))24 25 26@torch.no_grad()27def temperature_sigmoid(x: torch.Tensor, x0: float, temp_coeff: float) -> torch.Tensor:28    """29    Apply a sigmoid function with temperature scaling.30 31    Arguments:32        x {[torch.Tensor]} -- [Input tensor.]33        x0 {[float]} -- [Parameter that controls the threshold of the sigmoid.]34        temp_coeff {[float]} -- [Parameter that controls the slope of the sigmoid.]35 36    Returns:37        [torch.Tensor] -- [Output tensor after applying the sigmoid with temperature scaling.]38    """39    return torch.sigmoid((x - x0) / temp_coeff)40 41 42@torch.no_grad()43def linspace(start: Number, stop: Number, num: int = 50, endpoint: bool = True, **kwargs) -> torch.Tensor:44    """45    Generate a linearly spaced 1-D tensor.46 47    Arguments:48        start {[Number]} -- [The starting value of the sequence.]49        stop {[Number]} -- [The end value of the sequence, unless `endpoint` is set to False.50                            In that case, the sequence consists of all but the last of ``num + 1``51                            evenly spaced samples, so that `stop` is excluded. Note that the step52                            size changes when `endpoint` is False.]53 54    Keyword Arguments:55        num {[int]} -- [Number of samples to generate. Default is 50. Must be non-negative.]56        endpoint {[bool]} -- [If True, `stop` is the last sample. Otherwise, it is not included.57                              Default is True.]58        **kwargs -- [Additional arguments to be passed to the underlying PyTorch `linspace` function.]59 60    Returns:61        [torch.Tensor] -- [1-D tensor of `num` equally spaced samples from `start` to `stop`.]62    """63    if endpoint:64        return torch.linspace(start, stop, num, **kwargs)65    else:66        return torch.linspace(start, stop, num + 1, **kwargs)[:-1]67