MCPcopy Create free account
hub / github.com/ModelTC/LightX2V / error

Function error

lightx2v_kernel/python/lightx2v_kernel/utils.py:38–72  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

36
37
38def 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
75def benchmark(func: Callable, shape: List[int], tflops: float, steps: int, *args, **kwargs):

Callers 4

test_fake_quant.pyFile · 0.90
test_accuracyMethod · 0.90
test_accuracyMethod · 0.90
test_accuracyMethod · 0.90

Calls

no outgoing calls

Tested by 3

test_accuracyMethod · 0.72
test_accuracyMethod · 0.72
test_accuracyMethod · 0.72