svenwey commited on
Commit
279e629
·
1 Parent(s): 009c9e0

fix div by 0 in smape-score by replacing all zeroes in denominator with 1

Browse files
Files changed (1) hide show
  1. logmetric.py +9 -3
logmetric.py CHANGED
@@ -130,12 +130,18 @@ class LogMetric(evaluate.Metric):
130
 
131
  if P_isnumber and R_isnumber:
132
  if P == 0 and R == 0: return 1.0 # since this leads to (|R| + |P|) = 0
133
- n = 1
134
  else:
135
  if P == [] and R == []: return 1.0 # since this leads to n = 0
136
  n = len(P)
137
-
138
- return 1/n * np.sum(np.abs(R - P) / (np.abs(R) + np.abs(P)))
 
 
 
 
 
 
139
 
140
  # splits both strings at \n and then computes the smape_score of their lengths
141
  def getLineCountScore(self, pred, ref):
 
130
 
131
  if P_isnumber and R_isnumber:
132
  if P == 0 and R == 0: return 1.0 # since this leads to (|R| + |P|) = 0
133
+ return 1 - (np.sum(np.abs(R - P) / (np.abs(R) + np.abs(P)))) # (n = 1)
134
  else:
135
  if P == [] and R == []: return 1.0 # since this leads to n = 0
136
  n = len(P)
137
+ P = np.array(P)
138
+ R = np.array(R)
139
+ denominator = np.abs(R) + np.abs(P)
140
+ # Replace zeros in the denominator with 1 to avoid division by zero.
141
+ # the denominator[i] = 0 is only possible if R[i] == P[i] == 0, hence we can set denominator[i] = 1 and still achieve the result of 0 after division at index i
142
+ denominator[denominator == 0] = 1
143
+
144
+ return 1 - (1.0/n * np.sum(np.abs(R - P) / denominator))
145
 
146
  # splits both strings at \n and then computes the smape_score of their lengths
147
  def getLineCountScore(self, pred, ref):