utils.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. # this file collects several utility functions
  2. import numpy as np
  3. from keras import backend as K
  4. def naive_percent_loss(y_true, y_pred):
  5. """TODO: Docstring for .
  6. # Sums up the relative errors at all wavelength points of a spectrum
  7. # in comparison to a target spectrum
  8. : y_true -- target spectrum
  9. : y_pred -- predicted spectrum
  10. Note that the user needs to ensure we are comparing similiar spectra
  11. """
  12. y_err = np.abs(y_true - y_pred) / y_true
  13. y_err_f = K.flatten(y_err)
  14. return K.sum(y_err_f)
  15. def calc_mre(y_true, y_pred):
  16. """TODO: Docstring for .
  17. # Gives the mean relative errors in percentage terms comparing two sets of spectra
  18. """
  19. y_err = np.abs(y_true - y_pred) / y_true
  20. y_err = 100*np.abs(y_true - y_pred)/y_true
  21. return np.mean(y_err)
  22. #legacy function to compare two size vectors, not really needed anymore
  23. # def size_percent_loss(y_true, y_pred):
  24. # y_true_a = 0.5*y_true*(size_max - size_min) + size_av
  25. # y_pred_a = 0.5*y_pred*(size_max - size_min) + size_av
  26. # y_err = np.abs(y_true_a - y_pred_a)/y_true_a
  27. # y_err_f = K.flatten(y_err)
  28. # return K.sum(y_err_f)