MCPcopy Create free account
hub / github.com/Tencent/hpc-ops / calculate_errors

Function calculate_errors

tests/utils.py:4–90  ·  view source on GitHub ↗

Calculate various error metrics between reference and real tensors Args: ref_tensor: Reference tensor (PyTorch tensor) real_tensor: Real tensor (PyTorch tensor with the same shape as ref_tensor) eps: Small value to prevent division by zero, default 1e-6 top_

(ref_tensor, real_tensor, eps=1e-6, top_k=10)

Source from the content-addressed store, hash-verified

2
3
4def calculate_errors(ref_tensor, real_tensor, eps=1e-6, top_k=10):
5 """
6 Calculate various error metrics between reference and real tensors
7
8 Args:
9 ref_tensor: Reference tensor (PyTorch tensor)
10 real_tensor: Real tensor (PyTorch tensor with the same shape as ref_tensor)
11 eps: Small value to prevent division by zero, default 1e-6
12 top_k: Number of top largest errors to return, default 10
13
14 Returns:
15 dict: Dictionary containing the following metrics:
16 - mean_abs_error: Mean Absolute Error
17 - max_abs_error: Maximum Absolute Error
18 - max_abs_error_ref: Reference value at the position of maximum absolute error
19 - max_abs_error_real: Real value at the position of maximum absolute error
20 - max_abs_error_pos: Position coordinates of maximum absolute error (as tuple)
21 - mean_rel_error: Mean Relative Error
22 - max_rel_error: Maximum Relative Error
23 - max_rel_error_ref: Reference value at the position of maximum relative error
24 - max_rel_error_real: Real value at the position of maximum relative error
25 - max_rel_error_pos: Position coordinates of maximum relative error (as tuple)
26 """
27 # Ensure inputs are PyTorch tensors
28 if not isinstance(ref_tensor, torch.Tensor) or not isinstance(real_tensor, torch.Tensor):
29 raise TypeError("Inputs must be PyTorch tensors")
30
31 # Check if tensor shapes match
32 if ref_tensor.shape != real_tensor.shape:
33 raise ValueError("Reference and real tensors must have the same shape")
34
35 # Calculate absolute errors
36 abs_error = torch.abs(ref_tensor - real_tensor)
37
38 # Mean Absolute Error
39 mae = torch.mean(abs_error).item()
40
41 # Get top K absolute errors and their positions
42 num_elements = abs_error.numel()
43 k = min(top_k, num_elements)
44
45 # Flatten the error tensor and obtain the indices of the top k largest values
46 abs_error_flat = abs_error.flatten()
47 top_abs_values, top_abs_flat_indices = torch.topk(abs_error_flat, k, largest=True)
48
49 # Convert to multidimensional coordinates and collect corresponding values
50 top_abs_errors = []
51 for val, idx in zip(top_abs_values, top_abs_flat_indices):
52 pos = tuple(torch.unravel_index(idx, abs_error.shape))
53 top_abs_errors.append(
54 {
55 "error_value": val.item(),
56 "ref_value": ref_tensor[pos].item(),
57 "real_value": real_tensor[pos].item(),
58 "position": pos,
59 }
60 )
61

Callers 1

allcloseFunction · 0.85

Calls 4

tupleClass · 0.85
minFunction · 0.50
zipFunction · 0.50
maxMethod · 0.45

Tested by

no test coverage detected