BAMresearch / bayem

Implementation and derivation of "Variational Bayesian inference for a nonlinear forward model." [Chappell et al. 2008] for arbitrary, user-defined model errors.
MIT License
2 stars 1 forks source link

VB_handle_diverging_ME #87

Closed ajafarihub closed 2 years ago

ajafarihub commented 2 years ago

While VB iterations, it is likely to get a diverging execution of the Model Error (ME) for some reason (e.g. improper values for parameters lastly updated). In such a case, it would be nice at least to get a final VB output, which would be the best fitted parameters corresponding to the highest value of free energy up to the last iteration. Such an output is most likely not a reliable fit, but might be of any interest for example for studying purposes.

TTitscher commented 2 years ago

Excellent idea! One the one hand, we must avoid the "catch everything" pitfall. On the other hand, we do not know what exactly to catch. So my preferred solution is to have a tuple of allowed exceptions in VBOptions, essentially:

def f_with_value_error(x):
   return int("I am not a number")

result = bayem.vba(f_with_value_error, ...) # fails
result = bayem.vba(f_with_value_error, ..., allowed_exceptions=(ValueError,)) # result.success = False, but you get some values
result = bayem.vba(f_with_value_error, ..., allowed_exceptions=(IndexError,)) # fails (which is important to find bugs!)

internally, this then simply

...
try:
   k, J = self.p(self.m)
except self.options.allowed_exceptions as e:
   self.result.success = False
   self.result.exception = e  # or logger.error(e)
   # ... whatever cleanup
   return self.result
ajafarihub commented 2 years ago

I think, at this point we are not going to uncover the reason behind any exception. That would be the duty of the user, I suppose. I would say, the VB can stop iterating due to ANY exception emerging in the evaluation of the model error and its jacobian, however, the VB must return proper WARNING messages to the user; e.g. hey :) , something has gone wrong in the model evaluation and therefore I stopped iterations. Makes sense?

TTitscher commented 2 years ago

For me, it is very important to know how the code fails (and it also should for you). So if you expect VB to fail because of convergence issues in FEniCS, but it actually fails because of a typo (likely to result in an AttributeError), you should get a real, severe error. Not just a printed message that is likely lost in some logs.

But if you still want this behavior, you could always pass allowed_exceptions=RuntimeError and catch everything...

TTitscher commented 2 years ago

Solved via #88