Compute SNR between y_pred(tensor) and y_real(tensor) SNR can be calcualted as following equation: SNR(pred, real) = (pred - real) ^ 2 / (real) ^ 2 if x and y are matrixs, SNR error over matrix should be the mean value of SNR error over all elements. SNR(pred, real)
(y_pred: torch.Tensor, y_real: torch.Tensor)
| 36 | |
| 37 | |
| 38 | def error(y_pred: torch.Tensor, y_real: torch.Tensor) -> torch.Tensor: |
| 39 | """ |
| 40 | Compute SNR between y_pred(tensor) and y_real(tensor) |
| 41 | |
| 42 | SNR can be calcualted as following equation: |
| 43 | |
| 44 | SNR(pred, real) = (pred - real) ^ 2 / (real) ^ 2 |
| 45 | |
| 46 | if x and y are matrixs, SNR error over matrix should be the mean value of SNR error over all elements. |
| 47 | |
| 48 | SNR(pred, real) = mean((pred - real) ^ 2 / (real) ^ 2) |
| 49 | |
| 50 | |
| 51 | Args: |
| 52 | y_pred (torch.Tensor): _description_ |
| 53 | y_real (torch.Tensor): _description_ |
| 54 | reduction (str, optional): _description_. Defaults to 'mean'. |
| 55 | |
| 56 | Raises: |
| 57 | ValueError: _description_ |
| 58 | ValueError: _description_ |
| 59 | |
| 60 | Returns: |
| 61 | torch.Tensor: _description_ |
| 62 | """ |
| 63 | y_pred = torch.flatten(y_pred).float() |
| 64 | y_real = torch.flatten(y_real).float() |
| 65 | |
| 66 | if y_pred.shape != y_real.shape: |
| 67 | raise ValueError(f"Can not compute snr loss for tensors with different shape. ({y_pred.shape} and {y_real.shape})") |
| 68 | |
| 69 | noise_power = torch.pow(y_pred - y_real, 2).sum(dim=-1) |
| 70 | signal_power = torch.pow(y_real, 2).sum(dim=-1) |
| 71 | snr = (noise_power) / (signal_power + 1e-7) |
| 72 | return snr.item() |
| 73 | |
| 74 | |
| 75 | def benchmark(func: Callable, shape: List[int], tflops: float, steps: int, *args, **kwargs): |
no outgoing calls